我正在尝试编写解决上述问题的代码。我尝试找到一个解决方案。这是我目前拥有的。
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var drawColorLine = function(start, end, color) {
var deltaX, deltaY, i = 0,
currLength = 0,
isHor, isVert;
deltaX = end[0] - start[0];
deltaY = end[1] - start[1];
context.strokeStyle = color;
isHor = deltaX === 0 ? 0 : 1;
isVert = deltaY === 0 ? 0 : 1;
function draw() {
context.beginPath();
context.moveTo(start[0] + currLength * isHor, start[1] + currLength * isVert);
currLength = currLength + 0.5 * i;
context.lineTo(start[0] + currLength * isHor, start[1] + currLength * isVert);
context.stroke();
if (currLength <= Math.max(deltaX, deltaY)) {
i = i + 1;
requestAnimationFrame(draw);
}
}
draw();
};
drawColorLine([40, 40], [100, 40], '#116699');
drawColorLine([40, 40], [40, 100], '#bb11dd');
<canvas id='canvas' width='400' height='400'></canvas>
提前致谢。
then
回调函数可以返回承诺,然后会进行等待(详见这里 或 这里)。我的答案在magic happens here
那一行使用了它 :-) - Bergi