class CooCoo::Drawing::ChunkyCanvas

Attributes

image[R]

Public Class Methods

new(img_or_width, height = nil) click to toggle source
# 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

Public Instance Methods

blit(other, x, y, w, h) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/coo-coo/drawing/chunky_canvas.rb, line 95
def to_vector(grayscale = false)
  chunky_to_vector(@image, grayscale)
end