1+ const SNAP_DISTANCE = 20 ;
2+
13let alpha = 127 ;
24let fillColor = [ 0 , 0 , 0 , alpha ] ;
35let strokeColor = [ 0 , 0 , 0 , 255 ] ;
46let strokeW = 2 ;
57let shapeMode = 'ellipse' ;
68let lastClicked = { x : 0 , y : 0 } ;
9+ let shapePoints = [ ] ;
710
811let doc = {
912 backgroundColor : [ 255 , 255 , 255 ] ,
@@ -42,10 +45,20 @@ function draw() {
4245 strokeWeight ( shape . strokeW ) ;
4346 line ( shape . x1 , shape . y1 , shape . x2 , shape . y2 ) ;
4447 break ;
48+ case 'polyline' :
49+ stroke ( shape . strokeColor ) ;
50+ strokeWeight ( shape . strokeW ) ;
51+ fill ( shape . fillColor ) ;
52+ beginShape ( ) ;
53+ for ( let i = 0 ; i < shape . vertices . length ; i ++ ) {
54+ vertex ( shape . vertices [ i ] . x , shape . vertices [ i ] . y ) ;
55+ }
56+ endShape ( ) ;
57+ break ;
4558 }
4659 }
4760
48- // Draw shapes continuously
61+ // Draw shapes that we are currently manipulating/creating
4962 if ( mouseIsPressed ) {
5063 switch ( shapeMode ) {
5164 case 'ellipse' :
@@ -70,12 +83,40 @@ function draw() {
7083 break ;
7184 }
7285 }
86+
87+ // Draw polyline?
88+ if ( shapeMode === 'polyline' ) {
89+ stroke ( strokeColor ) ;
90+ strokeWeight ( strokeW ) ;
91+ fill ( fillColor ) ;
92+ beginShape ( ) ;
93+ for ( let i = 0 ; i < shapePoints . length ; i ++ ) {
94+ vertex ( shapePoints [ i ] . x , shapePoints [ i ] . y ) ;
95+ }
96+ vertex ( mouseX , mouseY ) ;
97+ endShape ( ) ;
98+ }
7399}
74100
75101function mousePressed ( ) {
76102 // console.log("Mouse pressed at " + mouseX + ", " + mouseY);
77103 lastClicked . x = mouseX ;
78104 lastClicked . y = mouseY ;
105+
106+ if ( shapeMode === 'polyline' ) {
107+ let shouldSnap = false ;
108+ if ( shapePoints . length > 0 &&
109+ dist ( mouseX , mouseY , shapePoints [ 0 ] . x , shapePoints [ 0 ] . y ) < SNAP_DISTANCE ) {
110+ shouldSnap = true ;
111+ }
112+
113+ if ( shouldSnap ) {
114+ shapePoints . push ( { x : shapePoints [ 0 ] . x , y : shapePoints [ 0 ] . y } ) ;
115+ addPolyToDocument ( ) ;
116+ } else {
117+ shapePoints . push ( { x : mouseX , y : mouseY } ) ;
118+ }
119+ }
79120}
80121
81122function mouseReleased ( ) {
@@ -118,7 +159,9 @@ function mouseReleased() {
118159 break ;
119160 }
120161
121- doc . shapes . push ( shape ) ;
162+ if ( shape !== undefined ) {
163+ doc . shapes . push ( shape ) ;
164+ }
122165}
123166
124167
@@ -130,6 +173,16 @@ function keyTyped() {
130173 shapeMode = 'rectangle' ;
131174 } else if ( key === 'l' ) {
132175 shapeMode = 'line' ;
176+ } else if ( key === 'p' ) {
177+ shapeMode = 'polyline' ;
178+ shapePoints = [ ] ;
179+ }
180+
181+ // SHAPE INTERACTIONS
182+ if ( keyCode === ENTER ) {
183+ if ( shapeMode === 'polyline' ) {
184+ addPolyToDocument ( ) ;
185+ }
133186 }
134187
135188 // SIZE
@@ -166,6 +219,7 @@ function keyTyped() {
166219 fillColor = [ 123 , 104 , 238 , alpha ] ;
167220 }
168221
222+
169223 // SAVING
170224 if ( key === 'f' ) {
171225 saveCanvas ( 'drawing_' + getDateTime ( ) , 'png' ) ;
@@ -174,9 +228,20 @@ function keyTyped() {
174228 }
175229}
176230
231+ function addPolyToDocument ( ) {
232+ let shape = {
233+ type : 'polyline' ,
234+ vertices : shapePoints ,
235+ fillColor : fillColor ,
236+ strokeColor : strokeColor ,
237+ strokeW : strokeW
238+ } ;
239+ doc . shapes . push ( shape ) ;
240+ shapePoints = [ ] ;
241+ }
177242
178243function parseFile ( file ) {
179- console . log ( file ) ;
244+ // console.log(file);
180245
181246 if ( file . subtype === 'json' ) {
182247 doc = file . data ;
0 commit comments