Skip to content

Commit a6a2219

Browse files
authored
feat(Canvas): add support for displaying output in Jupyter Notebook (#60)
1 parent 57b8e31 commit a6a2219

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/canvas.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ export class Canvas {
209209
flush() {
210210
if (this[_gpu]) sk_canvas_flush(this[_ptr]);
211211
}
212+
213+
[Symbol.for("Jupyter.display")]() {
214+
return {
215+
"image/png": encodeBase64(this.encode("png")),
216+
};
217+
}
212218
}
213219

214220
/**

src/svgcanvas.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ export class SvgCanvas {
114114
);
115115
const text = new TextDecoder().decode(buffer);
116116
sk_data_free(skdata);
117+
Object.defineProperty(text, Symbol.for("Jupyter.display"), {
118+
value: function (this: string) {
119+
return {
120+
"image/svg+xml": this,
121+
};
122+
},
123+
});
117124
return text;
118125
}
119126

0 commit comments

Comments
 (0)