Module: Sumi::Graphics::Mixtures::Transform
- Defined in:
- graphics/lib/sumi/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:) ⇒ Sumi::Graphics::Element
Aligns an inner box inside an outer box.
-
#Flip ⇒ Sumi::Graphics::Element
Appends a scale(-1, -1) transform.
-
#FlipX ⇒ Sumi::Graphics::Element
Appends a horizontal flip transform.
-
#FlipY ⇒ Sumi::Graphics::Element
Appends a vertical flip transform.
-
#Matrix(*values) ⇒ Sumi::Graphics::Element
Appends a six-value SVG matrix transform.
-
#Rotate(a, *origin) ⇒ Sumi::Graphics::Element
Appends an SVG rotate transform.
-
#RotateLeft(*origin) ⇒ Sumi::Graphics::Element
Appends a -90-degree SVG rotate transform.
-
#RotateRight(*origin) ⇒ Sumi::Graphics::Element
Appends a 90-degree SVG rotate transform.
-
#Scale(x, y = nil) ⇒ Sumi::Graphics::Element
Appends an SVG scale transform.
-
#Skew(ax, ay) ⇒ Sumi::Graphics::Element
Appends a skew transform through an SVG matrix.
-
#SkewX(a) ⇒ Sumi::Graphics::Element
Appends an SVG skewX transform.
-
#SkewY(a) ⇒ Sumi::Graphics::Element
Appends an SVG skewY transform.
-
#Translate(x, y = nil) ⇒ Sumi::Graphics::Element
Appends an SVG translate transform.
-
#TranslateX(x) ⇒ Sumi::Graphics::Element
Appends an x-axis SVG translate transform.
-
#TranslateY(y) ⇒ Sumi::Graphics::Element
Appends a y-axis SVG translate transform.
Instance Method Details
#Align(position, inner:, outer:) ⇒ Sumi::Graphics::Element
Size-only boxes have origin (0, 0). Positions describe known box geometry, not renderer-computed bounds.
Aligns an inner box inside an outer box.
66 67 68 69 70 71 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 66 def Align(position, inner:, outer:) offset = Alignment.offset(position, inner:, outer:) return self unless offset Translate(*offset) end |
#Flip ⇒ Sumi::Graphics::Element
Appends a scale(-1, -1) transform.
75 76 77 78 79 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 75 def Flip tap do attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(-1, -1)" end end |
#FlipX ⇒ Sumi::Graphics::Element
Appends a horizontal flip transform.
83 84 85 86 87 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 83 def FlipX tap do attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(-1, 1)" end end |
#FlipY ⇒ Sumi::Graphics::Element
Appends a vertical flip transform.
91 92 93 94 95 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 91 def FlipY tap do attributes[:"transform#{Attributes::UPDATE_SUFFIX}"] = "scale(1, -1)" end end |
#Matrix(*values) ⇒ Sumi::Graphics::Element
Appends a six-value SVG matrix transform.
101 102 103 104 105 106 107 108 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 101 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) ⇒ Sumi::Graphics::Element
Appends an SVG rotate transform.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 115 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) ⇒ Sumi::Graphics::Element
140 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 140 def RotateLeft(...) = Rotate(-90, ...) |
#RotateRight(*origin) ⇒ Sumi::Graphics::Element
134 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 134 def RotateRight(...) = Rotate(90, ...) |
#Scale(x, y = nil) ⇒ Sumi::Graphics::Element
Appends an SVG scale transform.
147 148 149 150 151 152 153 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 147 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) ⇒ Sumi::Graphics::Element
Appends a skew transform through an SVG matrix.
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 160 def Skew(ax, ay) ax = Scalar.number(ax, context: "skew", field: :x) ay = Scalar.number(ay, context: "skew", field: :y) [ax, ay].each do |angle| ArgumentError.("Skew angle must not be +/-90 (mod 180): #{angle}") if ((angle % 180) - 90).zero? end return self if ax.zero? && ay.zero? Matrix(1.0, F.tan(ay), F.tan(ax), 1.0, 0.0, 0.0) end |
#SkewX(a) ⇒ Sumi::Graphics::Element
Appends an SVG skewX transform.
177 178 179 180 181 182 183 184 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 177 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) ⇒ Sumi::Graphics::Element
Appends an SVG skewY transform.
190 191 192 193 194 195 196 197 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 190 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) ⇒ Sumi::Graphics::Element
Appends an SVG translate transform. One argument translates only the x axis.
204 205 206 207 208 209 210 211 212 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 204 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) ⇒ Sumi::Graphics::Element
Appends an x-axis SVG translate transform.
219 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 219 def TranslateX(x) = Translate(x) |
#TranslateY(y) ⇒ Sumi::Graphics::Element
Appends a y-axis SVG translate transform.
226 |
# File 'graphics/lib/sumi/graphics/mixtures/transform.rb', line 226 def TranslateY(y) = Translate(0, y) |