Skip to content

Commit 1543111

Browse files
committed
Complete Day 8 - Fun with HTML5 Canvas
1 parent f2bb212 commit 1543111

2 files changed

Lines changed: 51 additions & 32 deletions

File tree

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

Lines changed: 0 additions & 20 deletions
This file was deleted.

08 - Fun with HTML5 Canvas/index-FINISHED.html renamed to 08 - Fun with HTML5 Canvas/index.html

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>HTML5 Canvas</title>
6-
<link rel="icon" href="https://fav.farm/" />
6+
<link rel="icon" href="https://fav.farm/🔥" />
77
</head>
88
<body>
9+
<h1>Draw on the canvas!</h1>
10+
<button id="clear">Clear Canvas</button>
911
<canvas id="draw" width="800" height="800"></canvas>
1012
<script>
1113
const canvas = document.querySelector('#draw');
@@ -16,16 +18,17 @@
1618
ctx.lineJoin = 'round';
1719
ctx.lineCap = 'round';
1820
ctx.lineWidth = 100;
19-
// ctx.globalCompositeOperation = 'multiply';
21+
ctx.globalCompositeOperation = 'screen';
2022

21-
let isDrawing = false;
23+
let isDrawing = false;
2224
let lastX = 0;
2325
let lastY = 0;
24-
let hue = 0;
25-
let direction = true;
26+
let hue = 0;
27+
let direction = true;
28+
2629

2730
function draw(e) {
28-
if (!isDrawing) return; // stop the fn from running when they are not moused down
31+
if(!isDrawing) return; // stop the fn from running when they are not mouse down
2932
console.log(e);
3033
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
3134
ctx.beginPath();
@@ -40,16 +43,14 @@
4043
if (hue >= 360) {
4144
hue = 0;
4245
}
43-
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
46+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1){
4447
direction = !direction;
4548
}
46-
47-
if(direction) {
48-
ctx.lineWidth++;
49+
if(direction){
50+
ctx.lineWidth++;
4951
} else {
5052
ctx.lineWidth--;
5153
}
52-
5354
}
5455

5556
canvas.addEventListener('mousedown', (e) => {
@@ -62,12 +63,50 @@
6263
canvas.addEventListener('mouseup', () => isDrawing = false);
6364
canvas.addEventListener('mouseout', () => isDrawing = false);
6465

65-
</script>
66+
document.querySelector('#clear').addEventListener('click', () => {
67+
ctx.clearRect(0, 0, canvas.width, canvas.height);
68+
});
69+
6670

71+
</script>
6772
<style>
6873
html, body {
6974
margin: 0;
75+
background-color: black;
7076
}
77+
78+
h1 {
79+
position: absolute;
80+
text-align: center;
81+
top: 20px;
82+
left: 50%;
83+
transform: translateX(-50%);
84+
color: white;
85+
font-family: sans-serif;
86+
z-index: 10;
87+
pointer-events: none;
88+
background-color: black;
89+
width: 400px;
90+
}
91+
92+
#clear {
93+
position: absolute;
94+
top: 20px;
95+
right: 20px;
96+
padding: 10px 20px;
97+
background: white;
98+
border: none;
99+
border-radius: 5px;
100+
cursor: pointer;
101+
font-size: 1rem;
102+
z-index: 10;
103+
}
104+
105+
#clear:hover {
106+
background: black;
107+
color: white;
108+
transition: all 0.2s ease;
109+
}
71110
</style>
72111

73112
</body>

0 commit comments

Comments
 (0)