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文本居中实例的更多相关文章

  1. JavaScript 基于HTML5 canvas 获取文本占用的像素宽度

    基于HTML5 canvas 获取文本占用的像素宽度   by:授客 QQ:1033553122 直接上代码   // 获取单行文本的像素宽度 getTextPixelWith(text, fontS ...

  2. HTML5 Canvas图片操作简单实例1

    1.加载显示图片 <canvas id="canvasOne" class="myCanvas" width="500" height ...

  3. Html5 Canvas Text

    html5 canvas中支持对text文本进行渲染;直接的理解就是把text绘制在画布上,并像图形一样处理它(可以加shadow.gradient.pattern.color fill等等):既然它 ...

  4. html5 canvas在线文本第二步设置(字体边框)等我全部写完,我会写在页面底部

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. HTML5 Canvas绘文本动画(使用CSS自定义字体)

    一.HTML代码: <!DOCTYPE html> <html> <head> <title>Matrix Text - HTML5 Canvas De ...

  6. html5 canvas 圆形抽奖的实例

    年底没啥,抽空学习了canvas,写了个html5抽奖的代码,造了个轮子,有用的童鞋可以拿走. 其中,canvas.onclick触发抽奖行为,概率可以在core.lottery()函数上添加,美化也 ...

  7. HTML5 Canvas Text实例1

    1.简单实例1 <canvas width="300" height="300" id="canvasOne" class=" ...

  8. HTML5 canvas绘制文本

    demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...

  9. HTML5区域范围文本框实例页面

    CSS代码: input { font-size: 14px; font-weight: bold; } input[type=range]:before { content: attr(min); ...

随机推荐

  1. ps的使用方法

    1.打开原图素材,Ctrl + J把背景图层复制一层,按Ctrl + Shift + U去色,执行:滤镜 > 模糊 > 高斯模糊,数值4,图层混合模式为滤色,图层不透明度改为27%. 2. ...

  2. Browsing History

    hdu4464:http://acm.hdu.edu.cn/showproblem.php?pid=4464 题意:就是统计n个字符串中每个字符串每个字符对印的Asci,然后输出最大的长度. 题解:水 ...

  3. Groovy学起来,这要和GRAILS,RUNDECK打成一片

    还好,以前看过RUBY和JAVA,GROOVY感觉和它们有点相似.. 并且,我觉得这个GROOVY比SCALA要简单些(函数式编程+OBJ) 作类比,毕竟是最快的学习方法. XXX,还有必修课和证券从 ...

  4. delphi 程序是否为控制台编译选项

    http://www.birdol.com/article/tag/delphi 编译指令写在哪?: 编译指令可以写在代码页的任何地方, 不过在代码的不同区域有时也会不同; 譬如: {$APPTYPE ...

  5. Android 判断wifi和4G网络是否开启

    public boolean isWifiAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) g ...

  6. BZOJ 1071 [SCOI2007]组队

    1071: [SCOI2007]组队 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1330  Solved: 417[Submit][Status][ ...

  7. 【模拟】NEERC15 E Easy Problemset (2015-2016 ACM-ICPC)(Codeforces GYM 100851)

    题目链接: http://codeforces.com/gym/100851 题目大意: N个人,每个人有pi个物品,每个物品价值为0~49.每次从1~n顺序选当前这个人的物品,如果这个物品的价值&g ...

  8. Hierarchy Viewer显示视图性能指标

    Hierarchy Viewer默认打开“Tree View”窗口无法显示显示Performance indicators: 但选中根视图再点击按钮“Obtain layout times for t ...

  9. 动态规划——K背包问题

    Problem DescriptionNow you are asked to measure a dose of medicine with a balance and a number of we ...

  10. [后端Day1]Python2.7之基础

    注:以下内容全部为 廖雪峰的官方网站 中学习内容的摘要和总结 输入和输出: name = raw_input('please enter your name: ') print '100 + 200 ...