File tree Expand file tree Collapse file tree
08 - Fun with HTML5 Canvas Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88< body >
99< canvas id ="draw " width ="800 " height ="800 "> </ canvas >
1010< script >
11+ const canvas = document . querySelector ( '#draw' ) ;
12+ const ctx = canvas . getContext ( '2d' ) ;
13+ canvas . width = window . innerWidth ;
14+ canvas . height = window . innerHeight ;
15+ ctx . strokeStyle = '#BADA55' ;
16+ ctx . lineJoin = 'round' ;
17+ ctx . lineCap = 'round' ;
18+ ctx . lineWidth = 100 ;
19+
20+ let isDrawing = false ;
21+ let lastX = 0 ;
22+ let lastY = 0 ;
23+
24+ function draw ( e ) {
25+ if ( ! isDrawing ) return ;
26+ console . log ( e ) ;
27+ ctx . beginPath ( ) ;
28+ ctx . moveTo ( lastX , lastY ) ;
29+ ctx . lineTo ( e . offsetX , e . offsetY ) ;
30+ ctx . stroke ( ) ;
31+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
32+ }
33+
34+ canvas . addEventListener ( 'mousemove' , draw ) ;
35+ canvas . addEventListener ( 'mousedown' , ( e ) => {
36+ isDrawing = true ;
37+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
38+ } ) ;
39+ canvas . addEventListener ( 'mouseup' , ( ) => isDrawing = false ) ;
40+ canvas . addEventListener ( 'mouseout' , ( ) => isDrawing = false ) ;
1141</ script >
1242
1343< style >
You can’t perform that action at this time.
0 commit comments