以下代码(jsFiddle)在画布上随机绘制红色正方形,并注意通过使用
代码无法完全擦除先前的正方形,屏幕上仍保留着瑕疵。如果我这样做:
ctx.fillRect()
填充一个白色正方形来清除先前的正方形:<html>
<canvas id='canvas' width=300 height=300/>
<script>
const ctx = document.getElementById('canvas').getContext('2d');
let prevRect = null;
for (let i = 0 ; i < 10; i++) {
if (prevRect != null) {
ctx.fillStyle='white';
ctx.fillRect(prevRect.x, prevRect.y, 50, 50);
}
ctx.fillStyle='red';
const newRect = {x: Math.random()*(300-50), y: Math.random()*(300-50)};
ctx.fillRect(newRect.x, newRect.y, 50, 50);
prevRect = newRect;
}
</script>
</html>
代码无法完全擦除先前的正方形,屏幕上仍保留着瑕疵。如果我这样做:
const newRect = {x: Math.floor(Math.random()*(300-50)), y: Math.floor(Math.random()*(300-50))};
我有一个问题,为什么要这样做。看起来完全没有必要进行截断,因为我在prevRect
中保留了值,所以两个对fillRect()
的调用使用完全相同的坐标(即使使用浮点数),因此这两个正方形应该始终完美对齐。
然后一切都按预期工作。
var afterDelete = interpolate(background, newForeground, percentage);
- kotchwane