的jsfiddle:https://jsfiddle.net/CanvasCode/vgpov2yk/3
var c = document.getElementById('myCanvas');
var ctx = c.getContext("2d");
// Positions for the text startint off
var xText = -100;
var yText = 150;
// This will update the canvas as fast as possible (not good)
setInterval(function() {
// Clear the canvas
ctx.fillStyle = "#F0F";
ctx.fillRect(0, 0, c.width, c.height);
ctx.save()
// We create a clip rect
ctx.rect(200,0,400,400);
ctx.clip();
// Draw text
ctx.fillStyle = "#FFF";
ctx.font = "30px Arial";
ctx.fillText("Hello World", xText, yText);
ctx.restore();
// Increase the text x position
if (xText < 200) {
xText += 0.5;
}
}, 15);
所有你需要做的就是用剪辑矩形这就好比是在图像编辑口罩。
滑动文字时,可以在障碍物的区域上使用'ctx.clearRect()'。 – 1cgonza
那也不清楚在文本下画布上画的其他东西吗? – pera
是的,如果你的画布上已经有东西,那么'clearRect()'将会删除它。我建议你在jsfiddle中构建一个简单的版本,并更新你的问题。如果我们能看到一些代码,你会在这里得到更好的帮助。 – 1cgonza