Class: Vizcore::Renderer::SnapshotRenderer::Canvas
- Inherits:
-
Object
- Object
- Vizcore::Renderer::SnapshotRenderer::Canvas
- Defined in:
- lib/vizcore/renderer/snapshot_renderer.rb
Overview
Tiny RGBA canvas with alpha blending and a few primitive drawing helpers.
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #draw_circle_outline(cx, cy, radius, color, alpha:) ⇒ Object
- #draw_label(text, x:, y:, color:, alpha:, letter_spacing: 0.0) ⇒ Object
- #draw_line(x1, y1, x2, y2, color, alpha:) ⇒ Object
- #draw_rect_outline(x, y, rect_width, rect_height, color, alpha:) ⇒ Object
- #draw_wave(y_base, amplitude:, color:, alpha:, height_scale: 1.0) ⇒ Object
- #fill_circle(cx, cy, radius, color, alpha:) ⇒ Object
- #fill_gradient(top, bottom) ⇒ Object
- #fill_rect(x, y, rect_width, rect_height, color, alpha:) ⇒ Object
-
#initialize(width:, height:, transparent: false) ⇒ Canvas
constructor
A new instance of Canvas.
Constructor Details
#initialize(width:, height:, transparent: false) ⇒ Canvas
Returns a new instance of Canvas.
935 936 937 938 939 940 941 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 935 def initialize(width:, height:, transparent: false) @width = width @height = height @bytes = String.new(capacity: width * height * 4, encoding: Encoding::BINARY) alpha = transparent ? 0 : 255 @bytes << ([0, 0, 0, alpha].pack("C4") * (width * height)) end |
Instance Attribute Details
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes.
943 944 945 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 943 def bytes @bytes end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
943 944 945 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 943 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
943 944 945 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 943 def width @width end |
Instance Method Details
#draw_circle_outline(cx, cy, radius, color, alpha:) ⇒ Object
970 971 972 973 974 975 976 977 978 979 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 970 def draw_circle_outline(cx, cy, radius, color, alpha:) segments = 96 previous = nil (0..segments).each do |index| angle = (index.to_f / segments) * Math::PI * 2 point = [cx + Math.cos(angle) * radius, cy + Math.sin(angle) * radius] draw_line(previous[0], previous[1], point[0], point[1], color, alpha: alpha) if previous previous = point end end |
#draw_label(text, x:, y:, color:, alpha:, letter_spacing: 0.0) ⇒ Object
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 1018 def draw_label(text, x:, y:, color:, alpha:, letter_spacing: 0.0) chars = text.each_byte.first(24) char_width = 14 + Float(letter_spacing).clamp(0.0, 96.0).round total_width = chars.length * char_width start_x = x.round - total_width / 2 chars.each_with_index do |byte, index| height_factor = 0.35 + (byte % 9) * 0.07 (start_x + index * char_width, y.round, 9, (42 * height_factor).round, color, alpha) end end |
#draw_line(x1, y1, x2, y2, color, alpha:) ⇒ Object
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 991 def draw_line(x1, y1, x2, y2, color, alpha:) x1 = x1.round y1 = y1.round x2 = x2.round y2 = y2.round steps = [(x2 - x1).abs, (y2 - y1).abs].max return blend_pixel(x1, y1, color, alpha) if steps.zero? steps.times do |step| t = step.to_f / steps blend_pixel(interpolate(x1, x2, t).round, interpolate(y1, y2, t).round, color, alpha) end end |
#draw_rect_outline(x, y, rect_width, rect_height, color, alpha:) ⇒ Object
963 964 965 966 967 968 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 963 def draw_rect_outline(x, y, rect_width, rect_height, color, alpha:) draw_line(x, y, x + rect_width, y, color, alpha: alpha) draw_line(x + rect_width, y, x + rect_width, y + rect_height, color, alpha: alpha) draw_line(x + rect_width, y + rect_height, x, y + rect_height, color, alpha: alpha) draw_line(x, y + rect_height, x, y, color, alpha: alpha) end |
#draw_wave(y_base, amplitude:, color:, alpha:, height_scale: 1.0) ⇒ Object
953 954 955 956 957 958 959 960 961 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 953 def draw_wave(y_base, amplitude:, color:, alpha:, height_scale: 1.0) previous = nil width.times do |x| phase = (x.to_f / width) * Math::PI * 4.0 y = y_base + Math.sin(phase) * height * (0.06 + amplitude * 0.08) * height_scale draw_line(previous[0], previous[1], x, y, color, alpha: alpha) if previous previous = [x, y] end end |
#fill_circle(cx, cy, radius, color, alpha:) ⇒ Object
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 1005 def fill_circle(cx, cy, radius, color, alpha:) cx = cx.round cy = cy.round radius = radius.round (cy - radius).upto(cy + radius) do |y| (cx - radius).upto(cx + radius) do |x| next if ((x - cx)**2) + ((y - cy)**2) > radius**2 blend_pixel(x, y, color, alpha) end end end |
#fill_gradient(top, bottom) ⇒ Object
945 946 947 948 949 950 951 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 945 def fill_gradient(top, bottom) height.times do |y| t = y.to_f / [height - 1, 1].max color = 3.times.map { |index| interpolate(top[index], bottom[index], t).round } width.times { |x| set_pixel(x, y, color, 255) } end end |
#fill_rect(x, y, rect_width, rect_height, color, alpha:) ⇒ Object
981 982 983 984 985 986 987 988 989 |
# File 'lib/vizcore/renderer/snapshot_renderer.rb', line 981 def fill_rect(x, y, rect_width, rect_height, color, alpha:) start_x = x.round end_x = (x + rect_width).round start_y = y.round end_y = (y + rect_height).round start_y.upto(end_y) do |py| start_x.upto(end_x) { |px| blend_pixel(px, py, color, alpha) } end end |