@@ -10,7 +10,7 @@ import {
10
10
} from './version' ;
11
11
12
12
import {
13
- getArg , toBytes
13
+ getArg , toBytes , fromBytes
14
14
} from './utils' ;
15
15
16
16
@@ -22,6 +22,18 @@ function getContext(canvas: HTMLCanvasElement) {
22
22
return context ;
23
23
}
24
24
25
+ function serializeImageData ( array : Uint8ClampedArray ) {
26
+ return new DataView ( array . buffer . slice ( 0 ) ) ;
27
+ }
28
+
29
+ function deserializeImageData ( dataview : DataView | null ) {
30
+ if ( dataview === null ) {
31
+ return null ;
32
+ }
33
+
34
+ return new Uint8ClampedArray ( dataview . buffer ) ;
35
+ }
36
+
25
37
26
38
export
27
39
class CanvasModel extends DOMWidgetModel {
@@ -41,9 +53,10 @@ class CanvasModel extends DOMWidgetModel {
41
53
42
54
static serializers : ISerializers = {
43
55
...DOMWidgetModel . serializers ,
44
- image_data : { serialize : ( bytes : Uint8ClampedArray ) => {
45
- return new DataView ( bytes . buffer . slice ( 0 ) ) ;
46
- } }
56
+ image_data : {
57
+ serialize : serializeImageData ,
58
+ deserialize : deserializeImageData
59
+ }
47
60
}
48
61
49
62
initialize ( attributes : any , options : any ) {
@@ -53,13 +66,24 @@ class CanvasModel extends DOMWidgetModel {
53
66
this . ctx = getContext ( this . canvas ) ;
54
67
55
68
this . resizeCanvas ( ) ;
69
+ this . drawImageData ( ) ;
56
70
57
71
this . on ( 'change:size' , this . resizeCanvas . bind ( this ) ) ;
58
72
this . on ( 'msg:custom' , this . onCommand . bind ( this ) ) ;
59
73
60
74
this . send ( { event : 'client_ready' } , { } ) ;
61
75
}
62
76
77
+ private async drawImageData ( ) {
78
+ if ( this . get ( 'sync_image_data' ) && this . get ( 'image_data' ) !== null ) {
79
+ const img = await fromBytes ( this . get ( 'image_data' ) ) ;
80
+
81
+ this . ctx . drawImage ( img , 0 , 0 ) ;
82
+
83
+ this . trigger ( 'new-frame' ) ;
84
+ }
85
+ }
86
+
63
87
private async onCommand ( command : any , buffers : any ) {
64
88
await this . processCommand ( command , buffers ) ;
65
89
@@ -328,7 +352,7 @@ class CanvasView extends DOMWidgetView {
328
352
}
329
353
330
354
updateCanvas ( ) {
331
- this . clear ( ) ;
355
+ this . clear ( ) ;
332
356
this . ctx . drawImage ( this . model . canvas , 0 , 0 ) ;
333
357
}
334
358
0 commit comments