Class: Dare::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/dare/canvas.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Canvas

Returns a new instance of Canvas.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dare/canvas.rb', line 4

def initialize(opts = {})
  opts[:width] ||= 640
  opts[:height] ||= 480
  opts[:border] ||= false
  `var my_canvas = document.createElement("canvas")`
  @id = rand(36**8).to_s(36)
  `my_canvas.setAttribute('id', #{@id})`
  `my_canvas.width = #{opts[:width]}`
  `my_canvas.height = #{opts[:height]}`
  `my_canvas.style.border = "solid 1px black"` if opts[:border]
  `document.body.appendChild(my_canvas)`
  @canvas = `my_canvas`
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



3
4
5
# File 'lib/dare/canvas.rb', line 3

def canvas
  @canvas
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/dare/canvas.rb', line 3

def id
  @id
end

Instance Method Details

#contextObject



17
18
19
# File 'lib/dare/canvas.rb', line 17

def context
  `#{@canvas}.getContext('2d')`
end