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

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

Overview

DSL helpers for SVG transform attributes.

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



14
15
16
17
18
19
20
21
22
23
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 14

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

  case position.to_sym
  when :center
    Translate((outer.width - inner.width) / 2.0, (outer.height - inner.height) / 2.0)
  else
    ArgumentError.("Unsupported alignment: #{position}")
  end
end

#FlipSevgi::Graphics::Element

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

Returns:



27
28
29
30
31
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 27

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

#FlipXSevgi::Graphics::Element

Appends a horizontal flip transform.

Returns:



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

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

#FlipYSevgi::Graphics::Element

Appends a vertical flip transform.

Returns:



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

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

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

Appends a six-value SVG matrix transform.

Parameters:

  • values (Array<Numeric>)

    matrix values

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when exactly six values are not supplied



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

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

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

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

Appends an SVG rotate transform.

Parameters:

  • a (Numeric)

    angle in degrees

  • origin (Array<Numeric>)

    optional x and y origin

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when origin is not two coordinates



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 66

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

    next if a.to_f == 0.0

    attributes[:"transform#{ATTRIBUTE_UPDATE_SUFFIX}"] = "rotate(#{[a, *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:



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

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:



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

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

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

Appends an SVG scale transform.

Parameters:

  • x (Numeric)

    x scale, or uniform scale when y is nil

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

    y scale

Returns:



94
95
96
97
98
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 94

def Scale(x, y = nil)
  tap do
    attributes[:"transform#{ATTRIBUTE_UPDATE_SUFFIX}"] = "scale(#{(y ? [x, y] : [x]).join(", ")})"
  end
end

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

Appends a skew transform through an SVG matrix.

Parameters:

  • ax (Numeric)

    x skew angle in degrees

  • ay (Numeric)

    y skew angle in degrees

Returns:



104
105
106
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 104

def Skew(ax, ay)
  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)

    angle in degrees

Returns:



111
112
113
114
115
116
117
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 111

def SkewX(a)
  tap do
    next if a.to_f == 0.0

    attributes[:"transform#{ATTRIBUTE_UPDATE_SUFFIX}"] = "skewX(#{a})"
  end
end

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

Appends an SVG skewY transform.

Parameters:

  • a (Numeric)

    angle in degrees

Returns:



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

def SkewY(a)
  tap do
    next if a.to_f == 0.0

    attributes[:"transform#{ATTRIBUTE_UPDATE_SUFFIX}"] = "skewY(#{a})"
  end
end

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

Appends an SVG translate transform.

Parameters:

  • x (Numeric)

    x translation

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

    y translation

Returns:



134
135
136
137
138
139
140
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 134

def Translate(x, y = nil)
  tap do
    next if x.to_f == 0.0 && (y.nil? || y.to_f == 0.0)

    attributes[:"transform#{ATTRIBUTE_UPDATE_SUFFIX}"] = "translate(#{(y ? [x, y] : [x]).join(" ")})"
  end
end