var _canvas = document.getElementById('cansStage');
var _context = _canvas.getContext("2d");
_context .fillText('hello world llo world'llo world'llo world'llo world'llo world'', 16, 16); //
无法换行呀,伤不起
var _context = _canvas.getContext("2d");
_context .fillText('hello world llo world'llo world'llo world'llo world'llo world'', 16, 16); //
无法换行呀,伤不起
解决方案 »
- 明眼人一看就明白,为什么的Jplayer放不了mp3文件,写错了吗
- 我想实现0-6位数字抽奖,取随机数。请帮忙
- 如何将字典数组字符串转换为数组
- 怎么为表格里的动态文字创建连接?
- 关于jquery.autocomplete控件
- $获取select的长度
- input的onkeydown ,onkeyup,onpropertychange
- 用javascript做脚本语言怎么写防SQL注入程序呢
- 求救!!!!能不能不限定iframe的高度,让它所调用的外部文件的文档长度来决定iframe的高度?
- 谢谢孟子兄,还有问题。。。
- 求教:怎么获取windows2000系统下的路由表?
- IE不能调用本地的js文件
这个canvas是绘图的,所有可能\r\n无效果
难道换行要重新绘制吗?
这个是网站 http://www.iseefor.com/Html5_one.html帮我看看如何换行。
function getLines(text, maxWidth) {
var returns = text.split("\n");
var lines = [];
var lastPhrase = "";
function splitWord() {
var width = measureText(lastPhrase).width;
var posA = 0;
var posZ = 0;
if (width > maxWidth) {
for (var n = 0, length = lastPhrase.length; n < length; n ++) {
var width = measureText(lastPhrase.substr(posA, posZ ++)).width;
if (width > maxWidth) {
lines.push(lastPhrase.substr(posA, posZ - 2));
posA = n - 1;
posZ = 2;
}
}
return lastPhrase.substr(posA, posZ + 2);
}
};
for (var n = 0; n < returns.length; n++) {
if (lastPhrase) lines.push(lastPhrase);
var phrase = returns[n];
var spaces = phrase.split(" ");
var lastPhrase = "";
for (var i = 0; i < spaces.length; i++) {
var measure = measureText(lastPhrase + " " + spaces[i]).width;
if (measure < maxWidth) {
lastPhrase += ((lastPhrase ? " " : "") + spaces[i]);
} else {
if (measure > maxWidth) {
var split = splitWord();
if (split) {
lastPhrase = split + " " + spaces[i];
} else {
lines.push(lastPhrase);
lastPhrase = spaces[i];
}
}
}
if (i == spaces.length - 1) {
lines.push(lastPhrase);
lastPhrase = "";
break;
}
}
}
return lines;
};
然后这样调用:
var lines = getLines(text + " ", maxWidth);
for (var n = 0, length = lines.length; n < length; n ++) {
ctx.fillText(lines[n], 0, (n * bboxHeight) + offsety);
}
希望对你有所帮助