drawing = function() {
let w = canvasImage.width
let h = canvasImage.height
let canvas = DOM.canvas(w, h);
let context = canvas.getContext("2d");
let strokes = canvas.value = []
let fg = "white";
let bg = "black";
let stroke;
let time;
let newtime;
let strokeWidth = 5;
canvas.value.strokeWidth = strokeWidth
context.lineCap = "round"
context.lineJoin = "round"
canvas.onmousedown = event => {
stroke = []
time = +new Date()
strokes.push(stroke)
canvas.onmousemove(event);
};
canvas.onmouseup = event => {
stroke = null;
};
canvas.onwheel = function(event) {
strokeWidth *= 1 + event.wheelDelta / 10000;
event.preventDefault();
canvas.value.strokeWidth = strokeWidth
canvas.dispatchEvent(new CustomEvent("input"));
render()
var rect = canvas.getBoundingClientRect();
let ex = event.clientX - rect.left - 2
let ey = event.clientY - rect.top - 1
context.strokeStyle = fg
context.lineWidth = 1
context.beginPath()
context.arc(ex, ey, strokeWidth, 0, Math.PI * 2)
context.stroke()
context.strokeStyle = bg
context.lineWidth = 1
context.beginPath()
context.arc(ex, ey, strokeWidth - 1, 0, Math.PI * 2)
context.stroke()
}
canvas.onmousemove = event => {
var rect = canvas.getBoundingClientRect();
let ex = event.clientX - rect.left - 2
let ey = event.clientY - rect.top - 1
if (stroke != null) {
newtime = +new Date();
stroke.push([ex, ey, newtime - time]);
canvas.dispatchEvent(new CustomEvent("input"));
time = newtime;
}
render()
context.strokeStyle = fg
context.lineWidth = 1
context.beginPath()
context.arc(ex, ey, strokeWidth, 0, Math.PI * 2)
context.stroke()
context.strokeStyle = bg
context.lineWidth = 1
context.beginPath()
context.arc(ex, ey, strokeWidth - 1, 0, Math.PI * 2)
context.stroke()
};
function render() {
context.fillStyle = bg;
context.fillRect(0, 0, canvas.width, canvas.height);
if(!strokes.length)
context.drawImage(canvasImage, 0, 0);
context.lineWidth = strokeWidth * 2;
context.strokeStyle = fg
strokes.forEach(stroke => {
context.beginPath();
context.moveTo(stroke[0][0], stroke[0][1]);
stroke.slice(1).forEach(p => context.lineTo(p[0], p[1]));
context.stroke();
})
}
canvas.clear = function() {
strokes = canvas.value = []
strokes.strokeWidth = strokeWidth
canvas.dispatchEvent(new CustomEvent("input"));
render()
}
canvas.style.border = "solid 1px #ccc";
canvas.style.cursor = "crosshair"
render();
return canvas;
}