New Post has been published on http://www.attuts.com/draw-walkpath-in-svg-svg-animation-html5/
Draw WalkPath in SVG - SVG animation | HTML5
Today we are going to see how can we draw a walkpath over a vector map. For vector map, we are going to use SVG, since SVG graphics are designed for use on the web or on web pages, basic knowledge of HTML / XHTML, XML, CSS and JavaScript are highly recommended for the understanding of the tutorial. I have recently finished a project that contains functionality to show walkpaths to the seat on the map of the application. If someone asked me to develop just kind of project using html and javascript, few years ago then definitely I would choose flash. Following library and element we are going to use in this project
Jquery
SVG
Lazy line painter
Lazy line painter library help us in order to animate vector paths.
I wanted to share the demo before starting the tutorial, it would help us in better understanding. I tried to make this tutorial easy and simple. Click on icons to see the action.
Let’s start with vector map, I got a nice map of brazil from the amcharts , that I modified in illustrator and placed “you are here” with three Capitol icon.
First embed the SVG to HTML, there are numerous way to embed and load SVG to HTML. Below is the SVG code that i am using
<svg version="1.1" id="Layer_1" xmlns:amcharts="http://amcharts.com/ammap" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="612px" height="792px" viewBox="0 0 612 792" enable-background="new 0 0 612 792" xml:space="preserve"> <symbol id="Capitol" viewBox="-5.78 -5.781 11.559 11.563"> <g> <path fill="#DA2128" d="M5.779-0.002c0-3.191-2.588-5.779-5.779-5.779s-5.78,2.588-5.78,5.779c0,3.193,2.588,5.781,5.78,5.781 S5.779,3.191,5.779-0.002z"/> <g> <g> <polygon fill="#FFFFFF" points="3.434,-4.676 0.008,-2.244 -3.382,-4.744 -2.321,-0.718 -5.554,1.718 -1.525,1.863 -0.08,5.781 1.436,1.896 5.476,1.83 2.299,-0.663 "/> </g> </g> </g> </symbol> <symbol id="School" viewBox="-3.836 -6.788 7.672 13.576"> <g> <polygon fill="#DA2128" points="3.571,-6.788 -3.836,-6.788 -3.836,0.62 3.571,0.62 "/> <polygon fill="#DA2128" points="-1.639,2.702 1.131,2.702 1.131,0.604 -1.639,0.604 "/> <polygon fill="#DA2128" points="-0.482,6.788 -0.027,6.739 3.836,6.005 -0.051,4.882 -0.051,1.138 -0.482,1.138 "/> </g> </symbol> <defs> <amcharts:ammap bottomLatitude="-33.743888" rightLongitude="-34.789914" topLatitude="5.275696" leftLongitude="-74.008595" projection="mercator"> </amcharts:ammap> </defs> <g> <path id="BR-AC" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> <path id="BR-AL" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> <path id="BR-AM" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> <path id="BR-AP" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> <path id="BR-BA" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> <path id="BR-CE" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> <path id="BR-ES" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> <path id="BR-GO" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> <path id="BR-MA" fill="#CCCCCC" stroke="#FFFFFF" stroke-width="0.5" d=""/> </g> <use xlink:href="#Capitol" id="shape3" class="hit" width="11.559" height="11.563" x="-5.78" y="-5.781" transform="matrix(1.9375 0 0 -1.9375 82.0005 170.6401)" overflow="visible"/> <use xlink:href="#Capitol" id="shape2" class="hit" width="11.559" height="11.563" x="-5.78" y="-5.781" transform="matrix(1.9375 0 0 -1.9375 387.668 432.9512)" overflow="visible"/> <use xlink:href="#Capitol" id="shape1" class="hit" width="11.559" height="11.563" x="-5.78" y="-5.781" transform="matrix(1.9375 0 0 -1.9375 480.8457 363.9702)" overflow="visible"/> <use xlink:href="#School" width="7.672" height="13.576" x="-3.836" y="-6.788" transform="matrix(1.6588 0 0 -1.6588 334.7695 148.77)" overflow="visible"/> <g id="shape1walkpath"></g> <g id="shape2walkpath"></g> <g id="shape3walkpath"></g> </svg>
As you see in the above code that “you are here icon” has school ID and capitol has “hit” class. With “hit” class, we assign click interactivity to SVG element. Before end of SVG code, you may notice that we have drawn an empty < g > element with id, this help us in drawing vector animation or walkpath.
Open any vector program, I used adobe illustrator and draw a path over that map and after finishing path drawing, save it as SVG format. For tutorial, I drawn three vector path and captured coordinates from the SVG using notepad and assigned it to javascript object. Code is below
var pathObj = { "shape3walkpath": { "strokepath": [ { "path": "M325.39,153.886c0,0-139.89-79.156-227.89,10.729", "duration": 1000 } ], "dimensions": { "width": 612, "height": 792 } }, "shape1walkpath": { "strokepath": [ { "path": "M341.133,156.85c0,0,164.967,29.16,144.167,208.405", "duration": 800 } ], "dimensions": { "width": 612, "height": 792 } }, "shape2walkpath": { "strokepath": [ { "path": "M332.052,159.443 275.21,272.5 342.32,331.17 371.03,366.27 381.37,403.5 387.592,411.38", "duration": 2000 } ], "dimensions": { "width": 612, "height": 792 } } };
We may also capture these data in json and load it via jquery but for the sake of tutorial simplicity, I put that on an object format. As I mentioned earlier that “hit” class call lazy line painter method to draw walk path. Here is the code.
$(".hit").click(function(e){ $($(this).attr('id')).lazylinepainter( { "svgData": pathObj, "strokeWidth": 2, "strokeColor": "#e09b99" }).lazylinepainter('paint'); })
Erase Walkpath Erasing any vector line is simple, follow these lines.
$(object).lazylinepainter('erase');













