Class: CooCoo::Drawing::ChunkyCanvas
- Defined in:
- lib/coo-coo/drawing/chunky_canvas.rb
Instance Attribute Summary collapse
-
#image ⇒ Object
readonly
Returns the value of attribute image.
Attributes inherited from Canvas
Instance Method Summary collapse
- #blit(other, x, y, w, h) ⇒ Object
- #circle(x, y, r) ⇒ Object
-
#initialize(img_or_width, height = nil) ⇒ ChunkyCanvas
constructor
A new instance of ChunkyCanvas.
- #lerp_color(a, b, t) ⇒ Object
- #line(x1, y1, x2, y2) ⇒ Object
- #rect(x, y, w, h) ⇒ Object
- #stroke(points) ⇒ Object
- #text(txt, x, y, font, font_size, font_style = nil) ⇒ Object
- #to_vector(grayscale = false) ⇒ Object
Methods inherited from Canvas
Constructor Details
#initialize(img_or_width, height = nil) ⇒ ChunkyCanvas
Returns a new instance of ChunkyCanvas.
9 10 11 12 13 14 15 16 17 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 9 def initialize(img_or_width, height = nil) if height img = ChunkyPNG::Image.new(img_or_width, height) else img = img_or_width end @image = img end |
Instance Attribute Details
#image ⇒ Object (readonly)
Returns the value of attribute image.
7 8 9 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 7 def image @image end |
Instance Method Details
#blit(other, x, y, w, h) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 82 def blit(other, x, y, w, h) img = ChunkyPNG::Image.from_blob(other) if w != img.width || h != img.height img.resample_bilinear!(w.to_i, h.to_i) end @image.compose!(img, x.to_i, y.to_i) self end |
#circle(x, y, r) ⇒ Object
77 78 79 80 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 77 def circle(x, y, r) @image.circle(x, y, r, stroke_color, fill_color) self end |
#lerp_color(a, b, t) ⇒ Object
68 69 70 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 68 def lerp_color(a, b, t) ChunkyPNG::Color.interpolate_quick(a, b, (t * 256).to_i) end |
#line(x1, y1, x2, y2) ⇒ Object
19 20 21 22 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 19 def line(x1, y1, x2, y2) @image.line(x1, y1, x2, y2, stroke_color) self end |
#rect(x, y, w, h) ⇒ Object
72 73 74 75 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 72 def rect(x, y, w, h) @image.rect(x, y, w, h, stroke_color, fill_color) self end |
#stroke(points) ⇒ Object
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 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 24 def stroke(points) last_x = points[0][0] last_y = points[0][1] last_w = points[0][2] || 1.0 last_color = points[0][3] points.each.drop(1).each do |(x, y, w, color)| w ||= 1.0 if color self.stroke_color = color self.fill_color = color end if w <= 1.0 line(last_x.to_i, last_y.to_i, x.to_i, y.to_i) else step_x = (x / w).abs.round step_y = (y / w).abs.round steps = Math.max(step_x, step_y) steps = 4.0 if steps < 4.0 (steps + 1).to_i.times do |n| t = n / steps.to_f if color self.stroke_color = lerp_color(last_color, color, t) self.fill_color = lerp_color(last_color, color, t) end circle(Math.lerp(last_x, x, t).to_i, Math.lerp(last_y, y, t).to_i, (Math.lerp(last_w, w, t)/2.0).ceil.to_i) end end last_x = x last_y = y last_w = w last_color = color end self end |
#text(txt, x, y, font, font_size, font_style = nil) ⇒ Object
91 92 93 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 91 def text(txt, x, y, font, font_size, font_style = nil) self end |
#to_vector(grayscale = false) ⇒ Object
95 96 97 |
# File 'lib/coo-coo/drawing/chunky_canvas.rb', line 95 def to_vector(grayscale = false) chunky_to_vector(@image, grayscale) end |