File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
08 - Fun with HTML5 Canvas Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+
4+ < head >
5+ < meta charset ="UTF-8 ">
6+ < title > HTML5 Canvas</ title >
7+ </ head >
8+
9+ < body >
10+ < canvas id ="draw " width ="800 " height ="800 "> </ canvas >
11+ < script >
12+ const canvas = document . querySelector ( '#draw' )
13+ canvas . width = window . innerWidth
14+ canvas . height = window . innerHeight
15+
16+ const context = canvas . getContext ( '2d' )
17+ context . strokeStyle = '#ff8800'
18+ context . lineJoin = 'round'
19+ context . lineCap = 'round'
20+
21+ let isDrawing = false
22+ let last = { offsetX : 0 , offsetY : 0 }
23+
24+ const draw = ( { offsetX : x , offsetY : y } ) => {
25+ if ( ! isDrawing ) return
26+ context . beginPath ( )
27+ context . moveTo ( last . x , last . y )
28+ context . lineTo ( x , y )
29+ context . stroke ( )
30+ last = { x, y }
31+ }
32+
33+ const startDrawing = ( { offsetX : x , offsetY : y } ) => {
34+ last = { x, y }
35+ isDrawing = true
36+ }
37+
38+ canvas . addEventListener ( 'mousemove' , draw )
39+ canvas . addEventListener ( 'mousedown' , startDrawing )
40+ canvas . addEventListener ( 'mouseup' , ( ) => isDrawing = false )
41+ canvas . addEventListener ( 'mouseout' , ( ) => isDrawing = false )
42+ </ script >
43+
44+ < style >
45+ html ,
46+ body {
47+ margin : 0 ;
48+ }
49+ </ style >
50+
51+ </ body >
52+
53+ </ html >
You can’t perform that action at this time.
0 commit comments