Class: SVG::Node

Inherits:
XML::Node
  • Object
show all
Includes:
NodeHelpers
Defined in:
lib/svg/node.rb,
lib/svg/node/path.rb

Overview

Wrapper class around XML::Node, defining the base SVG methods available to all nodes, such as #scale and #transform.

Examples:

SVG::Node.new "svg"
SVG::Node.new "svg", width: 100
SVG::Node.new "path" do |path|
  path.scale 1, -1
  path["width"] = 100
end

Direct Known Subclasses

Path

Defined Under Namespace

Classes: Path

Instance Method Summary (collapse)

Methods included from NodeHelpers

#clip_path, #g, #path, #rect

Constructor Details

- (Node) initialize(name, properties = {}) {|SVG::Node| ... }

Creates a new instance of an SVG node with the given name.

Parameters:

  • name (String)

    the name for this node

  • properties (Hash) (defaults to: {})

    a hash of properties to set on the node

Yields:



31
32
33
34
35
36
37
38
39
# File 'lib/svg/node.rb', line 31

def initialize(name, properties = {})
  super name

  properties.each do |property, value|
    self[property] = value
  end

  yield self if block_given?
end

Instance Method Details

- (Object) []=(attribute, value)

Sets the value of an attribute, ensuring that both the key and the value have been converted to strings.

Parameters:

  • attribute (Symbol, String)

    the attribute's name

  • value (SVG::Node, #to_s)

    a node with an id to be turned into a reference, or an object responding to #to_s.



51
52
53
54
55
56
57
58
59
# File 'lib/svg/node.rb', line 51

def []=(attribute, value)
  case value
    when SVG::Node
      super attribute.to_s, "url(##{value[:id]})"

    else
      super attribute.to_s, value.to_s
  end
end

- (SVG::Transform) current_transformation

Returns the current transformation object

Returns:



42
43
44
# File 'lib/svg/node.rb', line 42

def current_transformation
  @current_transformation ||= SVG::Transform.new
end

- (Object) scale(xscale, yscale = xscale)

Scales the node based on the provided x and y scale factors.

See: www.w3.org/TR/SVG/coords.html#TransformAttribute

Parameters:

  • xscale

    the x-axis scale factor

  • yscale (defaults to: xscale)

    the y-axis scale factor



67
68
69
70
# File 'lib/svg/node.rb', line 67

def scale(xscale, yscale = xscale)
  current_transformation.scale(xscale, yscale)
  self["transform"] = current_transformation.to_s
end

- (Object) translate(x, y = 0)

Translates the node based on the provided x and y positions.

See: www.w3.org/TR/SVG/coords.html#TransformAttribute

Parameters:

  • x

    the x position

  • y (defaults to: 0)

    the y position



78
79
80
81
# File 'lib/svg/node.rb', line 78

def translate(x, y = 0)
  current_transformation.translate(x, y)
  self["transform"] = current_transformation.to_s
end

- (String) xpath

Returns the xpath to the current node

Returns:

  • (String)

    the xpath to the current node



84
85
86
# File 'lib/svg/node.rb', line 84

def xpath
  _path
end