HTML5 动画虚线

发布于 2024-11-07 10:12:03 字数 264 浏览 0 评论 0原文

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

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

感谢罗德在这篇帖子中的回答,我已经得到了虚线,但我没有确定从哪里开始让它移动。有人知道从哪里开始吗?

谢谢!

Being relatively new to the HTML5 game, just wanted to ask if anyone knew if it was possible to animate a dashed line along a path? Think snake from Nokia days, just with a dashed line...

I've got a dashed line (which represents electrical current flow), which I'd like to animate as 'moving' to show that current is flowing to something.

Thanks to Rod's answer on this post, I've got the dashed line going, but am not sure where to start to get it moving. Anyone know where to begin?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是偏爱你 2024-11-14 10:12:03

让它在此处工作,基于这篇文章,作者:phrogz

我做了什么:

  • 添加一个 start 参数,该参数是 0 到 99 之间的数字
  • 计算 dashSize,对 dash 数组的内容求和
  • 计算 dashOffSet 为基于 start 百分比的 dashSize 的一小部分
  • 从 x, y 中减去偏移量并添加到 dx, dy
  • 偏移量消失后才开始绘制(它是负数,记住)
  • 添加了 setIntervalstart 从 0 更新到 99,步长为 10

更新

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

此处演示

更新的代码:

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;
}

Got it working here, based on this post by phrogz.

What i did:

  • Add a start parameter which is a number between 0 and 99
  • Calculate the dashSize summing the contents of the dash array
  • Calculate dashOffSet as a fraction of dashSize based on start percent
  • Subtracted the offset from x, y and added to dx, dy
  • Only started drawying after the offset been gone (it´s negative, remember)
  • Added a setInterval to update the start from 0 to 99, step of 10

Update

The original algorithm wasn't working for vertical or negative inclined lines. Added a check to use the inclination based on the y slope on those cases, and not on the x slope.

Demo here

Updated code:

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;
}
苏辞 2024-11-14 10:12:03

您可以使用 SNAPSVG 库创建虚线动画。

请在此处查看教程DEMO
虚线动画

You can create the dashed line animation using SNAPSVG library.

Please check the tutorial here DEMO
Dashed Animation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文