#
SVG Class Reference
The Victor::SVG
(and Victor::SVGBase
) class provides a foundation for
creating SVG graphics in Ruby. It supports custom templates, CSS styling, and
flexible SVG element generation.
#
Initialization
#
#initialize
Creates a new instance of SVG
.
svg = Victor::SVGBase.new viewBox: '0 0 200 200'
svg.circle cx: '100', cy: '100', r: '50'
# Initialize with a block
svg = Victor::SVGBase.new viewBox: '0 0 200 200' do
circle cx: '100', cy: '100', r: '50'
end
#
Building
#
#tag
/ #element
Adds an SVG element to the content.
- If
value
is a Hash, it's treated asattributes
. - If
name
ends with!
, the content is not XML-escaped. - This method is the target for
method_missing
, enabling dynamic element names.
svg.element :rect, x: '10', y: '10', width: '100', height: '100'
svg.tag :text, 'Hello, World!', x: '50', y: '50'
#
#build
Evaluates a block in the context of the SVG instance, allowing dynamic SVG content creation.
svg.build do
circle cx: '100', cy: '100', r: '50'
text 'Centered Text', x: '100', y: '100'
end
#
#append
/ #embed
/ #<<
Adds content from another source to the SVG. This can be used to compose SVG using other SVG objects.
host = SVG.new
guest = SVG.new
host << guest
host.append guest
host.embed guest
#
#css
Manages CSS definitions for the SVG.
svg.css = { '.hint': { color: :red, font_size: '12px' } }
svg['.hint'] = { color: :red, font_size: '12px' }
#
Rendering / Saving
#
#save
Saves the SVG content to a file.
svg.save 'outfile'
svg.save 'outfile', template: :minimal
svg.save 'outfile', template: 'path/to/template'
svg.save 'outfile.svg', template: 'path/to/template', glue: ''
#
#render
Renders the full SVG string, including its CSS and SVG header.
puts svg.render
#
#to_s
Converts the SVG content to a string. This method is used internally to join the
SVG content with the specified glue
.
Note
The string output provided by #to_s
does not include the CSS or the SVG header.
If you want the full SVG string, see #render
puts svg
#
Internal / Special Use
#
#setup
Sets up the initial attributes for the SVG.
This method is used internally, and by the DSL and typically not required for basic usage.
#
#content
Manages the array of SVG lines. This is used internally to assemble the SVG content.
#
#svg_attributes
Holds the attributes for the SVG element, such as width
, height
, and other
SVG attributes.
svg.svg_attributes.width = '200'
svg.svg_attributes.height = '200'
#
#template
Specifies the SVG template to use. This is rarely needed as the default template is often sufficient.
svg.template = :minimal
svg.template = 'path/to/template'
#
#glue
Specifies the string to use between SVG elements. This is rarely needed as the default glue is often sufficient.