19 May 2022 17:03:48 UTC
Distribution: SVG
Module version: 2.87
Source (raw)
Browse (raw)
Changes
How to Contribute
Repository
Issues (11)
Testers (970 / 0 / 0)
Kwalitee
Bus factor: 1
49.97% Coverage
License: perl_5
Perl: v5.6.0
Activity
24 month
Tools
Download (53.99KB)
MetaCPAN Explorer
Permissions
Subscribe to distribution
code
Dependencies
Exporter
Scalar::Util
constant
parent
strict
warnings
and possibly others
Reverse dependencies
CPAN Testers List
Dependency graph
Permalinks
This version
Latest version
++ed by:
DELON EMILPER CONTRA SHLOMIF KEEDI
7 PAUSE users
11 non-PAUSE users
Mohammad S Anwar /
SVG-2.87
/ SVG
Contents
code
Assistant
SVG is a Perl module that provides an easy and flexible way to generate Scalable Vector Graphics (SVG) documents. SVG is a markup language for describing two-dimensional graphics applications and images, and a set of related graphics script interfaces. SVG documents are pure XML, allowing for the creation of graphics that can be searched, indexed, scripted, and compressed. Moreover, SVG is scalable and can be rendered at any resolution.
User
#!/usr/bin/perl
use strict;
use warnings;
use SVG;
create an SVG object with a size of 100x100 pixels
This Perl script demonstrates how to use the SVG module to create SVG graphics, including a square, a circle, and a triangle, each with distinct colors and styles. Here's a breakdown of what the script does:
Setup: The script starts by including the necessary strict and warnings pragmas for good practice, and it loads the SVG module to enable SVG generation.
SVG makes it straightforward for Perl developers to programmatically create vector graphics, which can be used on websites, in multimedia presentations, or for graphical data visualization. The module is designed to be both simple for beginners and powerful enough for advanced users, providing methods to generate every valid SVG element, apply transformations, and manage styles.
Key features of the SVG module include:
Easy creation of SVG documents: Developers can start creating SVG content with minimal setup, using intuitive methods to add and manipulate graphical elements.
Conversion capabilities: While primarily focused on creating SVG format documents, there are examples provided for converting SVG into other raster image formats (such as PNG), using external tools.
Extensibility: The module supports all SVG tags and their attributes, allowing for the creation of complex graphics with gradients, patterns, animations, and more.
Support for interactivity and animation: SVG files can contain animations and respond to user events, which can be defined directly within the module.
DOM-like manipulation: SVG::DOM methods allow developers to navigate and manipulate the elements of the SVG document in a way that is familiar to those who have used the Document Object Model in web development.
In summary, the SVG Perl module is a powerful and flexible tool for creating and manipulating SVG documents programmatically. It enables developers to generate complex graphics from Perl scripts, making it an invaluable tool for web developers, data scientists, and anyone who needs to produce scalable, high-quality vector graphics programmatically.
In order to create a triangle we are going to create a polygon
To make it easy to create various path based constructs, SVG.pm
provides a "get_path" method that, give a series of coordinates
and a type, return the respective data structure that is needed
for SVG.
my path=svg->get_path(
x => [40, 60, 80],
y => [40, 6, 40],
-type => 'polygon');
Then we use that data structure to create a polygon
now render the SVG object, implicitly use svg namespace
print $svg->xmlify, "\n";
Create an SVG Object: It creates an SVG object with a specified width and height of 100x100 pixels. This object serves as the canvas for drawing shapes.
Draw a Rectangle (Square): A square is drawn by specifying a rectangle with equal width and height (40 pixels) and its top-left corner positioned at (40, 50) on the SVG canvas. The square is styled to be filled with green ('rgb(0, 255, 0)') and has a black stroke around it.
Draw a Circle: A circle is added with a center positioned at (40, 40) and a radius of 20 pixels. The circle is filled with red ('rgb(255, 0, 0)') and also has a black stroke around it, similar to the square.
Draw a Triangle (Polygon): To create a triangle, the script uses the get_path method with specified x and y coordinates that represent the vertices of the triangle. The method is told to create a polygon type path. The resulting path is then used to draw a polygon on the SVG canvas, which appears as a triangle filled with blue ('rgb(0, 0, 255)') and outlined with a black stroke.
Render and Print the SVG: Finally, the xmlify method is called on the SVG object to generate the SVG XML string representation of the graphic. This string is printed to STDOUT, which could be redirected to an SVG file for viewing in a web browser or SVG viewer.
This script effectively demonstrates the basics of creating SVG graphics with the SVG Perl module. You can experiment with different shapes, styles, and positions by adjusting the parameters provided to the drawing methods (rectangle, circle, polygon) and by exploring other methods provided by the SVG module.