Visitar URL original
08 - Fun with HTML5 Canvas - On resize set canvas size · wesbos/JavaScript30@6053dc1 · GitHub
Skip to content

Commit 6053dc1

Browse files
committed
08 - Fun with HTML5 Canvas - On resize set canvas size
1 parent a185a2d commit 6053dc1

1 file changed

Lines changed: 16 additions & 20 deletions

File tree

‎src/08 - Fun with HTML5 Canvas/index-START.js‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
document.addEventListener('DOMContentLoaded', () => {
2-
let canvas
3-
let ctx
42

5-
canvas = document.querySelector('#draw')
6-
ctx = canvas.getContext('2d')
7-
8-
// _setCanvasSize()
9-
// _registerEvents()
3+
_setCanvasSize()
4+
_registerEvents()
105
_initializeContext()
116

127
function _registerEvents () {
@@ -16,32 +11,33 @@ document.addEventListener('DOMContentLoaded', () => {
1611
}
1712

1813
function _setCanvasSize () {
19-
let width = window.innerWidth - 2
20-
let height = window.innerHeight
21-
canvas.width = width
22-
canvas.height = height - 6
14+
const canvas = document.querySelector('#draw')
15+
canvas.width = window.innerWidth
16+
canvas.height = window.innerHeight
2317
}
2418

25-
let isDrawing = false
2619
let lastX = 0
2720
let lastY = 0
2821

2922
function _initializeContext () {
23+
const canvas = document.querySelector('#draw')
24+
const ctx = canvas.getContext('2d')
3025
ctx.strokeStyle = '#BADASS'
3126
ctx.lineCap = 'round'
3227
ctx.lineJoin = 'round'
3328

34-
document.querySelector('#draw').addEventListener('mousemove', (ev) => {
35-
console.log(ev)
36-
})
37-
// canvas.addEventListener('mousedown', () => { isDrawing = true })
38-
// canvas.addEventListener('mouseup', () => { isDrawing = false })
39-
// canvas.addEventListener('mouseout', () => { console.log(`--hello--`) })
29+
let isDrawing = false
30+
31+
canvas.addEventListener('mousemove', (ev) => _draw(ev, isDrawing))
32+
canvas.addEventListener('mousedown', () => { isDrawing = true })
33+
canvas.addEventListener('mouseup', () => { isDrawing = false })
34+
canvas.addEventListener('mouseout', () => { isDrawing = false })
4035
}
4136

42-
function _draw (ev) {
37+
function _draw (ev, isDrawing) {
4338
if (isDrawing) {
44-
console.log(`--ev--`); console.log(ev) // DEBUG
39+
console.log(`--ev--`);
40+
console.log(ev) // DEBUG
4541
}
4642
}
4743
})

0 commit comments

Comments
 (0)