HTML5动画虚线

问题描述 投票:4回答:2

在HTML5游戏中还比较陌生,只是想问问是否有人知道是否可以沿路径设置虚线动画?用虚线想想诺基亚时代的蛇...

我有一条虚线(代表电流),我想对其进行动画处理,以表示电流正在流动。

[感谢Rod在this上的回答,我的虚线已经走了,但不确定从哪里开始使它前进。有人知道从哪里开始吗?

谢谢!

javascript html5 animation canvas path
2个回答
8
投票

根据here乘以this post,使其正常工作[[phrogz

我做了什么:

    [添加一个start参数,该参数是介于0到99之间的数字
  • 计算破折号数组内容的dashSize总和
  • 基于dashOffSet百分比计算dashSize作为start的分数
  • [减去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; }


  • 0
    投票
    您可以使用SNAPSVG库创建虚线动画。

    请在此处查看教程DEMODashed Animation

    © www.soinside.com 2019 - 2024. All rights reserved.