ROASP:
| |
An Open-Source contribution from RO IT Systems |
|
Perl SVG Tutorials |
Home | Free Code Samples | SVG Module Documentation | SVG Tutorials | SVG Gallery | Forum & Discussion | SVG Perl Modules | SVG-Perl Articles | External Links | Local Copies of Modules (Win32 & CPAN) | YASB: A Perl SVG Browser |
In this tutorial, we are going to discuss how we generate an svg image, and use that image to fill another image's background. This code was written as an example for tesselation for a question which came up in the svg-developer discussion on Yahoo in 2001.
#!/usr/bin/perl -w
If our ISP does not have SVG.pm and we installed it locally, then we point to the module location and to the location of the SVG/ directory where the module resources live. In this particular case, my ISP does not support SVG (this is an inexpensive hosted service) so I explicitly point to the SVG.pm and SVG/*.pm modules which are located one directory up from where the script is being run.
BEGIN {
push @INC , '../';
push @INC , '../SVG';
}
use strict;
use SVG;
my $svg = SVG->new(width=>"100%", height=>"100%");
We are going to generate two triangle images, overlap them, and use them as the basis for the fill pattern for the broken-window polygon.
my $g = $svg->group(style=>{"fill-rule"=>"evenodd","stroke-linejoin"=>"round", "stroke-linecap"=>"round"});
Define definition 1
my $d1 = $g->defs();
my $path = $svg->get_path(-type=>"path", x=>[0,90,60], y=>[0,60,90], -closed=>1);
my $d1g1p = $d1->group(id=>"Tess0p")->path(%$path);
my $d1g0 = $d1->group( id=>"Tess0", fill=>"rgb(255,255,0)", stroke=>"none")->use(-href=>"#Tess0p");
my $d1g1 = $d1->group(id=>"Tess1", fill=>"none", stroke=>"rgb(0,0,0)", "stroke-width"=>"2.413")->use(-href=>"#Tess0p");
$path = $svg->get_path(-type=>"path", x=>[15,75,50], y=>[15,50,75], -closed=>1);
my $d1g2p = $d1->group( id=>"Tess2p")->path(%$path);
my $d1g2 = $d1->group(id=>"Tess2", fill=>"rgb(255,170,255)", stroke=>"none")->use(-href=>"#Tess2p");
my $d1g3 = $d1->group( id=>"Tess3",
fill=>"none",
stroke=>"rgb(0,0,0)",
"stroke-width"=>"2.413")
->use(-href=>"#Tess2p");
my $d2p2 = $g->defs()->
pattern(id=>"TessPattern",
patternUnits=>"userSpaceOnUse", x=>"0", y=>"0",
width=>"100", height=>"100", viewBox=>"0 0 100 100",
overflow=>"visible");
We create 4 groups and each employs group uses a predefinde group by calling by that group's ID.
$d2p2->group()->use(-href=>"#Tess0");
$d2p2->group()->use(-href=>"#Tess1");
$d2p2->group()->use(-href=>"#Tess2");
$d2p2->group()->use(-href=>"#Tess3");
$svg->comment('Now let us define the polygon with the fill inside it refered to by url reference');
Define the container image. We just use a polygon here for coolness. This could have just as easily been:
$svg->rect(x=>"100",y=>"200",width="100",height=>"100",fill=>"url(#TessPattern)",
stroke=>"black");
But to be cool we will do a quasi-random broken-window polygon that looks complicated. Notice that here we are explicitly defining the polygon points rather than having the module give them to us,a as it could (see the $svg->get_path() method.
#broken-window-pane polygon which contains the fill we designed
$svg->polygon(id=>"broken-window-pane", "points=>"163.816,337.5 ".
(140+rand(20)).
",".
(400+rand(60)).
" 234.868,344.079 334.868,428.289 291.447,299.342 480.921,284.868 326.974,".(160+rand(60)).
" 344.079,30.9211 232.237,167.763 123.026,29.6053 150.658,191.447 37.5,94.0789 ".
(100+rand(10)).
','.
(200+rand(40)).
" 7.23684,288.816 84.8684,287.5 33.5526,333.553 111.184,320.395 82.2368,448.026",
fill=>"url(#TessPattern)",
stroke=>"black");
Now, let's add some text to this whole thing to make it more interesting, and so the user can see the source code
$svg->text(x=>100,y=>20)->cdata("Using A tessalated pattern to create a fill");
$svg->anchor(-href=>'http://roasp.com/tutorial/source/tessalate.txt')->text(x=>50,y=>400, fill=>'red' )->cdata("View Script Source");
Let us not forget to send the content type to the browser
print "Content-type: image/svg+xml\n\n";
Finally, we render the svg and send it to the output hsing a print command.
print $svg->xmlify;
For more information, please send the author an email: Ronan Oger
Click here for the source code, and Click here to see the result.
Previous Tutorial | Tutorial Home | Next Tutorial
| SVG Home | © 2002, 2003 RO IT Systems GmbH | Site Info |