javascript - HTML5 虚线动画

标签 javascript html animation canvas path

作为 HTML5 游戏的新手,只是想问问是否有人知道是否可以沿路径设置虚线动画?想想诺基亚时代的蛇,只是一条虚线...

我有一条虚线(代表电流),我想将其设置为“移动”的动画,以表明电流正在流向某物。

感谢 Rod 在 this 上的回答帖子,我有虚线,但我不确定从哪里开始让它移动。有人知道从哪里开始吗?

谢谢!

最佳答案

成功了 here ,基于this post通过 phrogz .

我做了什么:

  • 添加一个 start 参数,它是一个介于 0 和 99 之间的数字
  • 计算 dashSize 对破折号数组的内容求和
  • 根据 start 百分比计算 dashOffSet 作为 dashSize 的分数
  • 从x、y减去偏移量并添加到dx、dy
  • 只有在偏移量消失后才开始绘制(记住,它是负的)
  • 添加了 setInterval 以将 start 从 0 更新为 99,步长为 10

更新

原始算法不适用于垂直线或负斜线。在这些情况下添加了基于 y 斜率而不是 x 斜率使用倾斜度的检查。

Demo here

更新代码:

if (window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype.lineTo) {
    CanvasRenderingContext2D.prototype.dashedLine = function(x, y, x2, y2, dashArray, start) {
        if (!dashArray) dashArray = [10, 5];
        var dashCount = dashArray.length;
        var dashSize = 0;
        for (i = 0; i < dashCount; i++) dashSize += parseInt(dashArray[i]);
        var dx = (x2 - x),
            dy = (y2 - y);
        var slopex = (dy < dx);
        var slope = (slopex) ? dy / dx : dx / dy;
        var dashOffSet = dashSize * (1 - (start / 100))
        if (slopex) {
            var xOffsetStep = Math.sqrt(dashOffSet * dashOffSet / (1 + slope * slope));
            x -= xOffsetStep;
            dx += xOffsetStep;
            y -= slope * xOffsetStep;
            dy += slope * xOffsetStep;
        } else {
            var yOffsetStep = Math.sqrt(dashOffSet * dashOffSet / (1 + slope * slope));
            y -= yOffsetStep;
            dy += yOffsetStep;
            x -= slope * yOffsetStep;
            dx += slope * yOffsetStep;
        }
        this.moveTo(x, y);
        var distRemaining = Math.sqrt(dx * dx + dy * dy);
        var dashIndex = 0,
            draw = true;
        while (distRemaining >= 0.1 && dashIndex < 10000) {
            var dashLength = dashArray[dashIndex++ % dashCount];
            if (dashLength > distRemaining) dashLength = distRemaining;
            if (slopex) {
                var xStep = Math.sqrt(dashLength * dashLength / (1 + slope * slope));
                x += xStep
                y += slope * xStep;
            } else {
                var yStep = Math.sqrt(dashLength * dashLength / (1 + slope * slope));
                y += yStep
                x += slope * yStep;
            }
            if (dashOffSet > 0) {
                dashOffSet -= dashLength;
                this.moveTo(x, y);
            } else {
                this[draw ? 'lineTo' : 'moveTo'](x, y);
            }
            distRemaining -= dashLength;
            draw = !draw;
        }
        // Ensure that the last segment is closed for proper stroking
        this.moveTo(0, 0);
    }
}

var dashes = '10 20 2 20'

var c = document.getElementsByTagName('canvas')[0];
c.width = 300;
c.height = 400;
var ctx = c.getContext('2d');
ctx.strokeStyle = 'black';

var drawDashes = function() {
    ctx.clearRect(0, 0, c.width, c.height);
    var dashGapArray = dashes.replace(/^\s+|\s+$/g, '').split(/\s+/);
    if (!dashGapArray[0] || (dashGapArray.length == 1 && dashGapArray[0] == 0)) return;

    ctx.lineWidth = 4;
    ctx.lineCap = 'round';
    ctx.beginPath();
    ctx.dashedLine(10, 0, 10, c.height, dashGapArray, currentOffset);
    ctx.dashedLine(0, 10, c.width, 10, dashGapArray, currentOffset);
    ctx.dashedLine(0, 0, c.width, c.height, dashGapArray, currentOffset);
    ctx.dashedLine(0, c.height, c.width, 0, dashGapArray, currentOffset);
    ctx.closePath();
    ctx.stroke();
};
window.setInterval(dashInterval, 500);

var currentOffset = 0;

function dashInterval() {
    drawDashes();
    currentOffset += 10;
    if (currentOffset >= 100) currentOffset = 0;
}

关于javascript - HTML5 虚线动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012940/

相关文章:

javascript - HTML 音频播放器时间轴功能添加

javascript - 在for循环中循环遍历google Maps Api

jquery - 为什么我的 jQuery 过滤器只对这个选择元素起作用一次?

css - 分区定位

html - 为 div 设置动画?正确的选择器?

javascript - 为什么 A-Frame 相机动画不总是沿着最短路径旋转?

jquery - 谷歌图表格式

javascript - 动画时阻止链接切换

html - CSS 和 Div 定位

javascript - 在单个元素上设置多个动画,当它们一一触发时