Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fe69ede

Browse filesBrowse files
committed
Add script to draw on canvas with variable-width rainbow pen
1 parent f7e2f61 commit fe69ede
Copy full SHA for fe69ede

2 files changed

+90Lines changed: 90 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+11Lines changed: 11 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#Fun with HTML5 Canvas
2+
3+
Draw with a rainbow pen on the canvas.
4+
5+
- Hold primary mouse button down to draw.
6+
- Line width varies directly with mouse speed.
7+
- Double-click to erase the canvas.
8+
9+
Have fun!
10+
11+
Collapse file
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>HTML5 Canvas</title>
7+
<link rel="icon" href="https://fav.farm/🔥" />
8+
</head>
9+
10+
<body>
11+
<canvas id="draw" width="800" height="800"></canvas>
12+
13+
<script>
14+
15+
const canvas = document.querySelector('canvas');
16+
const ctx = canvas.getContext('2d');
17+
18+
[canvas.width, canvas.height] = [window.innerWidth, window.innerHeight];
19+
ctx.strokeStyle = 'crimson';
20+
ctx.lineJoin = 'round'; // round, bevel, miter (default)
21+
ctx.lineCap = 'round'; // butt (default), round, square
22+
23+
let isDrawing = false;
24+
let [lastX, lastY] = [0, 0];
25+
let hue = 0;
26+
let saturation = 1;
27+
let lightness = 5;
28+
29+
function draw(e) {
30+
if (!isDrawing) return;
31+
32+
// set lineWidth based on mouse speed
33+
if (e.movementX !== 0 || e.movementY !== 0) {
34+
ctx.lineWidth = Math.sqrt(e.movementX ** 2 + e.movementY ** 2);
35+
} else {
36+
ctx.lineWidth = 1;
37+
}
38+
// rainbow pen
39+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
40+
// draw line
41+
ctx.beginPath();
42+
ctx.moveTo(lastX, lastY);
43+
ctx.lineTo(e.offsetX, e.offsetY);
44+
ctx.stroke();
45+
[lastX, lastY] = [e.offsetX, e.offsetY];
46+
hue = ++hue % 360;
47+
}
48+
49+
canvas.addEventListener('mousedown', (e) => {
50+
isDrawing = true;
51+
[lastX, lastY] = [e.offsetX, e.offsetY];
52+
});
53+
canvas.addEventListener('mouseover', (e) => {
54+
if (e.buttons & 1) isDrawing = true;
55+
[lastX, lastY] = [e.offsetX, e.offsetY];
56+
});
57+
canvas.addEventListener('mousemove', draw);
58+
canvas.addEventListener('mouseup', () => isDrawing = false);
59+
canvas.addEventListener('mouseout', () => isDrawing = false);
60+
// erase canvas on double-click
61+
canvas.addEventListener('dblclick', () => {
62+
ctx.reset();
63+
ctx.lineJoin = 'round';
64+
ctx.lineCap = 'round';
65+
66+
});
67+
68+
</script>
69+
70+
<style>
71+
html,
72+
body {
73+
margin: 0;
74+
}
75+
</style>
76+
77+
</body>
78+
79+
</html>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.