NAME SVG - Perl extension for generating Scalable Vector Graphics (SVG) documents.…
- User
NAME
SVG - Perl extension for generating Scalable Vector Graphics (SVG) documents. SYNOPSIS
#!/usr/bin/perl use strict; use warnings; use SVG; # create an SVG object my $svg= SVG->new( width => 200, height => 200); # use explicit element constructor to generate a group element my $y = $svg->group( id => 'group_y', style => { stroke => 'red', fill => 'green' }, ); # add a circle to the group $y->circle( cx => 100, cy => 100, r => 50, id => 'circle_in_group_y' ); # or, use the generic 'tag' method to generate a group element by name my $z = $svg->tag('g', id => 'group_z', style => { stroke => 'rgb(100,200,50)', fill => 'rgb(10,100,150)' } ); # create and add a circle using the generic 'tag' method $z->tag('circle', cx => 50, cy => 50, r => 100, id => 'circle_in_group_z'); # create an anchor on a rectangle within a group within the group z my $k = $z->anchor( id => 'anchor_k', -href => 'http://test.hackmare.com/', target => 'new_window_0' )->rectangle( x => 20, y => 50, width => 20, height => 30, rx => 10, ry => 5, id => 'rect_k_in_anchor_k_in_group_z' ); # now render the SVG object, implicitly use svg namespace print $svg->xmlify; # or render a child node of the SVG object without rendering the entire object print $k->xmlify; #renders the anchor $k above containing a rectangle, but does not #render any of the ancestor nodes of $k # or, explicitly use svg namespace and generate a document with its own DTD print $svg->xmlify(-namespace=>'svg'); # or, explicitly use svg namespace and generate an inline docunent print $svg->xmlify( -namespace => "svg", -pubid => "-//W3C//DTD SVG 1.0//EN", -inline => 1 );See the other modules in this
- Assistant
The provided script demonstrates how to use the Perl module SVG to generate Scalable Vector Graphics. This script covers various features of the SVG module, including creating SVG elements such as groups, circles, rectangles, and anchors (links), applying styles, and rendering the SVG document. Below is a detailed breakdown of what the script does:
-
Setting Up: It starts by initializing the script with
use strict;anduse warnings;, which are good practices in Perl for catching potential issues. It then includes the SVG module withuse SVG;. -
Creating an SVG Object: An SVG object is created with a specified width and height. This object acts as the container for all SVG elements created subsequently.
-
Creating and Styling Groups:
- The script demonstrates two ways to create a group (
<g>) element. The first method uses thegroupmethod directly with a specified ID and style. A group element is useful for applying transformations or styles collectively to its child elements. - The second method uses the generic
tagmethod to create a group element, showcasing the flexibility of creating various SVG elements dynamically with different properties.
- The script demonstrates two ways to create a group (
-
Adding Elements to Groups:
- A circle is added to the first group (identified as
group_y) with specified center coordinates (cx,cy), radius (r), and an ID. - Similarly, another circle is created within the second group (
group_z) using thetagmethod. This shows how you can nest SVG elements within group elements.
- A circle is added to the first group (identified as
-
Creating Links and Rectangles:
- An example is given of creating a clickable rectangle by wrapping a
rectangleelement within ananchor(or<a>) element, which is nested within the second group. The anchor element is created with specified attributes likeid,href(the hyperlink reference), andtargetattribute for specifying where to open the linked document.
- An example is given of creating a clickable rectangle by wrapping a
-
Rendering SVG:
- The script illustrates different ways of rendering the SVG content to XML format. The
xmlifymethod of SVG objects can be used to output the SVG document. You can render the entire SVG document or specific elements like the anchor and its children. - Further examples show how to use the
xmlifymethod to include an SVG namespace, specify a DTD (Document Type Definition), or create an inline SVG document by using various parameters like-namespace,-pubid, and-inline.
- The script illustrates different ways of rendering the SVG content to XML format. The
These examples effectively teach how to create and manipulate SVG documents with Perl's SVG module, rendering these SVG documents or elements to XML format for web use or further processing. This module is powerful for dynamically generating graphics such as charts, diagrams, or any illustrations where SVG is suitable due to its scalability and performance in web environments.
-