Module: Waxy::Render::Svg
- Defined in:
- lib/waxy/render/svg.rb,
lib/waxy/render/canvas.rb
Defined Under Namespace
Classes: Canvas
Constant Summary collapse
- TEMPLATE_PATH =
File.join(__dir__, 'templates').freeze
Class Method Summary collapse
- .close_svg ⇒ Object
- .hex_pie(layout, hex, meta = Waxy::Meta.new) ⇒ Object
- .hexagon(layout, hex, color = 'black') ⇒ Object
- .open_svg(canvas) ⇒ Object
-
.painted_hexagon(layout, map_radius, metadata) ⇒ Object
Code directly from Amit/RedBlob games This is for known radius, if you don’t have enough hexes it won’t render a full hexagon, “painted” because it completes in bands.
- .rectangle(layout, metadata, w = 10, h = 10) ⇒ Object
- .rectangle_coords(w, h) ⇒ Object
-
.spiral(layout, radius, metadata) ⇒ Object
A spiral layout that is continuous from an empty center.
-
.square(layout, metadata) ⇒ Object
Map for Square.
Class Method Details
.close_svg ⇒ Object
12 13 14 15 |
# File 'lib/waxy/render/svg.rb', line 12 def self.close_svg t = Tilt.new(File.join(TEMPLATE_PATH, 'close_svg.erb')) t.render end |
.hex_pie(layout, hex, meta = Waxy::Meta.new) ⇒ Object
22 23 24 25 26 |
# File 'lib/waxy/render/svg.rb', line 22 def self.hex_pie(layout, hex, = Waxy::Meta.new) hex.size = .size t = Tilt.new(File.join(TEMPLATE_PATH, 'hex_pie.erb')) s = t.render(layout, hex: hex, meta: ) end |
.hexagon(layout, hex, color = 'black') ⇒ Object
17 18 19 20 |
# File 'lib/waxy/render/svg.rb', line 17 def self.hexagon(layout, hex, color = 'black') t = Tilt.new(File.join(TEMPLATE_PATH, 'hex.erb')) t.render(layout, hex: hex, color: color) end |
.open_svg(canvas) ⇒ Object
7 8 9 10 |
# File 'lib/waxy/render/svg.rb', line 7 def self.open_svg(canvas) t = Tilt.new(File.join(TEMPLATE_PATH, 'open_svg.erb')) t.render(canvas) end |
.painted_hexagon(layout, map_radius, metadata) ⇒ Object
Code directly from Amit/RedBlob games This is for known radius, if you don’t have enough hexes it won’t render a full hexagon, “painted” because it completes in bands
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/waxy/render/svg.rb', line 65 def self.painted_hexagon(layout, map_radius, ) str = '' ((-1 * map_radius)..(map_radius - 1)).each do |q| r1 = [(-1 * map_radius), (-1 * q) - map_radius].max r2 = [map_radius, (-1 * q) + map_radius].min (r1 ... r2 - 1).each do |r| hex = Waxy::Geometry::Hex.new( q, r, ((-1 * q) - r)) str << Waxy::Render::Svg.hex_pie(layout, hex, .pop) return str if .size == 0 end end str end |
.rectangle(layout, metadata, w = 10, h = 10) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/waxy/render/svg.rb', line 97 def self.rectangle(layout, , w = 10, h = 10) str = '' (0..h).each do |r| layout.reset_w_padding (0..w).each do |j| return str if .size == 0 q = j - (r/2).floor hex = Waxy::Geometry::Hex.new(q, r) str << Waxy::Render::Svg.hex_pie(layout, hex, .pop) layout.increase_w_padding end layout.increase_h_padding end str end |
.rectangle_coords(w, h) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/waxy/render/svg.rb', line 114 def self.rectangle_coords(w, h) a = [] (0..h).each do |r| a[r] ||= [] (0..w).each do |j| q = j - (r/2).floor a[r][j] ||= [] a[r][j] = [q, r] end end a end |
.spiral(layout, radius, metadata) ⇒ Object
A spiral layout that is continuous from an empty center. TODO: use a theta/spiral calc and pixel_to_hex to simplify(?) the algorithim
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 57 58 59 |
# File 'lib/waxy/render/svg.rb', line 32 def self.spiral(layout, radius, ) str = '' hex = Waxy::Geometry::Hex.new( 0, 0, nil) directions = [2, 3, 4, 5] (0..radius).each do |r| (0..r).each do |z| hex = hex.hex_neighbor(0) str << Waxy::Render::Svg.hex_pie(layout, hex, .pop ) return str if .size == 0 end (0..r - 1).each do |o| hex = hex.hex_neighbor(1) str << Waxy::Render::Svg.hex_pie(layout, hex, .pop ) return str if .size == 0 end directions.each do |v1| (0..r).each do |y| hex = hex.hex_neighbor(v1) str << Waxy::Render::Svg.hex_pie(layout, hex, .pop) return str if .size == 0 end end end str end |
.square(layout, metadata) ⇒ Object
Map for Square
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/waxy/render/svg.rb', line 82 def self.square(layout, ) str = '' h = Math.sqrt(.size).to_i (0..h).each do |r| (0..h).each do |j| return str if .size == 0 q = j - (r / 2).floor hex = Waxy::Geometry::Hex.new(q, r) str << Waxy::Render::Svg.hex_pie(layout, hex, .pop) end end str end |