的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