Sha256: 7ce13aaf5fb06af4bdceebbc0df7124b1dbc12148093c10e27705fba825ab90f

Contents?: true

Size: 999 Bytes

Versions: 7

Compression:

Stored size: 999 Bytes

Contents

require 'opal'
require 'native'
require 'promise'
require 'browser/setup/full'

$document.head << CSS {
  rule("body") {
    margin 0
    padding 0
    overflow :hidden
  }
}

c = Browser::Canvas.new($window.view.width, $window.view.height)
$document.body << c

$window.on :resize do |e|
  c.height = $window.view.height
  c.width = $window.view.width
end

# A port of https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event

is_drawing = false
x, y = 0, 0
y = 0

c.on :mousedown do |e|
  x, y = e.offset.x, e.offset.y
  is_drawing = true
end

c.on :mousemove do |e|
  if is_drawing
    draw_line(c, x, y, e.offset.x, e.offset.y)
    x, y = e.offset.x, e.offset.y
  end
end

$window.on :mouseup do |e|
  if is_drawing
    draw_line(c, x, y, e.offset.x, e.offset.y)
    x, y = e.offset.x, e.offset.y
    is_drawing = false
  end
end

def draw_line(c, x1, y1, x2, y2)
  c.path do
    c.style.stroke = :black
    c.style.line.width = 1
    c.line(x1, y1, x2, y2)
    c.stroke
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
opal-browser-0.3.5 examples/canvas/app/application.rb
opal-browser-0.3.4 examples/canvas/app/application.rb
atome-opal-browser-0.3.9.5 examples/canvas/app/application.rb
opal-browser-0.3.3 examples/canvas/app/application.rb
opal-browser-0.3.2 examples/canvas/app/application.rb
opal-browser-0.3.1 examples/canvas/app/application.rb
opal-browser-0.3.0 examples/canvas/app/application.rb