Question

I have the following (simplified) SVG:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 120 100" preserveAspectRatio="none" >
   <script type="text/ecmascript">
       <![CDATA[
        function getPathLength(path)
        {
           alert(path.getTotalLength());
        }
       ]]>
    </script>
   <rect x="0" y="0" width="100%" height="100%" fill="black"/>

   <path id="word" onclick="getPathLength(this);" fill="none" stroke="red" stroke-dasharray="83,83" 
    d="M 8.90625 3.78125 C 8.5377448 3.8134782 8.1622587 3.965423 7.8125 4.21875 C 6.5568946 6.0743722 5.8966744 8.1275786 5.125 10.15625 C 5.2012388 11.683341 4.3280721 13.093169 4.34375 14.59375 C 3.5435523 17.003468 4.9013748 18.393271 4.125 20.90625 C 3.8249969 23.220653 3.3490969 25.389271 2.96875 27.71875 C 2.775721 29.626027 1.7870007 33.314518 4.6875 33.4375 C 6.6892564 33.267533 8.9446263 33.620664 10.6875 32.53125 C 12.308551 32.345986 13.845824 31.488491 14.96875 30.4375 C 15.336184 29.709947 14.506173 29.249547 15.09375 28.53125 C 15.464699 26.390933 13.333175 28.42555 12.53125 28.4375 C 10.625482 29.096293 8.8511553 29.69647 6.875 29.65625 C 5.6218253 29.22028 6.5435685 27.168764 6.34375 26.03125 C 6.6314225 24.872659 6.8389714 23.78868 7.0625 22.53125 C 7.4982943 20.310119 8.0925706 18.009049 8.5 15.84375 C 9.2688601 14.209069 9.0995411 12.312299 9.71875 10.5625 C 10.027547 9.1134942 10.141384 7.3374588 10.5 5.90625 C 10.999008 4.514099 10.011766 3.6845653 8.90625 3.78125 z ">
      <animate attributeName="stroke-dashoffset" begin="0s" from="100%" to="0%" dur="5s" fill="freeze" />
   </path>
</svg>

When I click on the path to get the length Firefox v16 reports 82.4... which is what I use for the dasharray. But it starts out with part of the path already drawn, erases it then draws the full path again!? I tinkered witht he dasharay values and it works when I use 110 but I don't know why!

I have not tried other browsers or newer versions of Firefox yet...

NOTE The full problem is a word converted to a path. The path is discontinuous - it has 5 parts (there is a loop in the script forms of 'e', 'z' and 'h' and the first letter 'L' is not joined with the rest). What I want to do is make it look like a laser is drawing the letters. In a previous questions I learned that animating the dashoffset will acheive that effect. I was finally able to convert text to path using Inkscape. Now I am having trouble animating.

I tried animating the dasharray. That works for the correct length, but with the larger full word example (which is 7 letters long and 822 units long) it looks like several paths are in play - not just the 5 discontinuous parts but 9 different paths. When I try to animate the dashoffset on the full word I see NO animation at all - the word just appears fully formed.

Was it helpful?

Solution

Did you think 100% would be 100% of the length of the path? If so that's where you're going wrong as it's 100% of the viewport width. I don't think Opera and Firefox have a bug here, particularly as they happen to be displaying things identically.

OTHER TIPS

Figured it out. I was trying to be cute by using a percentage so that I did not have to put the length in each animate command - all my animates were 100% to 0%. When I replaced the percentages with the actual lengths it worked as expected.

So looks like a bug in the SVG implementations of at least Firefox and Opera as the documentation clearly says you can use percentages...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top