2021-10-27 22:18:09 +02:00
|
|
|
import jsdom
|
|
|
|
|
2021-11-20 20:28:11 +01:00
|
|
|
fn get_canvas(elem JS.HTMLElement) &JS.HTMLCanvasElement {
|
|
|
|
match elem {
|
|
|
|
JS.HTMLCanvasElement {
|
|
|
|
return elem
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('Not a canvas')
|
2021-10-27 22:18:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 20:28:11 +01: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 22:18:09 +02:00
|
|
|
context.stroke()
|
2021-11-20 20:28:11 +01:00
|
|
|
context.closePath()
|
2021-10-27 22:18:09 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 10:23:40 +02:00
|
|
|
struct DrawState {
|
|
|
|
mut:
|
2021-11-20 20:28:11 +01:00
|
|
|
context JS.CanvasRenderingContext2D
|
2021-10-29 10:23:40 +02:00
|
|
|
drawing bool
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2021-10-27 22:18:09 +02:00
|
|
|
fn main() {
|
2021-11-20 20:28:11 +01:00
|
|
|
window := jsdom.window()
|
2021-10-27 22:18:09 +02:00
|
|
|
document := jsdom.document
|
2021-11-20 20:28:11 +01:00
|
|
|
clear_btn := document.getElementById('clearButton'.str) ?
|
|
|
|
canvas_elem := document.getElementById('canvas'.str) ?
|
|
|
|
canvas := get_canvas(canvas_elem)
|
|
|
|
ctx := canvas.getContext('2d'.str, js_undefined()) ?
|
|
|
|
context := match ctx {
|
|
|
|
JS.CanvasRenderingContext2D {
|
|
|
|
ctx
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('can not get 2d context')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut state := DrawState{context, false, 0, 0}
|
2021-10-27 22:18:09 +02:00
|
|
|
|
2021-11-20 20:28:11 +01:00
|
|
|
canvas.addEventListener('mousedown'.str, fn [mut state] (event JS.Event) {
|
2021-10-29 10:23:40 +02:00
|
|
|
state.drawing = true
|
2021-11-20 20:28:11 +01:00
|
|
|
match event {
|
|
|
|
JS.MouseEvent {
|
|
|
|
state.x = int(event.offsetX)
|
|
|
|
state.y = int(event.offsetY)
|
|
|
|
}
|
|
|
|
else {}
|
2021-10-27 22:18:09 +02:00
|
|
|
}
|
2021-11-20 20:28:11 +01:00
|
|
|
}, JS.EventListenerOptions{})
|
|
|
|
canvas.addEventListener('mousemove'.str, fn [mut state] (event JS.Event) {
|
2021-10-29 10:23:40 +02:00
|
|
|
if state.drawing {
|
2021-11-20 20:28:11 +01: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 22:18:09 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-20 20:28:11 +01:00
|
|
|
}, JS.EventListenerOptions{})
|
2021-10-27 22:18:09 +02:00
|
|
|
|
2021-11-20 20:28:11 +01:00
|
|
|
window.addEventListener('mouseup'.str, fn [mut state] (event JS.Event) {
|
2021-10-29 10:23:40 +02:00
|
|
|
if state.drawing {
|
2021-11-20 20:28:11 +01:00
|
|
|
match event {
|
|
|
|
JS.MouseEvent {
|
|
|
|
draw_line(mut state.context, state.x, state.y, int(event.offsetX),
|
|
|
|
int(event.offsetY))
|
|
|
|
}
|
|
|
|
else {}
|
2021-10-27 22:18:09 +02:00
|
|
|
}
|
2021-10-29 10:23:40 +02:00
|
|
|
state.x = 0
|
|
|
|
state.y = 0
|
|
|
|
state.drawing = false
|
2021-10-27 22:18:09 +02:00
|
|
|
}
|
2021-11-20 20:28:11 +01: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 10:41:33 +01:00
|
|
|
}
|