File tree Expand file tree Collapse file tree 2 files changed +75
-19
lines changed
08 - Fun with HTML5 Canvas Expand file tree Collapse file tree 2 files changed +75
-19
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > HTML5 Canvas</ title >
6+ </ head >
7+ < body >
8+ < canvas id ="draw " width ="800 " height ="800 ">
9+
10+ </ canvas >
11+ < script >
12+ const canvas = document . querySelector ( "#draw" ) ;
13+ const ctx = canvas . getContext ( '2d' ) ;
14+
15+ canvas . width = window . innerWidth ;
16+ canvas . height = window . innerHeight ;
17+
18+ ctx . strokeStyle = '#BADA55' ;
19+ ctx . lineJoin = 'round' ;
20+ ctx . lineCap = 'round' ;
21+ ctx . lineWidth = 100 ;
22+ ctx . globalCompositeOperation = 'multiply' ;
23+
24+ let hue = 0 ;
25+ let direction = true ;
26+
27+ let isDrawing = false ;
28+ let lastX = 0 ;
29+ let lastY = 0 ;
30+
31+ function draw ( e ) {
32+ if ( ! isDrawing ) {
33+ return ;
34+ }
35+ ctx . strokeStyle = `hsl(${ hue } , 100%, 50%)` ;
36+ ctx . beginPath ( ) ;
37+ ctx . moveTo ( lastX , lastY ) ;
38+ ctx . lineTo ( e . offsetX , e . offsetY ) ;
39+ ctx . stroke ( ) ;
40+
41+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
42+
43+ hue ++ ;
44+ if ( hue >= 360 ) {
45+ hue = 0 ;
46+ }
47+ if ( ctx . lineWidth > 100 || ctx . lineWidth <= 1 ) {
48+ direction = ! direction ;
49+ }
50+ if ( direction ) {
51+ ctx . lineWidth ++ ;
52+ } else {
53+ ctx . lineWidth -- ;
54+ }
55+
56+ }
57+
58+ canvas . addEventListener ( 'mousemove' , draw ) ;
59+ canvas . addEventListener ( 'mousedown' , ( e ) => {
60+ isDrawing = true ;
61+ [ lastX , lastY ] = [ e . offsetX , e . offsetY ] ;
62+ } ) ;
63+ canvas . addEventListener ( 'mouseup' , ( ) => isDrawing = false ) ;
64+ canvas . addEventListener ( 'mouseout' , ( ) => isDrawing = false ) ;
65+
66+ </ script >
67+
68+ < style >
69+ html , body {
70+ margin : 0 ;
71+ }
72+ </ style >
73+
74+ </ body >
75+ </ html >
You can’t perform that action at this time.
0 commit comments