HTML5 Canvas Text文本居中实例
1.代码:
<canvas width="700" height="300" id="canvasOne" class="canvasOne"></canvas>
<script>
var cancasOne = document.getElementById('canvasOne');
var ctx = cancasOne.getContext('2d');
var text = '后会无期',
textMetrics,
square_width = 20,
font_height = 128;
//画网线
function drawGrid(color, stepx, stepy) {
ctx.save();
ctx.strokeStyle = color;
ctx.lineWidth = 0.5;
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvasOne.width, canvasOne.height);
//画竖线
for (var i = stepx + 0.5; i < canvasOne.width; i += stepx) {
ctx.beginPath();
ctx.moveTo(i, 0);
ctx.lineTo(i, canvasOne.height);
ctx.stroke();
}
//画横线
for (var i = stepx + 0.5; i < canvasOne.height; i += stepy) {
ctx.beginPath();
ctx.moveTo(0, i);
ctx.lineTo(canvasOne.width, i);
ctx.stroke();
}
ctx.restore();
}
//画文本
function drawText() {
ctx.fillStyle = 'orange';
ctx.strokeStyle = 'cornflowerblue';
ctx.fillText(text, canvasOne.width / 2,
canvasOne.height / 2);
ctx.strokeText(text, canvasOne.width / 2,
cancasOne.height / 2);
}
//画中间的小正方形
function drawCenterSquare() {
ctx.fillStyle = 'rgba(255,0,0,0.4)';
ctx.strokeStyle = 'black';
ctx.fillRect(canvasOne.width / 2 - square_width / 2,
canvasOne.height / 2 - square_width / 2,
square_width, square_width);
ctx.strokeRect(canvasOne.width / 2 - square_width / 2,
cancasOne.height / 2 - square_width / 2,
square_width, square_width);
}
ctx.font = '128px Helvetica';
ctx.textBaseline = 'middle'; //设置文本的垂直对齐方式
ctx.textAlign = 'center';//设置文本的水平对齐方式
textMetrics = ctx.measureText(text);
drawGrid('lightgray', 10, 10);
drawText();
drawCenterSquare();
</script>
2.显示结果:

HTML5 Canvas Text文本居中实例的更多相关文章
- JavaScript 基于HTML5 canvas 获取文本占用的像素宽度
基于HTML5 canvas 获取文本占用的像素宽度 by:授客 QQ:1033553122 直接上代码 // 获取单行文本的像素宽度 getTextPixelWith(text, fontS ...
- HTML5 Canvas图片操作简单实例1
1.加载显示图片 <canvas id="canvasOne" class="myCanvas" width="500" height ...
- Html5 Canvas Text
html5 canvas中支持对text文本进行渲染;直接的理解就是把text绘制在画布上,并像图形一样处理它(可以加shadow.gradient.pattern.color fill等等):既然它 ...
- html5 canvas在线文本第二步设置(字体边框)等我全部写完,我会写在页面底部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HTML5 Canvas绘文本动画(使用CSS自定义字体)
一.HTML代码: <!DOCTYPE html> <html> <head> <title>Matrix Text - HTML5 Canvas De ...
- html5 canvas 圆形抽奖的实例
年底没啥,抽空学习了canvas,写了个html5抽奖的代码,造了个轮子,有用的童鞋可以拿走. 其中,canvas.onclick触发抽奖行为,概率可以在core.lottery()函数上添加,美化也 ...
- HTML5 Canvas Text实例1
1.简单实例1 <canvas width="300" height="300" id="canvasOne" class=" ...
- HTML5 canvas绘制文本
demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...
- HTML5区域范围文本框实例页面
CSS代码: input { font-size: 14px; font-weight: bold; } input[type=range]:before { content: attr(min); ...
随机推荐
- hadoop 2.2.0 集群部署 坑
注意fs.defaultFS为2..0新的变量,代替旧的:fs.default.name hadoop 2.2.0 集群启动命令:bin/hdfs namenode -formatsbin/start ...
- 常见的HTTPS攻击方法
0x00 背景 研究常见的https攻击方法 Beast crime breach,并针对https的特性提出一些安全部署https的建议. 针对于HTTPS的攻击,多存在于中间人攻击的环境中,主要是 ...
- ASP.NET最常用的页面生命周期事件
PreInit:在页生命周期的早期阶段可以访问的事件.在PreInit事件后,将加载个性化信息和页主题. Init:在所有的控件都已初始化,且已应用所有外观设置后引发.使用该事件来读取或初始化控件属性 ...
- Solution for "De-serialization exception: Unable to find assembly xxxxx"
public void DeSerialize() { BinaryFormatter formatter = new BinaryFormatter(); AppDomain.CurrentDoma ...
- C# 绘图对象 流对象 响应对象关系
- 【HDOJ】2451 Simple Addition Expression
递推,但是要注意细节.题目的意思,就是求s(x) = i+(i+1)+(i+2),i<n.该表达中计算过程中CA恒为0(包括中间值)的情况.根据所求可推得.1-10: 31-100: 3*41- ...
- nginx 1.3.9/1.4.0 x86 Brute Force Remote Exploit
测试方法: 本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! #nginx 1.3.9/1.4.0 x86 brute force remote exploit # copyri ...
- 学习Javascript闭包(Closure) by 阮一峰
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...
- (转载)用PHP正则表达式清除字符串的空白
(转载)http://www.chinaz.com/program/2009/0220/67569.shtml 我们经常会处理来自用户输入或从数据库中读取的数据,可能在你的字符串中有多余的空白或制表符 ...
- 数据结构(二维线段树,差分): NOI2012 魔幻棋盘
貌似想复杂了…… #include <iostream> #include <cstring> #include <cstdio> #define mid ((l+ ...