Module: Sevgi::Graphics::Mixtures::Transform

Defined in:
lib/sevgi/graphics/mixtures/transform.rb

Overview

DSL helpers for SVG transform attributes.

Transform calls are appended in call order and return the element, so they can be composed.

Examples:

Compose transforms on one element

Sevgi::Graphics.SVG(:minimal) do
  rect(width: 8, height: 4).Translate(12, 6).Rotate(15, 4, 2)
end

Instance Method Summary collapse

Instance Method Details

#Align(position, inner:, outer:) ⇒ Sevgi::Graphics::Element

Aligns an inner box inside an outer box.

Parameters:

  • position (Symbol, String, nil)

    alignment name

  • inner (#width, #height, nil)

    inner box

  • outer (#width, #height, nil)

    outer box

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when alignment is unsupported

  • (Sevgi::ArgumentError)

    when a box dimension is not a finite real number



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 21

def Align(position, inner:, outer:)
  return self unless position && inner && outer

  case position.to_sym
  when :center
    dimensions = [inner.width, inner.height, outer.width, outer.height]
    iw, ih, ow, oh = dimensions.map { Scalar.number(it, context: "alignment", field: :dimension) }
    Translate((ow - iw) / 2.0, (oh - ih) / 2.0)
  else
    ArgumentError.("Unsupported alignment: #{position}")
  end
end

#FlipSevgi::Graphics::Element

Appends a scale(-1, -1) transform.

Returns:



36
37
38
39
40
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 36

def Flip
  tap do
    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(-1, -1)"
  end
end

#FlipXSevgi::Graphics::Element

Appends a horizontal flip transform.

Returns:



44
45
46
47
48
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 44

def FlipX
  tap do
    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(-1, 1)"
  end
end

#FlipYSevgi::Graphics::Element

Appends a vertical flip transform.

Returns:



52
53
54
55
56
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 52

def FlipY
  tap do
    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(1, -1)"
  end
end

#Matrix(*values) ⇒ Sevgi::Graphics::Element

Appends a six-value SVG matrix transform.

Parameters:

  • values (Array<Numeric>)

    finite matrix values, normalized to SVG numbers

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when six finite real values are not supplied



62
63
64
65
66
67
68
69
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 62

def Matrix(*values)
  tap do
    ArgumentError.("Incorrect transform matrix (six values required): #{values}") if values.size != 6
    values = Scalar.numbers(values, context: "matrix")

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "matrix(#{values.join(" ")})"
  end
end

#Rotate(a, *origin) ⇒ Sevgi::Graphics::Element

Appends an SVG rotate transform.

Parameters:

  • a (Numeric)

    finite angle in degrees, normalized to an SVG number

  • origin (Array<Numeric>)

    optional finite x and y origin, normalized to SVG numbers

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when angle or origin is not finite real, or origin is not two coordinates



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 76

def Rotate(a, *origin)
  tap do
    if !origin.empty? && origin.size != 2
      ArgumentError.("Incorrect origin (two coordinates required): #{origin}")
    end

    angle = Scalar.number(a, context: "rotation", field: :angle)
    origin = Scalar.numbers(origin, context: "rotation")

    next if angle.zero?

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "rotate(#{[angle, *origin].join(", ")})"
  end
end

#RotateLeft(*origin) ⇒ Sevgi::Graphics::Element

Appends a -90-degree SVG rotate transform.

Parameters:

  • origin (Array<Numeric>)

    optional x and y origin

Returns:



101
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 101

def RotateLeft(...) = Rotate(-90, ...)

#RotateRight(*origin) ⇒ Sevgi::Graphics::Element

Appends a 90-degree SVG rotate transform.

Parameters:

  • origin (Array<Numeric>)

    optional x and y origin

Returns:



95
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 95

def RotateRight(...) = Rotate(90, ...)

#Scale(x, y = nil) ⇒ Sevgi::Graphics::Element

Appends an SVG scale transform.

Parameters:

  • x (Numeric)

    finite x scale, or uniform scale when y is nil

  • y (Numeric, nil) (defaults to: nil)

    finite y scale

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when a scale is not a finite real number



108
109
110
111
112
113
114
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 108

def Scale(x, y = nil)
  tap do
    x = Scalar.number(x, context: "scale", field: :x)
    y = Scalar.number(y, context: "scale", field: :y) unless y.nil?
    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(#{(y.nil? ? [x] : [x, y]).join(", ")})"
  end
end

#Skew(ax, ay) ⇒ Sevgi::Graphics::Element

Appends a skew transform through an SVG matrix.

Parameters:

  • ax (Numeric)

    finite x skew angle in degrees, normalized before calculation

  • ay (Numeric)

    finite y skew angle in degrees, normalized before calculation

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when an angle is not a finite real number



121
122
123
124
125
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 121

def Skew(ax, ay)
  ax = Scalar.number(ax, context: "skew", field: :x)
  ay = Scalar.number(ay, context: "skew", field: :y)
  Matrix(1.0, ::Math.tan(ay / 180.0 * ::Math::PI), ::Math.tan(ax / 180.0 * ::Math::PI), 1.0, 0.0, 0.0)
end

#SkewX(a) ⇒ Sevgi::Graphics::Element

Appends an SVG skewX transform.

Parameters:

  • a (Numeric)

    finite angle in degrees, normalized to an SVG number

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when angle is not a finite real number



131
132
133
134
135
136
137
138
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 131

def SkewX(a)
  tap do
    angle = Scalar.number(a, context: "skew", field: :x)
    next if angle.zero?

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "skewX(#{angle})"
  end
end

#SkewY(a) ⇒ Sevgi::Graphics::Element

Appends an SVG skewY transform.

Parameters:

  • a (Numeric)

    finite angle in degrees, normalized to an SVG number

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when angle is not a finite real number



144
145
146
147
148
149
150
151
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 144

def SkewY(a)
  tap do
    angle = Scalar.number(a, context: "skew", field: :y)
    next if angle.zero?

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "skewY(#{angle})"
  end
end

#Translate(x, y = nil) ⇒ Sevgi::Graphics::Element

Appends an SVG translate transform. One argument translates only the x axis.

Parameters:

  • x (Numeric)

    finite x translation, normalized to an SVG number

  • y (Numeric, nil) (defaults to: nil)

    finite y translation, normalized to an SVG number

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when a coordinate is not a finite real number



158
159
160
161
162
163
164
165
166
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 158

def Translate(x, y = nil)
  tap do
    dx = Scalar.number(x, context: "translation", field: :x)
    dy = Scalar.number(y, context: "translation", field: :y) unless y.nil?
    next if dx.zero? && (dy.nil? || dy.zero?)

    attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "translate(#{(dy.nil? ? [dx] : [dx, dy]).join(" ")})"
  end
end

#TranslateX(x) ⇒ Sevgi::Graphics::Element

Appends an x-axis SVG translate transform.

Parameters:

  • x (Numeric)

    finite x translation

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when x is not a finite real number

See Also:



173
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 173

def TranslateX(x) = Translate(x)

#TranslateY(y) ⇒ Sevgi::Graphics::Element

Appends a y-axis SVG translate transform.

Parameters:

  • y (Numeric)

    finite y translation

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when y is not a finite real number

See Also:



180
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 180

def TranslateY(y) = Translate(0, y)