2021-11-25 17:49:53 +03:00
|
|
|
import js.dom
|
2021-10-27 23:18:09 +03:00
|
|
|
|
2022-07-19 16:36:16 +03:00
|
|
|
fn get_canvas(elem JS.HTMLElement) JS.HTMLCanvasElement {
|
2021-11-20 22:28:11 +03:00
|
|
|
match elem {
|
|
|
|
JS.HTMLCanvasElement {
|
|
|
|
return elem
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('Not a canvas')
|
2021-10-27 23:18:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 22:28:11 +03:00
|
|
|
fn draw_line(mut context JS.CanvasRenderingContext2D, x1 int, y1 int, x2 int, y2 int) {
|
|
|
|
context.beginPath()
|
|
|
|
context.strokeStyle = 'black'.str
|
|
|
|
context.lineWidth = JS.Number(1)
|
|
|
|
context.moveTo(x1, y1)
|
|
|
|
context.lineTo(x2, y2)
|
2021-10-27 23:18:09 +03:00
|
|
|
context.stroke()
|
2021-11-20 22:28:11 +03:00
|
|
|
context.closePath()
|
2021-10-27 23:18:09 +03:00
|
|
|
}
|
|
|
|
|
2021-10-29 11:23:40 +03:00
|
|
|
struct DrawState {
|
|
|
|
mut:
|
2021-11-20 22:28:11 +03:00
|
|
|
context JS.CanvasRenderingContext2D
|
2021-10-29 11:23:40 +03:00
|
|
|
drawing bool
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2021-10-27 23:18:09 +03:00
|
|
|
fn main() {
|
2021-11-25 17:49:53 +03:00
|
|
|
window := dom.window()
|
|
|
|
document := dom.document
|
2022-05-13 06:56:21 +03:00
|
|
|
clear_btn := document.getElementById('clearButton'.str)?
|
|
|
|
canvas_elem := document.getElementById('canvas'.str)?
|
2021-11-20 22:28:11 +03:00
|
|
|
canvas := get_canvas(canvas_elem)
|
2022-05-13 06:56:21 +03:00
|
|
|
ctx := canvas.getContext('2d'.str, js_undefined())?
|
2021-11-20 22:28:11 +03:00
|
|
|
context := match ctx {
|
|
|
|
JS.CanvasRenderingContext2D {
|
|
|
|
ctx
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('can not get 2d context')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut state := DrawState{context, false, 0, 0}
|
2021-10-27 23:18:09 +03:00
|
|
|
|
2021-11-20 22:28:11 +03:00
|
|
|
canvas.addEventListener('mousedown'.str, fn [mut state] (event JS.Event) {
|
2021-10-29 11:23:40 +03:00
|
|
|
state.drawing = true
|
2021-11-20 22:28:11 +03:00
|
|
|
match event {
|
|
|
|
JS.MouseEvent {
|
|
|
|
state.x = int(event.offsetX)
|
|
|
|
state.y = int(event.offsetY)
|
|
|
|
}
|
|
|
|
else {}
|
2021-10-27 23:18:09 +03:00
|
|
|
}
|
2021-11-20 22:28:11 +03:00
|
|
|
}, JS.EventListenerOptions{})
|
|
|
|
canvas.addEventListener('mousemove'.str, fn [mut state] (event JS.Event) {
|
2021-10-29 11:23:40 +03:00
|
|
|
if state.drawing {
|
2021-11-20 22:28:11 +03:00
|
|
|
match event {
|
|
|
|
JS.MouseEvent {
|
|
|
|
draw_line(mut state.context, state.x, state.y, int(event.offsetX),
|
|
|
|
int(event.offsetY))
|
|
|
|
state.x = int(event.offsetX)
|
|
|
|
state.y = int(event.offsetY)
|
|
|
|
}
|
|
|
|
else {}
|
2021-10-27 23:18:09 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-20 22:28:11 +03:00
|
|
|
}, JS.EventListenerOptions{})
|
2021-10-27 23:18:09 +03:00
|
|
|
|
2021-11-20 22:28:11 +03:00
|
|
|
window.addEventListener('mouseup'.str, fn [mut state] (event JS.Event) {
|
2021-10-29 11:23:40 +03:00
|
|
|
if state.drawing {
|
2021-11-20 22:28:11 +03:00
|
|
|
match event {
|
|
|
|
JS.MouseEvent {
|
|
|
|
draw_line(mut state.context, state.x, state.y, int(event.offsetX),
|
|
|
|
int(event.offsetY))
|
|
|
|
}
|
|
|
|
else {}
|
2021-10-27 23:18:09 +03:00
|
|
|
}
|
2021-10-29 11:23:40 +03:00
|
|
|
state.x = 0
|
|
|
|
state.y = 0
|
|
|
|
state.drawing = false
|
2021-10-27 23:18:09 +03:00
|
|
|
}
|
2021-11-20 22:28:11 +03:00
|
|
|
}, JS.EventListenerOptions{})
|
|
|
|
clear_btn.addEventListener('click'.str, fn [mut state, canvas] (_ JS.Event) {
|
|
|
|
state.context.clearRect(0, 0, canvas.width, canvas.height)
|
|
|
|
}, JS.EventListenerOptions{})
|
2021-11-17 12:41:33 +03:00
|
|
|
}
|