Class: Pimento::Canvas

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/pimento/canvas.rb

Constant Summary collapse

FIRST_ID =
741820390
FIRST_OBJ =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#render

Constructor Details

#initialize(top, left, width, height) ⇒ Canvas

Returns a new instance of Canvas.



14
15
16
17
18
19
20
# File 'lib/pimento/canvas.rb', line 14

def initialize(top, left, width, height)
  @top = top
  @left = left
  @width = width
  @height = height
  @components = []
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/pimento/canvas.rb', line 9

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/pimento/canvas.rb', line 9

def width
  @width
end

Instance Method Details

#line(x1, y1, x2, y2) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pimento/canvas.rb', line 26

def line(x1, y1, x2, y2)
  x = x1
  y = y1
  w = (x2 - x1).abs
  h = (y2 - y1).abs

  case
  when y1 == y2
    @components << Component.for(:horizontal_line).new(self, x, y, w)
  when x1 == x2
    @components << Component.for(:vertical_line).new(self, x, y, h)
  else
    line_with_dots(x1, y1, x2, y2)
  end
end

#line_with_dots(x1, y1, x2, y2, step = 20) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pimento/canvas.rb', line 42

def line_with_dots(x1, y1, x2, y2, step = 20)
  x = x1
  y = y1
  w = (x2 - x1).abs
  h = (y2 - y1).abs

  num_dots = ((w > h) ? w : h) / step
  (num_dots + 1).times do |i|
    point(x1 + ((x2 - x1) / num_dots * i).to_i, y1 + ((y2 - y1) / num_dots * i).to_i)
  end
end

#point(x, y) ⇒ Object



22
23
24
# File 'lib/pimento/canvas.rb', line 22

def point(x, y)
  @components << Component.for(:dot).new(self, x, y)
end

#to_xmlObject



54
55
56
57
# File 'lib/pimento/canvas.rb', line 54

def to_xml
  XML::Document.string(render('canvas'), :options =>
    XML::Parser::Options::NOENT | XML::Parser::Options::NOBLANKS)
end