Skip to content

Commit b9e9808

Browse files
Lección wesbos#8
1 parent 89c8427 commit b9e9808

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,53 @@
55
<title>HTML5 Canvas</title>
66
</head>
77
<body>
8-
<canvas id="draw" width="800" height="800"></canvas>
8+
<canvas id="draw" width="1000" height="1000"></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.lineWidth = 100;
18+
19+
let isDrawing = false;
20+
let lastX = 0;
21+
let lastY = 0;
22+
let hue = 0;
23+
let direction = true;
24+
function draw(e){
25+
if (!isDrawing) return;
26+
console.log(e);
27+
//cambio de color
28+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
29+
ctx.beginPath();
30+
ctx.moveTo(lastX, lastY);
31+
ctx.lineTo(e.offsetX, e.offsetY);
32+
ctx.stroke();
33+
[lastX, lastY] = [e.offsetX, e.offsetY];
34+
hue++;
35+
if (hue >= 360) {
36+
hue = 0;
37+
}
38+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
39+
direction = !direction;
40+
}
41+
if(direction) {
42+
ctx.lineWidth++;
43+
} else {
44+
ctx.lineWidth--;
45+
}
46+
47+
}
48+
canvas.addEventListener('mousedown', (e) =>{
49+
isDrawing = true;
50+
[lastX, lastY] = [e.offsetX, e.offsetY];
51+
});
52+
canvas.addEventListener('mousemove', draw);
53+
canvas.addEventListener('mouseup', () => isDrawing = false);
54+
canvas.addEventListener('mouseout', () => isDrawing = false);
1055
</script>
1156

1257
<style>

0 commit comments

Comments
 (0)