I’ve been searching for ways to run scripts for my SVG.
But all the things I got don’t match up! And it doesn’t contain enough information why each set of codes were used. For example, one used event.target, another had event.getTarget(), and another had event.target.firstchild.data.
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<path d="M150 0 L75 200 L225 200 Z" />
</svg>
This is an example of a path svg, right? What I need is to get those coordinates, probably put it in a variable, and use it as coordinates for another svg. So how can I do that? Another thing is how can I change those coordinates by entering numbers in an interface?
It sounds like you may have four questions:
<path>element from script?<path>element from script?Let’s tackle them one at a time:
How do I embed script inside an SVG file?
As described in the SVG specification you can place a
<script>element in your document to contain JavaScript code. According to the latest SVG specifications, you do not need to specify atypeattribute for your script. It will default totype="application/ecmascript"."text/javascript","text/ecmascript"(specified in SVG 1.1),"application/javascript", and"application/x-javascript". I do not have detailed information on browser support for all of these, or for omitting thetypeattribute altogether. I have always had good success withtext/javascript.As with HTML, you may either put the script code directly in the document, or you may reference an external file. When doing the latter, you must use an
hrefattribute (notsrc) for the URI, with the attribute in thexlinknamespace.How do I run script inside an SVG file?
As with HTML, code included in your SVG document will be run as soon as it is encountered. If you place your
<script>element above the rest of your document (as you might when putting<script>in the<head>of an HTML document) then none of your document elements will be available when your code is running.The simplest way to avoid this is to place your
<script>elements at the bottom of your document:Alternatively, you can create a callback function at the top of your document that is only invoked when the rest of the document is ready:
How do I access data for a
<path>element from script?There are two ways to access most information about elements in SVG: you can either access the attribute as a string through the standard DOM Level 1 Core method
getAttribute(), or you can use the SVG DOM objects and methods. Let’s look at both:Accessing path data through
getAttribute()Using
getAttribute()returns the same string as you would see when you view source:<path>data, this can be excruciating.Accessing path data through SVG DOM methods
The above script produces the following output:
Pros: path data is parsed for you; you get exact numbers from the API itself; using
normalizedPathSegListtakes relative commands and makes them absolute for you; if SMIL animation is changing the path data, using the non-animated pathSegList can give you access to the base, non-animated information not available viagetAttribute().Cons: Sweet chimpunks a-flame, look at that code! And that doesn’t even handle all the possible path segments available.
Because it can be hard to read the W3C specs for SVG DOM, many years ago I created an online tool for browsing what properties and objects exist. You may use it here: http://objjob.phrogz.net/svg/hierarchy
How can I manipulate data for a
<path>element from scriptSimilar to the above, you can either create a new string and use
setAttribute()to shove it onto the object, or you can manipulate the SVG DOM.Manipulating path data using
setAttribute()Manipulating path data using SVG DOM
In general, you just have to modify the properties of the various
SVGPathSegsubclass instances; the changes are made immediately in the DOM. (With the above example, the original triangle is skewed as the last point is moved up slightly.)When you need to create new path segments, you need to use methods like
var newSegment = myPath.createSVGPathSegArcAbs(100,200,10,10,Math.PI/2,true,false)and then use one of the methods to stick this segment into the list, e.g.segments.appendItem(newSegment).