Skip to content

Commit c9bd22c

Browse files
committed
Draw synced data at canvas creation
Signed-off-by: martinRenou <[email protected]>
1 parent dfb9791 commit c9bd22c

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,18 @@ async function toBytes(canvas: HTMLCanvasElement) : Promise<Uint8ClampedArray> {
103103
reader.readAsArrayBuffer(blob);
104104
});
105105
}
106+
107+
export
108+
async function fromBytes(array: Uint8ClampedArray) : Promise<HTMLImageElement> {
109+
const blob = new Blob([array]);
110+
111+
return new Promise<HTMLImageElement>((resolve, reject) => {
112+
const img = new Image();
113+
114+
img.onload = () => {
115+
resolve(img);
116+
}
117+
118+
img.src = URL.createObjectURL(blob);
119+
});
120+
}

src/widget.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from './version';
1111

1212
import {
13-
getArg, toBytes
13+
getArg, toBytes, fromBytes
1414
} from './utils';
1515

1616

@@ -22,6 +22,20 @@ function getContext(canvas: HTMLCanvasElement) {
2222
return context;
2323
}
2424

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+
2539

2640
export
2741
class CanvasModel extends DOMWidgetModel {
@@ -41,9 +55,10 @@ class CanvasModel extends DOMWidgetModel {
4155

4256
static serializers: ISerializers = {
4357
...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+
}
4762
}
4863

4964
initialize(attributes: any, options: any) {
@@ -53,13 +68,24 @@ class CanvasModel extends DOMWidgetModel {
5368
this.ctx = getContext(this.canvas);
5469

5570
this.resizeCanvas();
71+
this.drawImageData();
5672

5773
this.on('change:size', this.resizeCanvas.bind(this));
5874
this.on('msg:custom', this.onCommand.bind(this));
5975

6076
this.send({ event: 'client_ready' }, {});
6177
}
6278

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+
6389
private async onCommand(command: any, buffers: any) {
6490
await this.processCommand(command, buffers);
6591

@@ -328,7 +354,7 @@ class CanvasView extends DOMWidgetView {
328354
}
329355

330356
updateCanvas() {
331-
this.clear();
357+
this.clear();
332358
this.ctx.drawImage(this.model.canvas, 0, 0);
333359
}
334360

0 commit comments

Comments
 (0)