2013-02-25 7 views
5

は、どのように私は次のコードを変更することができ、あなたが提供jfiddleでアクションでそれを見ることができます。アニメイト徐々に描かれた破線HTML5キャンバス&jqueryの

<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/

+0

[キャンバスのためのWHATWG仕様](http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#line-styles)が想定されるsetLineDash方法を言及しますそれを行うには、私はそれをサポートしようとしたブラウザはありません。 – Philipp

+0

キャンバスに破線や点線を作成する方法については、[この](http://stackoverflow.com/questions/4576724/dotted-stroke-in-canvas)の回答を参照してください。 – Zemljoradnik

答えて

5

使用 "ストローク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":"--" 
      }); 
+0

ありがとう、完璧!リンクをクリックするとコードがどのように変化するか知っていますか?それとも他のイベントですか? – user1937021

+0

http://jsfiddle.net/eA8bj/72/ < - jQuery on()を使用して、使用するイベントを使用します。 – StuR

関連する問題