]>
iEval git - svg-spritemaker.git/blob - lib/SVG/SpriteMaker.pm
1 package SVG
::SpriteMaker
;
7 our $VERSION = '0.001';
8 our @EXPORT = qw
/make_sprite make_sprite_prefix/;
9 our @EXPORT_OK = @EXPORT;
11 use parent qw
/Exporter/;
17 use SVG
-indent
=> '', -elsep
=> '';
21 my ($prefix, @images) = @_;
22 my $sub = ref $prefix eq 'CODE' ?
$prefix : sub {
23 my $base = scalar fileparse
$_[0], qr/[.].*/;
26 my $sprite = SVG
->new(-inline
=> 1);
27 my $parser = SVG
::Parser
->new;
28 @images = map {[ $sub->($_) => $parser->parse_file($_) ]} @images;
29 my ($x, $mh) = (0, 0);
32 my ($img, $doc) = @
$_;
33 my $svg = $doc->getFirstChild;
34 my ($w) = $svg->attr('width') =~ /([0-9.]*)/ or carp
"Image $img has no width";
35 my ($h) = $svg->attr('height') =~ /([0-9.]*)/ or carp
"Image $img has no height";
38 $svg->attr(version
=> undef);
39 my $view = $sprite->view(id
=> $img, viewBox
=> "$x 0 $w $h");
41 $view->getParent->insertAfter($svg, $view);
44 # Keep a reference to the documents to prevent garbage collection
45 $sprite->{'--images'} = \
@images;
46 $sprite->getFirstChild->attr(viewBox
=> "0 0 $x $mh");
57 SVG::SpriteMaker - Combine several SVG images into a single SVG sprite
61 use File::Slurp qw/write_file/;
63 my $sprite = make_sprite img => '1.svg', '2.svg', '3.svg';
64 write_file 'sprite.svg', $sprite->xmlify;
65 # You can now use <img src="sprite.svg#img-1" alt="...">
67 my @images = <dir/*>; # dir/ImageA.svg dir/ImageB.svg
68 $sprite = make_sprite sub {
69 my ($name) = $_[0] =~ m,/([^/.]*),;
71 }, @images; # Sprite will have identifiers #IMAGEA #IMAGEB
75 A SVG sprite is a SVG image that contains several smaller images that
76 can be referred to using fragment identifiers. For example, this HTML
79 <img src="/img/cat.svg" alt="A cat">
80 <img src="/img/dog.svg" alt="A dog">
81 <img src="/img/horse.svg" alt="A horse">
85 <img src="/img/sprite.svg#cat" alt="A cat">
86 <img src="/img/sprite.svg#dog" alt="A dog">
87 <img src="/img/sprite.svg#horse" alt="A horse">
89 This module exports a single function:
91 =head2 B<make_sprite>(I<$prefix>|I<$coderef>, I<@files>)
93 Takes a list of filenames, combines them and returns the resulting
94 sprite as a L<SVG> object. Each SVG must have width and height
95 attributes whose values are in pixels.
97 If the first argument is a coderef, it will be called with each
98 filename as a single argument and it should return the desired
101 If the first argument is not a coderef, the following coderef will be
105 my $base = scalar fileparse $_[0], qr/\..*/s;
109 where I<$prefix> is the value of the first argument.
113 L<svg-sprite>, L<https://css-tricks.com/svg-fragment-identifiers-work/>
117 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
119 =head1 COPYRIGHT AND LICENSE
121 Copyright (C) 2015 by Marius Gavrilescu
123 This library is free software; you can redistribute it and/or modify
124 it under the same terms as Perl itself, either Perl version 5.20.2 or,
125 at your option, any later version of Perl 5 you may have available.
This page took 0.058088 seconds and 5 git commands to generate.