Skip to content

Commit e6f80a2

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

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-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: 29 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,18 @@ function getContext(canvas: HTMLCanvasElement) {
2222
return context;
2323
}
2424

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

2638
export
2739
class CanvasModel extends DOMWidgetModel {
@@ -41,9 +53,10 @@ class CanvasModel extends DOMWidgetModel {
4153

4254
static serializers: ISerializers = {
4355
...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+
}
4760
}
4861

4962
initialize(attributes: any, options: any) {
@@ -53,13 +66,24 @@ class CanvasModel extends DOMWidgetModel {
5366
this.ctx = getContext(this.canvas);
5467

5568
this.resizeCanvas();
69+
this.drawImageData();
5670

5771
this.on('change:size', this.resizeCanvas.bind(this));
5872
this.on('msg:custom', this.onCommand.bind(this));
5973

6074
this.send({ event: 'client_ready' }, {});
6175
}
6276

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

@@ -328,7 +352,7 @@ class CanvasView extends DOMWidgetView {
328352
}
329353

330354
updateCanvas() {
331-
this.clear();
355+
this.clear();
332356
this.ctx.drawImage(this.model.canvas, 0, 0);
333357
}
334358

0 commit comments

Comments
 (0)