Logged out.

Learn
  SVG Forum
  Tutorials
  Resources 

Create
  Elf Workshop
  Downloads 

SVG Elves
  About
  Feedback
 


SVG 1.1
DOM Home
XML Home
CSS Home
XSLT Home
XPATH


home <%=sCrumb%>

Swarm, Lesson 1 by Paul Hounshell of the Wood Elves

We all know SVG is a wonderful format for graphics (otherwise we wouldn't be here). However, the true power of SVG isn't released until combined with scripting. The support for the XML DOM written into the SVG plugin by Adobe is very extensive, and short of the few blunders outlined here, very easy to use.

Before we begin, you will need to know a good amount of Javascript. You have to be one of the people who knows how to write your own functions, not simply scavenging them (which I do not have anything against). You also need to know good programming practices and a good understanding of algebra.

The project we will build today is called "Swarm." The intended effect is that of several bugs crawling or flying around your plugin window. This is the first part, where we deal with getting objects to move.

First you should have a good understanding of the XML DOM and what it is capable of. For this please read the XML DOM Discussion [coming soon]. We will be using routines which deal with selecting the correct svg element and manipulating it's attributes.

The very first step in obtaining your SVG plugin is a tricky one. However, once you learn this trick, you can use it with ease.

We need to be able to access the root of the SVG document. This is where all navigation through xml elements is done and thus a very vital step. There are two ways to accomplish this.

1.) Browser Code.

This method of detection basically uses the name specified by your browser to navigate to the SVG document. Consider the following HTML fragment:

<html>
<body>
<embed name="Example1" width="..." height="..." SRC="..." TYPE="image/svg-xml">
</body>
</html>

The Javascript to navigate to the SVG element is rather trivial. It is simply

var foo = document.Example1.getSVGDocument()

However, in general this is a bad way to go about doing this. It assumes that your svg is embedded in an HTML page (which it may not always be) and that the name is static. You may be in for a huge surprise if you copy that SVG document to another page and have to spend hours figuring out why the Javascript doesn't work. Furthermore, several copies of the same object then require unique names and code to run properly. This method does, however, work in certain circumstances (e.g. Javascript from one SVG doc modifying another SVG doc)

2.) SVG Initialization Event

This Javascript is far more stable and is used completely independantlt of the browser. If you have a standalone SVG document which doesn't interact with it's surroudings, this is the way to go

<svg onload="Initialize(evt)">
<defs>
<script>
<![CDATA[

var SVGDocument = null

function Initialize(LoadEvent)
{
SVGDocument = LoadEvent.getTarget().getOwnerDocument();
}
]]>
</script>
</defs>
</svg>

What happens here is that once the SVG has finished loading, the onload event is fired. We capture that event and send it to our routine - Initialize(). Initialize then gets the target of that event (a child of the SVG document) and from that, extrapolates the document object.

I think that's enough for today. Honestly, that's the trickiest part of the entire ordeal. From there, any further dfficulty is due to level of intelligence you give to the bugs.

Swarm, Lesson 2


     
 

Home | Add an Article, Tutorial, or Tool | SVG Tutorial | SVG

Professional Marketing Consulting Services

Stephen W. Thomas's BizTalk Server Blog | FunWadi.com Free Music

New Eastside | Moving Companies | mini itx | Australian Horse Racing

Matrix Sunglasses | Car Leasing UK | Buy Your Car Used Cars, Car Lease and Contract Hire

Used Car Trader - Used Cars | Electric Scooters, Gas Scooters, Pocket Bike Reviews

Search for Sapphire Jewelry Deals | Columbia MO Real Estate

Tradebit: Download Guides, Service Manuals, and more

Info Moving relocation service and moving information

Computer Support
Get professional computer support from Century Computing. Professional IT and computer support right when you need it.

Web Page Design
With our novel approach to web page design, you can alter the contents of your website. Click here to use our extraordinary service!

© 2000 - 2008 SVG Elves