#!/usr/bin/perl -w # Example 7-3. The bouncing ball. use strict; use XML::Writer; # Used to write the SVG output my ($width, $height) = (300, 300); my $writer = XML::Writer->new(); $writer->setDataMode(1); # Auto insert newlines $writer->setDataIndent(2); # Auto indent $writer->startTag('svg', height => $height, width => $width, 'xmlns:xlink' => 'http://www.w3.org/1999/Xlink'); $writer->startTag('circle', id => 'ball', r => 10, fill => '#FF0000'); $writer->emptyTag('animateMotion', calcMode=> 'spline', dur => "10s", path => make_bounce_path(300, 200, 4), repeatCount => "indefinite"); $writer->emptyTag('animate', attributeName => "r", from => 10, to => 50, dur => "10s", repeatCount => "indefinite" ); $writer->endTag('circle'); $writer->endTag('svg'); sub make_bounce_path { my ($ground, $bounce_height, $bounces) = @_; my $pi = atan2(1,1) * 4; my $points = "M0,$height "; my $y; for (my $x=1; $x < $width; $x+=5) { $y = int($ground - abs(($bounce_height * sin(($x/($width*2/$bounces)) * 2 * $pi)))); $points .= "L$x,$y "; $ground -= 40/($width/5); $height -= 40/($width/5); } return $points; }