jquery - 动画渐进绘制的虚线 HTML5 Canvas & Jquery

标签 jquery html canvas

我如何修改以下代码,使绘制的线为虚线,您可以在提供的 jfiddle 中看到它的实际效果。

<html>
    <style>
        #canvas
        {
        border-style:solid;
        border-width:1px;
        }
    </style>
    <div id="canvas"> 
        <p>Hover over me</p>        
    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</html>

$(function() {

    animateLine = function(canvas, hoverDivName, colorNumber, pathString) {
        $('#' + hoverDivName).hover(

        function() {
            var line = canvas.path(pathString).attr({
                stroke: colorNumber
            });
            var length = line.getTotalLength();

            $('path[fill*="none"]').animate({
                'to': 1
            }, {
                duration: 5000,
                step: function(pos, fx) {
                    var offset = length * fx.pos;
                    var subpath = line.getSubpath(0, offset);
                    canvas.clear();
                    canvas.path(subpath).attr({
                        stroke: colorNumber
                    });

                },
            });
        }, function() {
            $('path[fill*="none"]').stop(true, false).fadeOut();
        });
    };

    var canvas = Raphael('canvas', 200, 200);
    var pathString = "M10 10L10 200L100 200Z";

    animateLine(canvas, "canvas", "#000", pathString);

});

http://jsfiddle.net/eA8bj/

最佳答案

使用“stroke-dasharray”属性:

http://jsfiddle.net/eA8bj/70/

https://developer.mozilla.org/en-US/docs/SVG/Attribute/stroke-dasharray

例如:

            canvas.path(subpath).attr({
                stroke: colorNumber,
                "stroke-dasharray":"--"
            });

关于jquery - 动画渐进绘制的虚线 HTML5 Canvas & Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15066411/

相关文章:

javascript - KineticJS:动力学警告:无法获取数据 URL

javascript - 我如何压缩/优化这段代码(jQuery)?

html - 与底部对齐的中心 div 组

javascript - 如何在 KineticJS Wedge 中垂直居中文本?

html - 我正在从 Bootstrap 应用模态,但它没有正确关闭

javascript - 如何在HTML中制作波浪动画

javascript - 模糊窗口全宽没有透明边缘的 Canvas ?

jQuery 显示/隐藏按钮

javascript - 模板事件不适用于使用 Meteorjs 的模态

javascript - 完成百分比动画,圆形形式,带状态显示