Skip to content

Commit 76cebcb

Browse files
Eric LeeEric Lee
authored andcommitted
Finished project and added comments
1 parent 377471c commit 76cebcb

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Goal:
2+
3+
Learn how to use HTML5 Canvas.
4+
5+
## Summary:
6+
7+
Canvas has many features. Once you gain access to the canvas through querySelector, get the 2d context, and set the width and height, you can edit the attributes of ctx in order to edit and change the canvas.
8+
9+
ctx.beginPath() - begins path, or resets to a new path
10+
ctx.moveTo(lastX, lastY) - moves the coordinates the path is to the given coordinates
11+
ctx.lineTo(e.offsetX, e.offsetY) - Creates a line to the given coordinate from the last asserted point
12+
ctx.stroke() - draws the path that is defined.
13+
ctx.lineWidth = 100 - change the width of the line.
14+
15+
## One Thing Learned:
16+
17+
The context of a contains all the information you need to change and edit, whether manually or through methods.
18+

08 - Fun with HTML5 Canvas/index-START.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,60 @@
77
<body>
88
<canvas id="draw" width="800" height="800"></canvas>
99
<script>
10+
const canvas = document.querySelector('#draw');
11+
const ctx = canvas.getContext('2d');
12+
canvas.width = window.innerWidth;
13+
canvas.height = window.innerHeight;
14+
ctx.strokeStyle = '#BADA55';
15+
ctx.lineJoin = 'round';
16+
ctx.lineCap = 'round';
17+
// ctx.globalCompositeOperation = 'multiply';
18+
19+
let isDrawing = false;
20+
let lastX = 0;
21+
let lastY = 0;
22+
let hue = 0;
23+
let direction = true;
24+
25+
ctx.lineWidth = 100;
26+
27+
function draw(e) {
28+
if (!isDrawing) return;
29+
30+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
31+
// ctx.lineWidth = hue;
32+
ctx.beginPath();
33+
ctx.moveTo(lastX, lastY);
34+
ctx.lineTo(e.offsetX, e.offsetY);
35+
ctx.stroke();
36+
37+
[lastX, lastY] = [e.offsetX, e.offsetY];
38+
hue++;
39+
if(hue > 360) {
40+
hue = 0;
41+
}
42+
43+
if(ctx.lineWidth >=100 || ctx.lineWidth <= 1) {
44+
direction = !direction;
45+
}
46+
if(direction) {
47+
ctx.lineWidth++;
48+
} else {
49+
ctx.lineWidth--;
50+
}
51+
// lastX = e.offSetX;
52+
// lastY = e.offSetY;
53+
}
54+
55+
canvas.addEventListener('mousedown', (e) => {
56+
isDrawing = true;
57+
[lastX, lastY] = [e.offsetX, e.offsetY];
58+
59+
});
60+
canvas.addEventListener('mousemove', draw);
61+
canvas.addEventListener('mouseup', () => isDrawing = false);
62+
canvas.addEventListener('mouseout', () => isDrawing = false);
63+
1064
</script>
1165

1266
<style>

0 commit comments

Comments
 (0)