我有一段简单的代码可以在页面中画一条线。我的问题是我对 HTML5 或 JS 不太了解,我需要帮助设置延迟绘制这条线。我希望能够选择是否要在打开页面时立即看到它绘制或将其定义为在绘制之前有 5 秒的延迟。
这里是:
<canvas id="myCanvas" width="1250" height="120"></canvas>
<script>
var canvas = $("#myCanvas")[0];
var c = canvas.getContext("2d");
var amount = 0;
var startX = 164;
var startY = 120;
var endX = 1094;
var endY = 120;
setInterval(function() {
amount += 0.01; // change to alter duration
if (amount > 1) amount = 1;
c.clearRect(0, 0, canvas.width, canvas.height);
c.strokeStyle = "black";
c.lineWidth=1;
c.strokeStyle="#707070";
c.moveTo(startX, startY);
// lerp : a + (b - a) * f
c.lineTo(startX + (endX - startX) * amount, startY + (endY - startY) * amount);
c.stroke();
}, 0);
</script>
感谢您的帮助!