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); ...
随机推荐
- SharePoint 2010 产品六大功能模块
对IT专业人员来说,新的产品,意味着新的功能,IT生产力提高的契机,更加关心如何使用新产品投入更少,产出更多.在本文中我们通过对SharePoint 2010产品的功能介绍.管理中心新功能介绍.系统新 ...
- Area
http://poj.org/problem?id=1265 #include<cstdio> #include<istream> #include<algorithm& ...
- Ext.js form 表单提交问题
var form = new Ext.form.FormPanel({ labelAlign : 'right', border : false, bodyStyle : 'background-co ...
- Linux下安装软件心得
1 软件安装方法: 源代码编译安装:tar.gz等压缩格式,需要经过手动编译,./configure,make ,make install ,然后进行配置操作 二进制安装:tar.gz等压缩格式,解压 ...
- Unity NGUI实现Tabview
unity版本:4.5.1 NGUI版本:3.5 参考链接:http://blog.csdn.net/g__dragon/article/details/17242969,作者:CSDN G_Drag ...
- 【转】Chrome保存mhtml网页文件的方法 – 无需任何插件,完美!
原文网址:http://www.ihacksoft.com/chrome-save-mht.html 在 Chrome 地址栏中键入“chrome://flags”,回车,这是一个 Chrome 的功 ...
- 查看 AndroidManifest.xml文件
1.Manifest Explorer 装在Android手机中,用此apk看系统中已安装应用的AndroidManifest.xml文件: protected boolean configForPa ...
- K - Candies(最短路+差分约束)
题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面 ...
- poj2393
题目大意: 奶酪工厂 奶牛买了一个奶酪工厂制作全世界有名的Yucky酸奶,在接下来的N周(1<=N<=10000),牛奶的价格和工作将会受到波动例如他将花费C_i (1 <= C_i ...
- javaweb开发过程中的地址写法
凡是要表示web资源的地址,比如浏览器地址栏中,都是 /凡是要表示硬盘地址, 都是 \ public class ServletDemo1 extends HttpServlet { //实际开发过 ...