File tree Expand file tree Collapse file tree 2 files changed +60
-1
lines changed
08 - Fun with HTML5 Canvas Expand file tree Collapse file tree 2 files changed +60
-1
lines changed Original file line number Diff line number Diff line change 3434 // }
3535 // })
3636
37- const isAdult = people . some ( person => ( ( new Date ( ) ) . getFullYear ( ) ) - person . year >= 19 ) ;
3837 console . log ( { isAdult} ) ;
3938 // Array.prototype.every() // is everyone 19 or older?
4039
40+ const allAdults = people . every ( person => ( ( new Date ( ) ) . getFullYear ( ) ) - person . year >= 19 ) ;
41+
4142 // Array.prototype.find()
4243 // Find is like filter, but instead returns just the one you are looking for
4344 // find the comment with the ID of 823423
45+ const comment = comments . find ( comment => comment . id === 823423 ) ;
46+
47+ console . log ( comment ) ;
4448
4549 // Array.prototype.findIndex()
4650 // Find the comment with this ID
4751 // delete the comment with the ID of 823423
52+ const index = comment . findIndex ( comment => comment . id === 823423 ) ;
53+ comment . splice ( index , 1 ) ;
54+
55+ const newComments = [
56+ comments . slice ( 0 , index ) ,
57+ comment . slice ( index + 1 )
58+ ]
4859
4960 </ script >
5061</ body >
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 . width = 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 down.
29+ console . log ( e ) ;
30+ ctx . strokeStyle = `hsl(${ hue } , 100%, 50%)` ;
31+ ctx . beginPath ( ) ;
32+ ctx . moveTo ( lastX , lastY ) ;
33+ ctx . lineTo ( e . offsetX , e . offsetY ) ;
34+ ctx . stroke ( ) ;
35+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ]
36+ hue ++ ;
37+ if ( hue >= 360 ) {
38+ hue = 0 ;
39+ }
40+ if ( ctx . lineWidth >= 100 || ctx . lineWidth <= 1 ) {
41+ direction = ! direction ;
42+ }
43+ if ( direction ) {
44+ ctx . lineWidth ++ ;
45+ } else {
46+ ctx . lineWidth -- ;
47+ }
48+ }
49+
50+ canvas . addEventListener ( 'mousedown' , ( e ) => {
51+ isDrawing = true ;
52+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
53+
54+ } ) ;
55+ canvas . addEventListener ( 'mousemove' , draw ) ;
56+ canvas . addEventListener ( 'mouseup' , ( ) => isDrawing = false ) ;
57+ canvas . addEventListener ( 'mouseout' , ( ) => isDrawing = false ) ;
58+
1159</ script >
1260
1361< style >
You can’t perform that action at this time.
0 commit comments