04.07.2013 Views

Building Web Applications with SVG - Cdn.oreilly.com

Building Web Applications with SVG - Cdn.oreilly.com

Building Web Applications with SVG - Cdn.oreilly.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

example 9: Using a Filter to Create pond ripples over an Image<br />

This example shows some of the more advanced aspects of scripted animated gradients used in conjunction<br />

<strong>with</strong> filters to distort an image. The animated version shows ripples (customizable by the user<br />

using HTML input elements) moving across an image—much as ripples would disrupt the reflection of<br />

an image in a pond.<br />

Here’s how it’s done:<br />

■■ Creating concentric circles The circles are created <strong>with</strong> script. A gradient can have different<br />

color bands, called stops, defined <strong>with</strong>in it. In this case, a series of concentric stops (orange<br />

and green) is created through script and added to a gradient, which is then applied to an<br />

ellipse under the image of a face.<br />

■■ Animating concentric circles The radius (or offset) of each stop is then modified gradually<br />

through subtle changes in a setTimeout loop defined in JavaScript. Interestingly, the script for<br />

this example resides in the HTML rather than <strong>with</strong>in the <strong>SVG</strong>, and the <strong>SVG</strong> DOM is accessed<br />

from there.<br />

■<br />

■ Distorting the image Once the above two things have been done, the rest is rather easy.<br />

A filter is created that brings in both the concentric circles and the face as layers, and then distortion<br />

is applied through a filter effect known as , using the red channel<br />

of the gradient to determine the degree of distortion associated <strong>with</strong> the image. Because<br />

green doesn’t contain red but orange does, provides the differential<br />

distortion in concentric bands.<br />

16 <strong>Building</strong> <strong>Web</strong> <strong>Applications</strong> <strong>with</strong> <strong>SVG</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!