File tree 1 file changed +37
-1
lines changed
08 - Fun with HTML5 Canvas
1 file changed +37
-1
lines changed Original file line number Diff line number Diff line change 15
15
canvas . width = window . innerWidth ;
16
16
canvas . height = window . innerHeight ;
17
17
ctx . strokeStyle = '#BADA55' ;
18
+ ctx . lineJoin = 'round' ;
18
19
ctx . lineCap = 'round' ;
20
+ ctx . lineWidth = 100 ;
21
+ ctx . globalCompositeOperation = 'screen' ;
19
22
20
23
let isDrawing = false ;
24
+ let direction = true ;
21
25
let lastX = 0 ;
22
26
let lastY = 0 ;
27
+ let hue = 0 ;
23
28
24
29
function draw ( e ) {
25
- console . log ( 'e' , e ) ;
30
+ if ( ! isDrawing ) {
31
+ return ;
32
+ }
33
+ ctx . strokeStyle = `hsl(${ hue } , 100%, 50%)` ;
34
+ ctx . beginPath ( ) ;
35
+ // start from:
36
+ ctx . moveTo ( lastX , lastY ) ;
37
+ // go to:
38
+ ctx . lineTo ( e . offsetX , e . offsetY ) ;
39
+ ctx . stroke ( ) ;
40
+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
41
+ if ( hue > 360 ) hue = 0 ;
42
+ hue += 7 ;
43
+
44
+ if ( ctx . lineWidth >= 100 || ctx . lineWidth <= 1 ) {
45
+ direction = ! direction ;
46
+ }
47
+
48
+ if ( direction ) {
49
+ ctx . lineWidth += 3 ;
50
+ } else {
51
+ ctx . lineWidth -= 3 ;
52
+ }
53
+
26
54
}
27
55
56
+ canvas . addEventListener ( 'mousedown' , ( e ) => {
57
+ isDrawing = true
58
+ console . log ( '*' , e ) ;
59
+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
60
+ console . log ( 'x, y' , lastX , lastY ) ;
61
+ } ) ;
28
62
canvas . addEventListener ( 'mousemove' , draw ) ;
63
+ canvas . addEventListener ( 'mouseup' , ( ) => isDrawing = false ) ;
64
+ canvas . addEventListener ( 'mouseout' , ( ) => isDrawing = false ) ;
29
65
</ script >
30
66
31
67
< style >
You can’t perform that action at this time.
0 commit comments