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.
Instance Method Summary collapse
-
#Align(position, inner:, outer:) ⇒ Sevgi::Graphics::Element
Aligns an inner box inside an outer box.
-
#Flip ⇒ Sevgi::Graphics::Element
Appends a scale(-1, -1) transform.
-
#FlipX ⇒ Sevgi::Graphics::Element
Appends a horizontal flip transform.
-
#FlipY ⇒ Sevgi::Graphics::Element
Appends a vertical flip transform.
-
#Matrix(*values) ⇒ Sevgi::Graphics::Element
Appends a six-value SVG matrix transform.
-
#Rotate(a, *origin) ⇒ Sevgi::Graphics::Element
Appends an SVG rotate transform.
-
#RotateLeft(*origin) ⇒ Sevgi::Graphics::Element
Appends a -90-degree SVG rotate transform.
-
#RotateRight(*origin) ⇒ Sevgi::Graphics::Element
Appends a 90-degree SVG rotate transform.
-
#Scale(x, y = nil) ⇒ Sevgi::Graphics::Element
Appends an SVG scale transform.
-
#Skew(ax, ay) ⇒ Sevgi::Graphics::Element
Appends a skew transform through an SVG matrix.
-
#SkewX(a) ⇒ Sevgi::Graphics::Element
Appends an SVG skewX transform.
-
#SkewY(a) ⇒ Sevgi::Graphics::Element
Appends an SVG skewY transform.
-
#Translate(x, y = nil) ⇒ Sevgi::Graphics::Element
Appends an SVG translate transform.
-
#TranslateX(x) ⇒ Sevgi::Graphics::Element
Appends an x-axis SVG translate transform.
-
#TranslateY(y) ⇒ Sevgi::Graphics::Element
Appends a y-axis SVG translate transform.
Instance Method Details
#Align(position, inner:, outer:) ⇒ Sevgi::Graphics::Element
Aligns an inner box inside an outer box.
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 |
#Flip ⇒ Sevgi::Graphics::Element
Appends a scale(-1, -1) transform.
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 |
#FlipX ⇒ Sevgi::Graphics::Element
Appends a horizontal flip transform.
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 |
#FlipY ⇒ Sevgi::Graphics::Element
Appends a vertical flip transform.
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.
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.
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
101 |
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 101 def RotateLeft(...) = Rotate(-90, ...) |
#RotateRight(*origin) ⇒ Sevgi::Graphics::Element
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.
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.
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.
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.
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.
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.
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.
180 |
# File 'lib/sevgi/graphics/mixtures/transform.rb', line 180 def TranslateY(y) = Translate(0, y) |