Class: GeoPattern::XesPattern

Inherits:
BasePattern show all
Defined in:
lib/geo_pattern/pattern/xes_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

#hash, #svg

Instance Method Summary collapse

Methods inherited from BasePattern

#fill_color, #hex_val, #map, #opacity

Instance Method Details

#render_to_svgObject



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
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/geo_pattern/pattern/xes_pattern.rb', line 3

def render_to_svg
  square_size = map(hex_val(0, 1), 0, 15, 10, 25)
  x_shape     = build_plus_shape(square_size) # rotated later
  x_size      = square_size * 3 * 0.943

  svg.set_width(x_size * 3)
  svg.set_height(x_size * 3)

  i = 0
  for y in 0..5
    for x in 0..5
      val     = hex_val(i, 1)
      opacity = opacity(val)
      dy      = x % 2 == 0 ? y*x_size - x_size*0.5 : y*x_size - x_size*0.5 + x_size/4
      fill    = fill_color(val)

      styles = {
        "fill"  => fill,
        "style" => {
          "opacity" => opacity
        }
      }

      svg.group(x_shape, styles.merge({
        "transform" => "translate(#{x*x_size/2 - x_size/2},#{dy - y*x_size/2}) rotate(45, #{x_size/2}, #{x_size/2})"}))

      # Add an extra column on the right for tiling.
      if (x == 0)
        svg.group(x_shape, styles.merge({
          "transform" => "translate(#{6*x_size/2 - x_size/2},#{dy - y*x_size/2}) rotate(45, #{x_size/2}, #{x_size/2})"}))
      end

      # Add an extra row on the bottom that matches the first row, for tiling.
      if (y == 0)
        dy = x % 2 == 0 ? 6*x_size - x_size/2 : 6*x_size - x_size/2 + x_size/4;
        svg.group(x_shape, styles.merge({
          "transform" => "translate(#{x*x_size/2 - x_size/2},#{dy - 6*x_size/2}) rotate(45, #{x_size/2}, #{x_size/2})"}))
      end

      # These can hang off the bottom, so put a row at the top for tiling.
      if (y == 5)
        svg.group(x_shape, styles.merge({
          "transform" => "translate(#{x*x_size/2 - x_size/2},#{dy - 11*x_size/2}) rotate(45, #{x_size/2}, #{x_size/2})"}))
      end

      # Add an extra one at top-right and bottom-right, for tiling.
      if (x == 0 && y == 0)
        svg.group(x_shape, styles.merge({
          "transform" => "translate(#{6*x_size/2 - x_size/2},#{dy - 6*x_size/2}) rotate(45, #{x_size/2}, #{x_size/2})"}))
      end
      i += 1
    end
  end
end