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