【发布时间】:2013-07-31 14:42:16
【问题描述】:
我在
中添加了一个画布文本 <canvas id="canvasOne" width="500" height="500">
Your browser does not support HTML5 Canvas.
</canvas>
Javascript 代码:
theCanvas = document.getElementById("canvasOne");
var context = theCanvas.getContext("2d");
var text = 'word';
context.font = '16pt Calibri';
context.fillStyle = '#333';
var p0 = {x:x0,y:y0};
var word = {x:p0.x, y:p0.y, velocityx: 0, velocityy:0};
var lastTime = new Date().getTime();
wrapText(context, text, p0.x, p0.y, maxWidth, lineHeight);
wrapText 函数():
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
}
如何使用 clearRect() 只删除单词框?
context.clearRect(word.x, word.y, word.x + offsetX, word.y + offsetY);
更新
通过@Ozren Tkalčec Krznarić 技巧部分解决。但它并没有完全抹去这个词,部分先例没有抹去(见上图)。
您可以在这里看到问题:michelepierri.it/examples/canvas.html
jsfiddle:http://jsfiddle.net/michelejs/yHnYh/6/
非常感谢。
【问题讨论】:
-
您的代码不完整。
context未定义。word根本没有使用。wrapText未定义...
标签: html canvas html5-canvas