@@ -91,6 +91,50 @@ function getScaledCoords(e) {
91
91
return [ x / scaleFactor , y / scaleFactor ] ;
92
92
}
93
93
94
+ function drawCurrentPolygon ( cursorX , cursorY ) {
95
+ //ctx.clearRect(0, 0, canvas.width, canvas.height);
96
+ ctx . drawImage ( img , 0 , 0 ) ;
97
+ for ( var i = 0 ; i < points . length - 1 ; i ++ ) {
98
+ // draw arc around each point
99
+ ctx . beginPath ( ) ;
100
+ ctx . strokeStyle = rgb_color ;
101
+ ctx . arc ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , 5 , 0 , 2 * Math . PI ) ;
102
+ // fill with white
103
+ ctx . fillStyle = 'white' ;
104
+ ctx . fill ( ) ;
105
+ ctx . stroke ( ) ;
106
+ drawLine ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ i + 1 ] [ 0 ] , points [ i + 1 ] [ 1 ] ) ;
107
+ }
108
+
109
+ if ( ( points . length > 0 && drawMode == "polygon" ) || ( points . length > 0 && points . length < 2 && drawMode == "line" ) ) {
110
+ ctx . beginPath ( ) ;
111
+ ctx . strokeStyle = rgb_color ;
112
+ ctx . arc ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , 5 , 0 , 2 * Math . PI ) ;
113
+ // fill with white
114
+ ctx . fillStyle = 'white' ;
115
+ ctx . fill ( ) ;
116
+ ctx . stroke ( ) ;
117
+
118
+ if ( cursorX && cursorY ) {
119
+ drawLine ( points [ points . length - 1 ] [ 0 ] , points [ points . length - 1 ] [ 1 ] , cursorX , cursorY ) ;
120
+ }
121
+
122
+ if ( points . length == 2 && drawMode == "line" ) {
123
+ console . log ( "line" ) ;
124
+ // draw arc around each point
125
+ ctx . beginPath ( ) ;
126
+ ctx . strokeStyle = rgb_color ;
127
+ ctx . arc ( points [ 0 ] [ 0 ] , points [ 0 ] [ 1 ] , 5 , 0 , 2 * Math . PI ) ;
128
+ // fill with white
129
+ ctx . fillStyle = 'white' ;
130
+ ctx . fill ( ) ;
131
+ ctx . stroke ( ) ;
132
+ masterPoints . push ( points ) ;
133
+ points = [ ] ;
134
+ }
135
+ }
136
+ }
137
+
94
138
function drawAllPolygons ( ) {
95
139
// draw all points for previous regions
96
140
for ( var i = 0 ; i < masterPoints . length ; i ++ ) {
@@ -124,6 +168,15 @@ function drawAllPolygons () {
124
168
}
125
169
}
126
170
171
+ function getParentPoints ( ) {
172
+ var parentPoints = [ ] ;
173
+ for ( var i = 0 ; i < masterPoints . length ; i ++ ) {
174
+ parentPoints . push ( masterPoints [ i ] ) ;
175
+ }
176
+ parentPoints . push ( points ) ;
177
+ return parentPoints ;
178
+ }
179
+
127
180
function clearall ( ) {
128
181
ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
129
182
ctx . drawImage ( img , 0 , 0 ) ;
@@ -180,83 +233,25 @@ canvas.addEventListener('mousemove', function(e) {
180
233
ycoord . innerHTML = y ;
181
234
182
235
if ( canvas . style . cursor == 'crosshair' ) {
183
- //ctx.clearRect(0, 0, canvas.width, canvas.height);
184
- ctx . drawImage ( img , 0 , 0 ) ;
185
- for ( var i = 0 ; i < points . length - 1 ; i ++ ) {
186
- // draw arc around each point
187
- ctx . beginPath ( ) ;
188
- ctx . strokeStyle = rgb_color ;
189
- ctx . arc ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , 5 , 0 , 2 * Math . PI ) ;
190
- // fill with white
191
- ctx . fillStyle = 'white' ;
192
- ctx . fill ( ) ;
193
- ctx . stroke ( ) ;
194
- drawLine ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ i + 1 ] [ 0 ] , points [ i + 1 ] [ 1 ] ) ;
195
- }
196
- if ( ( points . length > 0 && drawMode == "polygon" ) || ( points . length > 0 && points . length < 2 && drawMode == "line" ) ) {
197
- ctx . beginPath ( ) ;
198
- ctx . strokeStyle = rgb_color ;
199
- ctx . arc ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , 5 , 0 , 2 * Math . PI ) ;
200
- // fill with white
201
- ctx . fillStyle = 'white' ;
202
- ctx . fill ( ) ;
203
- ctx . stroke ( ) ;
204
- drawLine ( points [ points . length - 1 ] [ 0 ] , points [ points . length - 1 ] [ 1 ] , x , y ) ;
205
-
206
- if ( points . length == 2 && drawMode == "line" ) {
207
- console . log ( "line" ) ;
208
- // draw arc around each point
209
- ctx . beginPath ( ) ;
210
- ctx . strokeStyle = rgb_color ;
211
- ctx . arc ( points [ 0 ] [ 0 ] , points [ 0 ] [ 1 ] , 5 , 0 , 2 * Math . PI ) ;
212
- // fill with white
213
- ctx . fillStyle = 'white' ;
214
- ctx . fill ( ) ;
215
- ctx . stroke ( ) ;
216
- masterPoints . push ( points ) ;
217
- points = [ ] ;
218
- }
219
- }
220
- var parentPoints = [ ] ;
221
-
222
- for ( var i = 0 ; i < masterPoints . length ; i ++ ) {
223
- parentPoints . push ( masterPoints [ i ] ) ;
224
- }
225
- parentPoints . push ( points ) ;
226
-
236
+ drawCurrentPolygon ( x , y )
227
237
drawAllPolygons ( ) ;
228
238
}
229
239
} ) ;
230
240
241
+
242
+
231
243
window . addEventListener ( 'keydown' , function ( e ) {
244
+ e . stopImmediatePropagation ( )
245
+ let validKey = false
246
+
232
247
if ( e . key === 'Enter' ) {
248
+ validKey = true
233
249
canvas . style . cursor = 'default' ;
234
- // remove line drawn by mouseover
235
- // ctx.clearRect(0, 0, canvas.width, canvas.height);
236
- // join the dots
237
- drawLine ( points [ 0 ] [ 0 ] , points [ 0 ] [ 1 ] , points [ points . length - 1 ] [ 0 ] , points [ points . length - 1 ] [ 1 ] ) ;
238
- // fill polygon with color
239
- if ( drawMode == 'polygon' ) {
240
- ctx . beginPath ( ) ;
241
- ctx . moveTo ( points [ 0 ] [ 0 ] , points [ 0 ] [ 1 ] ) ;
242
- ctx . fillStyle = opaque_color ;
243
- for ( var i = 1 ; i < points . length ; i ++ ) {
244
- ctx . lineTo ( points [ i ] [ 0 ] , points [ i ] [ 1 ] ) ;
245
- }
246
- ctx . closePath ( ) ;
247
- ctx . fill ( ) ;
248
- // draw line connecting last two points
249
- }
250
+
251
+ // save current polygon points
250
252
masterPoints . push ( points ) ;
251
- // draw arc around last point
252
- ctx . beginPath ( ) ;
253
- ctx . strokeStyle = rgb_color ;
254
- ctx . arc ( points [ points . length - 1 ] [ 0 ] , points [ points . length - 1 ] [ 1 ] , 5 , 0 , 2 * Math . PI ) ;
255
- // fill with white
256
- ctx . fillStyle = 'white' ;
257
- ctx . fill ( ) ;
258
- ctx . stroke ( ) ;
259
253
points = [ ] ;
254
+
260
255
// dont choose a color that has already been chosen
261
256
var remaining_choices = color_choices . filter ( function ( x ) {
262
257
return ! masterColors . includes ( x ) ;
@@ -267,9 +262,25 @@ window.addEventListener('keydown', function(e) {
267
262
}
268
263
269
264
rgb_color = remaining_choices [ Math . floor ( Math . random ( ) * remaining_choices . length ) ] ;
270
-
271
265
masterColors . push ( rgb_color ) ;
272
266
}
267
+
268
+ if ( e . key === 'Escape' ) {
269
+ validKey = true
270
+ points = [ ]
271
+ }
272
+
273
+ if ( e . key === 'z' && ( e . ctrlKey || e . metaKey ) ) {
274
+ validKey = true
275
+ points . pop ( )
276
+ }
277
+
278
+ if ( validKey ) {
279
+ drawCurrentPolygon ( )
280
+ drawAllPolygons ( ) ;
281
+ var parentPoints = getParentPoints ( ) ;
282
+ writePoints ( parentPoints ) ;
283
+ }
273
284
} ) ;
274
285
275
286
canvas . addEventListener ( 'drop' , function ( e ) {
@@ -327,6 +338,15 @@ function writePoints(parentPoints) {
327
338
parentPoints = normalized ;
328
339
}
329
340
341
+ // clean empty points
342
+ parentPoints = parentPoints . filter ( points => ! ! points . length ) ;
343
+
344
+ if ( ! parentPoints . length ) {
345
+ document . querySelector ( '#python' ) . innerHTML = '' ;
346
+ document . querySelector ( '#json' ) . innerHTML ;
347
+ return ;
348
+ }
349
+
330
350
// create np.array list
331
351
var code_template = `
332
352
[
@@ -339,6 +359,7 @@ ${points.map(function(point) {
339
359
} ) . join ( ',' ) }
340
360
]
341
361
` ;
362
+
342
363
document . querySelector ( '#python' ) . innerHTML = code_template ;
343
364
344
365
var json_template = `
@@ -378,28 +399,13 @@ canvas.addEventListener('click', function(e) {
378
399
}
379
400
380
401
ctx . arc ( x , y , 155 , 0 , 2 * Math . PI ) ;
381
- // concat all points into one array
382
- var parentPoints = [ ] ;
383
-
384
- for ( var i = 0 ; i < masterPoints . length ; i ++ ) {
385
- parentPoints . push ( masterPoints [ i ] ) ;
386
- }
387
- // add "points"
388
- parentPoints . push ( points ) ;
389
402
403
+ var parentPoints = getParentPoints ( ) ;
390
404
writePoints ( parentPoints ) ;
391
405
} ) ;
392
406
393
407
document . querySelector ( '#normalize_checkbox' ) . addEventListener ( 'change' , function ( e ) {
394
408
showNormalized = e . target . checked ;
395
- // normalize all
396
- var parentPoints = [ ] ;
397
-
398
- for ( var i = 0 ; i < masterPoints . length ; i ++ ) {
399
- parentPoints . push ( masterPoints [ i ] ) ;
400
- }
401
-
402
- parentPoints . push ( points ) ;
403
-
409
+ var parentPoints = getParentPoints ( ) ;
404
410
writePoints ( parentPoints ) ;
405
411
} ) ;
0 commit comments