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); ...
随机推荐
- WebApp 中用 hashchange 做路由解析
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 关于javascript
Client-Language-----------------------JavaScript/Object-C/Java/C# HTML5 DOM/Template/Data/Ajax/Regul ...
- Unity KGFMapSystem插件制作小地图
KGFMapSystem版本:2.3 在我们开发游戏或者虚拟现实中,一般都会用到小地图,如果要我们去写小地图,可以用到unity 3d中就有一个插件,是专门开发小地图用的,这个插件就是KGFMapSy ...
- BZOJ 1083 [SCOI2005]繁忙的都市
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1664 Solved: 1080[Submit][Sta ...
- 【图片处理】ImageMagick, gm
ImageMagick: Convert, Edit, Or Compose Bitmap Images http://www.imagemagick.org/script/binary-releas ...
- Unity Dx9 Occlusion Query plugin
//Occlusion Unity plugin #include "UnityPluginInterface.h" #include <math.h>#include ...
- HDOJ 2188 悼念512汶川大地震遇难同胞——选拔志愿者
Problem Description 对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每个省市都派出了大量的救援人员,这其中包括抢险救灾的武警部队,治疗和防疫的医护人员,以及进行心理疏导的心理 ...
- 在VC中集成cURL
libcurl 库的代码是完全开源的,但是我们一般不会在项目中直接引入它的源代码,而是通过动态链接库隐式链接的方式引入 libcrul 库.也就是说,我们需要在自己的项目中引入 libcrul 相关的 ...
- JavaScript中的声明提升
JavaScript中变量或者函数的声明会被提升(赋值语句不会被提升)到当前函数主体的顶部,不管这个声明是否出现在不可到达的地方. var test = 1; function f() { if(!t ...
- bithrtree
#include "stdio.h" #include "stdlib.h" #define OK 1 #define ERROR 0 typedef char ...