The HTML canvas can be used for sketching/drawing by mouse. The canvas API provides a means for drawing graphics via JavaScript and the HTML element. It largely focuses on 2D graphics. In this post, I would like to describe how to quickly go about implementing this.

Mouse Canvas

The mouse canvas can be used with any devices (desktop computer/laptop..) that use mouse. Belowing is the simple example:

<iframe src="/javascript/canvas/mouse_canvas.html" style="width:100%; height:360px", title="add-default-marker"></iframe>

Source Code

The source code was referring to Mozilla mouse drawing mousemove with clear/line width/color features enhancement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// CSS Style
<style>
#canvas_div {
text-align: center;
display: block;
margin-left: auto;
margin-right: auto;
}
canvas {
border: 2px solid black;
}
</style>

<div id="canvas_div" style="overflow-x: auto;">
<canvas id="canvas" width="900" height="360"></canvas>

<button onclick="javascript:clearArea();return false;">Clear Area</button>

Line width : <select id="selWidth">
<option value="11">11</option>
<option value="13" selected="selected">13</option>
<option value="15">15</option>
</select>

Color : <select id="selColor">
<option value="black">black</option>
<option value="blue" selected="selected">blue</option>
<option value="red">red</option>
<option value="green">green</option>
<option value="yellow">yellow</option>
<option value="gray">gray</option>
</select>
</div>

<script>
// When true, moving the mouse draws on the canvas
let isDrawing = false;
let x = 0;
let y = 0;

const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');

// event.offsetX, event.offsetY gives the (x,y) offset from the edge of the canvas.

// Add the event listeners for mousedown, mousemove, and mouseup
canvas.addEventListener('mousedown', (e) => {
x = e.offsetX;
y = e.offsetY;
isDrawing = true;
});

canvas.addEventListener('mousemove', (e) => {
if (isDrawing) {
drawLine(context, x, y, e.offsetX, e.offsetY);
x = e.offsetX;
y = e.offsetY;
}
});

canvas.addEventListener('mouseup', (e) => {
if (isDrawing) {
drawLine(context, x, y, e.offsetX, e.offsetY);
x = 0;
y = 0;
isDrawing = false;
}
});

function drawLine(context, x1, y1, x2, y2) {
context.beginPath();
context.strokeStyle = document.getElementById('selColor').value;
context.lineWidth = document.getElementById('selWidth').value;
context.lineJoin = "round";
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.closePath();
context.stroke();
}

function clearArea() {
context.setTransform(1, 0, 0, 1, 0, 0);
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
}
</script>

Reference

Thanks for reading! Feel free to leave the comments below or email to me. Any pieces of advice or discussions are always welcome. :)