Class: GeoPattern::TrianglePattern
- Inherits:
-
BasePattern
- Object
- Struct
- BasePattern
- GeoPattern::TrianglePattern
- Defined in:
- lib/geo_pattern/pattern/triangle_pattern.rb
Constant Summary
Constants inherited from BasePattern
BasePattern::FILL_COLOR_DARK, BasePattern::FILL_COLOR_LIGHT, BasePattern::OPACITY_MAX, BasePattern::OPACITY_MIN, BasePattern::STROKE_COLOR, BasePattern::STROKE_OPACITY
Instance Attribute Summary
Attributes inherited from BasePattern
Instance Method Summary collapse
Methods inherited from BasePattern
#fill_color, #hex_val, #map, #opacity
Instance Method Details
#build_triangle_shape(side_length, height) ⇒ Object
46 47 48 49 |
# File 'lib/geo_pattern/pattern/triangle_pattern.rb', line 46 def build_triangle_shape(side_length, height) half_width = side_length / 2 "#{half_width}, 0, #{side_length}, #{height}, 0, #{height}, #{half_width}, 0" end |
#render_to_svg ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/geo_pattern/pattern/triangle_pattern.rb', line 3 def render_to_svg scale = hex_val(0, 1) side_length = map(scale, 0, 15, 15, 80) triangle_height = side_length/2 * Math.sqrt(3) triangle = build_triangle_shape(side_length, triangle_height) svg.set_width(side_length * 3) svg.set_height(triangle_height * 6) i = 0 for y in 0..5 for x in 0..5 val = hex_val(i, 1) opacity = opacity(val) fill = fill_color(val) styles = { "fill" => fill, "fill-opacity" => opacity, "stroke" => STROKE_COLOR, "stroke-opacity" => STROKE_OPACITY } rotation = "" if y % 2 == 0 rotation = x % 2 == 0 ? 180 : 0 else rotation = x % 2 != 0 ? 180 : 0 end svg.polyline(triangle, styles.merge({ "transform" => "translate(#{x*side_length*0.5 - side_length/2}, #{triangle_height*y}) rotate(#{rotation}, #{side_length/2}, #{triangle_height/2})"})) # Add an extra one at top-right, for tiling. if (x == 0) svg.polyline(triangle, styles.merge({ "transform" => "translate(#{6*side_length*0.5 - side_length/2}, #{triangle_height*y}) rotate(#{rotation}, #{side_length/2}, #{triangle_height/2})"})) end i += 1 end end end |