File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
08 - Fun with HTML5 Canvas Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 77< body >
88< canvas id ="draw " width ="800 " height ="800 "> </ canvas >
99< script >
10+ console . log ( 'hi' ) ;
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+ ctx . globalCompositeOperation = 'multiply' ;
20+
21+ let isDrawing = false ;
22+ let lastX = 0 ;
23+ let lastY = 0 ;
24+ let hue = 0 ;
25+ let direction = true ;
26+
27+ function draw ( e ) {
28+ if ( ! isDrawing ) return ; // stop the function from running when they are not moused
29+ console . log ( e ) ;
30+ ctx . strokeStyle = `hsl(${ hue } , 100%, 50%)`
31+ ctx . beginPath ( ) ;
32+ //start from
33+ ctx . moveTo ( lastX , lastY ) ;
34+ //go to
35+ ctx . lineTo ( e . offsetX , e . offsetY ) ;
36+ ctx . stroke ( ) ;
37+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
38+ hue ++ ;
39+ if ( hue >= 360 ) {
40+ hue = 0 ;
41+ }
42+
43+ if ( ctx . lineWidth >= 100 || ctx . lineWidth <= 1 ) {
44+ direction = ! direction ;
45+ }
46+ if ( direction ) {
47+ ctx . lineWidth ++ ;
48+ } else {
49+ ctx . lineWidth -- ;
50+ }
51+ }
52+
53+ canvas . addEventListener ( 'mousedown' , ( e ) => {
54+ isDrawing = true ;
55+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
56+ } ) ;
57+
58+ canvas . addEventListener ( 'mousemove' , draw ) ;
59+ canvas . addEventListener ( 'mouseup' , ( ) => isDrawing = false ) ;
60+ canvas . addEventListener ( 'mouseout' , ( ) => isDrawing = false ) ;
1061</ script >
1162
1263< style >
You can’t perform that action at this time.
0 commit comments