@@ -13,20 +13,20 @@ import makePassiveEventOption from "./makePassiveEventOption";
13
13
function midPointBtw ( p1 , p2 ) {
14
14
return {
15
15
x : p1 . x + ( p2 . x - p1 . x ) / 2 ,
16
- y : p1 . y + ( p2 . y - p1 . y ) / 2
16
+ y : p1 . y + ( p2 . y - p1 . y ) / 2 ,
17
17
} ;
18
18
}
19
19
20
20
const canvasStyle = {
21
21
display : "block" ,
22
- position : "absolute"
22
+ position : "absolute" ,
23
23
} ;
24
24
25
25
const canvasTypes = [ "grid" , "drawing" , "temp" , "interface" ] ;
26
26
27
27
const dimensionsPropTypes = PropTypes . oneOfType ( [
28
28
PropTypes . number ,
29
- PropTypes . string
29
+ PropTypes . string ,
30
30
] ) ;
31
31
32
32
const boundsProp = PropTypes . shape ( {
@@ -114,7 +114,7 @@ export default class CanvasDraw extends PureComponent {
114
114
this . interactionSM = new DefaultState ( ) ;
115
115
this . coordSystem = new CoordinateSystem ( {
116
116
scaleExtents : props . zoomExtents ,
117
- documentSize : { width : props . canvasWidth , height : props . canvasHeight }
117
+ documentSize : { width : props . canvasWidth , height : props . canvasHeight } ,
118
118
} ) ;
119
119
this . coordSystem . attachViewChangeListener ( this . applyView . bind ( this ) ) ;
120
120
}
@@ -132,7 +132,7 @@ export default class CanvasDraw extends PureComponent {
132
132
} ;
133
133
134
134
eraseAll = ( ) => {
135
- this . erasedLines . push ( [ ...this . lines ] ) ;
135
+ this . erasedLines . push ( [ ...this . lines ] ) ;
136
136
this . clearExceptErasedLines ( ) ;
137
137
this . triggerOnChange ( ) ;
138
138
} ;
@@ -156,10 +156,15 @@ export default class CanvasDraw extends PureComponent {
156
156
return JSON . stringify ( {
157
157
lines : this . lines ,
158
158
width : this . props . canvasWidth ,
159
- height : this . props . canvasHeight
159
+ height : this . props . canvasHeight ,
160
160
} ) ;
161
161
} ;
162
162
163
+ getDataURL = ( ) => {
164
+ //generates the image data url (JPG format) from the HTML5 canvas element
165
+ return this . canvas . drawing . toDataURL ( ) ;
166
+ } ;
167
+
163
168
loadSaveData = ( saveData , immediate = this . props . immediateLoading ) => {
164
169
if ( typeof saveData !== "string" ) {
165
170
throw new Error ( "saveData needs to be of type string!" ) ;
@@ -179,7 +184,7 @@ export default class CanvasDraw extends PureComponent {
179
184
) {
180
185
this . simulateDrawingLines ( {
181
186
lines,
182
- immediate
187
+ immediate,
183
188
} ) ;
184
189
} else {
185
190
// we need to rescale the lines based on saved & current dimensions
@@ -188,15 +193,15 @@ export default class CanvasDraw extends PureComponent {
188
193
const scaleAvg = ( scaleX + scaleY ) / 2 ;
189
194
190
195
this . simulateDrawingLines ( {
191
- lines : lines . map ( line => ( {
196
+ lines : lines . map ( ( line ) => ( {
192
197
...line ,
193
- points : line . points . map ( p => ( {
198
+ points : line . points . map ( ( p ) => ( {
194
199
x : p . x * scaleX ,
195
- y : p . y * scaleY
200
+ y : p . y * scaleY ,
196
201
} ) ) ,
197
- brushRadius : line . brushRadius * scaleAvg
202
+ brushRadius : line . brushRadius * scaleAvg ,
198
203
} ) ) ,
199
- immediate
204
+ immediate,
200
205
} ) ;
201
206
}
202
207
} ;
@@ -211,8 +216,8 @@ export default class CanvasDraw extends PureComponent {
211
216
enabled : true ,
212
217
initialPoint : {
213
218
x : window . innerWidth / 2 ,
214
- y : window . innerHeight / 2
215
- }
219
+ y : window . innerHeight / 2 ,
220
+ } ,
216
221
} ) ;
217
222
this . chainLength = this . props . lazyRadius * window . devicePixelRatio ;
218
223
@@ -248,11 +253,12 @@ export default class CanvasDraw extends PureComponent {
248
253
// Attach our wheel event listener here instead of in the render so that we can specify a non-passive listener.
249
254
// This is necessary to prevent the default event action on chrome.
250
255
// https://github.com/facebook/react/issues/14856
251
- this . canvas . interface && this . canvas . interface . addEventListener (
252
- "wheel" ,
253
- this . handleWheel ,
254
- makePassiveEventOption ( )
255
- ) ;
256
+ this . canvas . interface &&
257
+ this . canvas . interface . addEventListener (
258
+ "wheel" ,
259
+ this . handleWheel ,
260
+ makePassiveEventOption ( )
261
+ ) ;
256
262
}
257
263
258
264
componentDidUpdate ( prevProps ) {
@@ -279,7 +285,8 @@ export default class CanvasDraw extends PureComponent {
279
285
280
286
componentWillUnmount = ( ) => {
281
287
this . canvasObserver . unobserve ( this . canvasContainer ) ;
282
- this . canvas . interface && this . canvas . interface . removeEventListener ( "wheel" , this . handleWheel ) ;
288
+ this . canvas . interface &&
289
+ this . canvas . interface . removeEventListener ( "wheel" , this . handleWheel ) ;
283
290
} ;
284
291
285
292
render ( ) {
@@ -292,20 +299,20 @@ export default class CanvasDraw extends PureComponent {
292
299
touchAction : "none" ,
293
300
width : this . props . canvasWidth ,
294
301
height : this . props . canvasHeight ,
295
- ...this . props . style
302
+ ...this . props . style ,
296
303
} }
297
- ref = { container => {
304
+ ref = { ( container ) => {
298
305
if ( container ) {
299
306
this . canvasContainer = container ;
300
307
}
301
308
} }
302
309
>
303
- { canvasTypes . map ( ( { name, zIndex } ) => {
310
+ { canvasTypes . map ( ( name ) => {
304
311
const isInterface = name === "interface" ;
305
312
return (
306
313
< canvas
307
314
key = { name }
308
- ref = { canvas => {
315
+ ref = { ( canvas ) => {
309
316
if ( canvas ) {
310
317
this . canvas [ name ] = canvas ;
311
318
this . ctx [ name ] = canvas . getContext ( "2d" ) ;
@@ -314,7 +321,7 @@ export default class CanvasDraw extends PureComponent {
314
321
}
315
322
}
316
323
} }
317
- style = { { ...canvasStyle , zIndex } }
324
+ style = { { ...canvasStyle } }
318
325
onMouseDown = { isInterface ? this . handleDrawStart : undefined }
319
326
onMouseMove = { isInterface ? this . handleDrawMove : undefined }
320
327
onMouseUp = { isInterface ? this . handleDrawEnd : undefined }
@@ -332,21 +339,21 @@ export default class CanvasDraw extends PureComponent {
332
339
333
340
///// Event Handlers
334
341
335
- handleWheel = e => {
342
+ handleWheel = ( e ) => {
336
343
this . interactionSM = this . interactionSM . handleMouseWheel ( e , this ) ;
337
344
} ;
338
345
339
- handleDrawStart = e => {
346
+ handleDrawStart = ( e ) => {
340
347
this . interactionSM = this . interactionSM . handleDrawStart ( e , this ) ;
341
348
this . mouseHasMoved = true ;
342
349
} ;
343
350
344
- handleDrawMove = e => {
351
+ handleDrawMove = ( e ) => {
345
352
this . interactionSM = this . interactionSM . handleDrawMove ( e , this ) ;
346
353
this . mouseHasMoved = true ;
347
354
} ;
348
355
349
- handleDrawEnd = e => {
356
+ handleDrawEnd = ( e ) => {
350
357
this . interactionSM = this . interactionSM . handleDrawEnd ( e , this ) ;
351
358
this . mouseHasMoved = true ;
352
359
} ;
@@ -356,11 +363,13 @@ export default class CanvasDraw extends PureComponent {
356
363
return ;
357
364
}
358
365
359
- canvasTypes . map ( ( { name } ) => this . ctx [ name ] ) . forEach ( ctx => {
360
- this . clearWindow ( ctx ) ;
361
- const m = this . coordSystem . transformMatrix ;
362
- ctx . setTransform ( m . a , m . b , m . c , m . d , m . e , m . f ) ;
363
- } ) ;
366
+ canvasTypes
367
+ . map ( ( { name } ) => this . ctx [ name ] )
368
+ . forEach ( ( ctx ) => {
369
+ this . clearWindow ( ctx ) ;
370
+ const m = this . coordSystem . transformMatrix ;
371
+ ctx . setTransform ( m . a , m . b , m . c , m . d , m . e , m . f ) ;
372
+ } ) ;
364
373
365
374
if ( ! this . deferRedrawOnViewChange ) {
366
375
this . drawGrid ( this . ctx . grid ) ;
@@ -409,7 +418,9 @@ export default class CanvasDraw extends PureComponent {
409
418
} ;
410
419
411
420
redrawImage = ( ) => {
412
- this . image && this . image . complete && drawImage ( { ctx : this . ctx . grid , img : this . image } ) ;
421
+ this . image &&
422
+ this . image . complete &&
423
+ drawImage ( { ctx : this . ctx . grid , img : this . image } ) ;
413
424
} ;
414
425
415
426
simulateDrawingLines = ( { lines, immediate } ) => {
@@ -418,7 +429,7 @@ export default class CanvasDraw extends PureComponent {
418
429
let curTime = 0 ;
419
430
let timeoutGap = immediate ? 0 : this . props . loadTimeOffset ;
420
431
421
- lines . forEach ( line => {
432
+ lines . forEach ( ( line ) => {
422
433
const { points, brushColor, brushRadius } = line ;
423
434
424
435
// Draw all at once if immediate flag is set, instead of using setTimeout
@@ -427,7 +438,7 @@ export default class CanvasDraw extends PureComponent {
427
438
this . drawPoints ( {
428
439
points,
429
440
brushColor,
430
- brushRadius
441
+ brushRadius,
431
442
} ) ;
432
443
433
444
// Save line with the drawn points
@@ -443,7 +454,7 @@ export default class CanvasDraw extends PureComponent {
443
454
this . drawPoints ( {
444
455
points : points . slice ( 0 , i + 1 ) ,
445
456
brushColor,
446
- brushRadius
457
+ brushRadius,
447
458
} ) ;
448
459
} , curTime ) ;
449
460
}
@@ -500,7 +511,7 @@ export default class CanvasDraw extends PureComponent {
500
511
this . lines . push ( {
501
512
points : [ ...this . points ] ,
502
513
brushColor : brushColor || this . props . brushColor ,
503
- brushRadius : brushRadius || this . props . brushRadius
514
+ brushRadius : brushRadius || this . props . brushRadius ,
504
515
} ) ;
505
516
506
517
// Reset points array
@@ -510,7 +521,10 @@ export default class CanvasDraw extends PureComponent {
510
521
this . inClientSpace ( [ this . ctx . drawing , this . ctx . temp ] , ( ) => {
511
522
this . ctx . drawing . drawImage (
512
523
this . canvas . temp ,
513
- 0 , 0 , this . canvas . drawing . width , this . canvas . drawing . height
524
+ 0 ,
525
+ 0 ,
526
+ this . canvas . drawing . width ,
527
+ this . canvas . drawing . height
514
528
) ;
515
529
} ) ;
516
530
@@ -524,8 +538,10 @@ export default class CanvasDraw extends PureComponent {
524
538
this . props . onChange && this . props . onChange ( this ) ;
525
539
} ;
526
540
527
- clearWindow = ctx => {
528
- this . inClientSpace ( [ ctx ] , ( ) => ctx . clearRect ( 0 , 0 , ctx . canvas . width , ctx . canvas . height ) ) ;
541
+ clearWindow = ( ctx ) => {
542
+ this . inClientSpace ( [ ctx ] , ( ) =>
543
+ ctx . clearRect ( 0 , 0 , ctx . canvas . width , ctx . canvas . height )
544
+ ) ;
529
545
} ;
530
546
531
547
clearExceptErasedLines = ( ) => {
@@ -553,15 +569,22 @@ export default class CanvasDraw extends PureComponent {
553
569
} ;
554
570
555
571
inClientSpace = ( ctxs , action ) => {
556
- ctxs . forEach ( ctx => {
572
+ ctxs . forEach ( ( ctx ) => {
557
573
ctx . save ( ) ;
558
- ctx . setTransform ( IDENTITY . a , IDENTITY . b , IDENTITY . c , IDENTITY . d , IDENTITY . e , IDENTITY . f ) ;
574
+ ctx . setTransform (
575
+ IDENTITY . a ,
576
+ IDENTITY . b ,
577
+ IDENTITY . c ,
578
+ IDENTITY . d ,
579
+ IDENTITY . e ,
580
+ IDENTITY . f
581
+ ) ;
559
582
} ) ;
560
583
561
584
try {
562
585
action ( ) ;
563
586
} finally {
564
- ctxs . forEach ( ctx => ctx . restore ( ) ) ;
587
+ ctxs . forEach ( ( ctx ) => ctx . restore ( ) ) ;
565
588
}
566
589
} ;
567
590
@@ -581,7 +604,7 @@ export default class CanvasDraw extends PureComponent {
581
604
this . image . src = this . props . imgSrc ;
582
605
} ;
583
606
584
- drawGrid = ctx => {
607
+ drawGrid = ( ctx ) => {
585
608
if ( this . props . hideGrid ) return ;
586
609
587
610
this . clearWindow ( ctx ) ;
0 commit comments