我想要移動剪輯區域的位置,然後在剪輯區域中繪製。下面的方法怎麼不起作用? 感謝任何啓示:-) 傑拉德是否可以爲畫布剪輯區域的位置設置動畫?
<canvas id="canvas" width="150" height="150"></canvas>
<script>
function fade() {
var level = 0;
var xClip=0, yClip=0;
var step = function () {
var ctx = document.getElementById('canvas').getContext('2d');
ctx.fillRect(0,0,150,150);
// Create a circular clipping path
ctx.beginPath();
ctx.arc(xClip, xClip, 60, 0, Math.PI*2, true);
ctx.clip();
// draw background
var lingrad = ctx.createLinearGradient(0,-75,0,75);
lingrad.addColorStop(0, '#232256');
lingrad.addColorStop(1, '#143778');
ctx.fillStyle = lingrad;
ctx.fillRect(-75,-75,150,150);
if (level < 15) {
level ++;
xClip = yClip = level*10;
console.log("level: " + level);
console.log("xClip: " + xClip);
setTimeout(step, 1000);
}
};
setTimeout(step,100);
}
fade();
</script>
您只需創建一次上下文。使用'ctx.save()'在創建clop之前保存上下文狀態,然後使用函數底部的'ctx.restore()'恢復狀態。 – Blindman67