Building Web Applications with SVG - Cdn.oreilly.com
Building Web Applications with SVG - Cdn.oreilly.com
Building Web Applications with SVG - Cdn.oreilly.com
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
■■ The movement of the mountains The foreground layers are simply shifted leftward more<br />
quickly than the layers in the back. Each array has its first element removed so that another<br />
random element can be added onto the end of the array <strong>with</strong>out the array be<strong>com</strong>ing arbitrarily<br />
large. Any memory of what has happened is systematically purged.<br />
example 2: equidistant positioning points along a Bézier Curve<br />
The mathematics of Bézier curves, while quite accessible to a mathematician, are not trivial. Bézier<br />
curves were, after all, not discovered until 1959 (see http://en.wikipedia.org/wiki/B%C3%A9zier_curve),<br />
130 years after Évariste Galois resolved the theory of roots of polynomials and laid the foundation for<br />
much of the algebra of the 19th and 20th centuries. Fortunately, <strong>SVG</strong> (following the lead of Adobe<br />
Illustrator 88) gives direct and intuitive access to these wonderfully expressive curves in terms of<br />
the ability to draw, measure, subdivide, orient, and animate them. In this example (visible at http://<br />
srufaculty.sru.edu/david.dailey/svg/curve.svg), the curve is drawn <strong>with</strong> a simple set of markup <strong>com</strong>mands,<br />
and each time the user clicks the curve or near it, JavaScript is used to measure the curve<br />
and divide it into an increasingly larger number of parts, <strong>with</strong> the option of animating the process<br />
ultimately being offered to the user.<br />
Here’s how it’s done:<br />
■■ Drawing the curve The markup used is quite simple:<br />
<br />
Note The drawing of <strong>SVG</strong> paths is one of the most powerful and expressive aspects<br />
of the language; it’s covered in Chapter 2.<br />
■<br />
■ Measuring and subdividing the curve The JavaScript language binding of <strong>SVG</strong> allows you<br />
to interrogate properties of things that have been drawn either through markup or dynamically,<br />
and to manipulate them using methods. In this case, we are using two function calls:<br />
L = B.getTotalLength(); and P = B.getPointAtLength(L * i / n);. The first measures the path, B,<br />
and returns a numeric value; the second returns a point (an object <strong>with</strong> both x and y values)<br />
a given fraction of the distance along B. Script is then used to create new ellipses of different<br />
colors at those fractional mileposts.<br />
8 <strong>Building</strong> <strong>Web</strong> <strong>Applications</strong> <strong>with</strong> <strong>SVG</strong>