Skip to content

Commit c7d2b09

Browse files
committed
Add transferToImageBitmap support to make Canvas API work as an OffscreenCanvas
1 parent a6bf521 commit c7d2b09

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

lib/classes/canvas.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"use strict"
66

77
const {RustClass, core, inspect, REPR} = require('./neon'),
8-
{Image, ImageData, pixelSize} = require('./imagery'),
8+
{Image, ImageBitmap, ImageData, pixelSize} = require('./imagery'),
99
{toSkMatrix} = require('./geometry')
1010

1111
class Canvas extends RustClass{
@@ -98,6 +98,10 @@ class Canvas extends RustClass{
9898
return `data:${mime};base64,${buffer.toString('base64')}`
9999
}
100100

101+
transferToImageBitmap(){
102+
const buffer = this.toBufferSync("png");
103+
return new ImageBitmap(buffer);
104+
}
101105

102106
[REPR](depth, options) {
103107
let {width, height, gpu, engine, pages} = this

lib/classes/imagery.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ class ImageData{
164164
}
165165
}
166166

167+
class ImageBitmap extends Image {
168+
constructor(buffer) {
169+
super()
170+
171+
if (Buffer.isBuffer(buffer)) {
172+
this.prop('data', buffer)
173+
}
174+
}
175+
176+
close() {
177+
// no-op
178+
}
179+
180+
[REPR](depth, options) {
181+
let {width, height} = this
182+
return `ImageBitmap ${inspect({width, height}, options)}`
183+
}
184+
}
185+
167186
function pixelSize(colorType){
168187
const bpp = ["Alpha8", "Gray8", "R8UNorm"].includes(colorType) ? 1
169188
: ["A16Float", "A16UNorm", "ARGB4444", "R8G8UNorm", "RGB565"].includes(colorType) ? 2
@@ -177,4 +196,4 @@ function pixelSize(colorType){
177196
return bpp
178197
}
179198

180-
module.exports = {Image, ImageData, loadImage, loadImageData, pixelSize}
199+
module.exports = {Image, ImageData, ImageBitmap, loadImage, loadImageData, pixelSize}

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"use strict"
66

77
const {Canvas, CanvasGradient, CanvasPattern, CanvasTexture} = require('./classes/canvas'),
8-
{Image, ImageData, loadImage, loadImageData} = require('./classes/imagery'),
8+
{Image, ImageBitmap, ImageData, loadImage, loadImageData} = require('./classes/imagery'),
99
{DOMPoint, DOMMatrix, DOMRect} = require('./classes/geometry'),
1010
{TextMetrics, FontLibrary} = require('./classes/typography'),
1111
{CanvasRenderingContext2D} = require('./classes/context'),
@@ -14,7 +14,7 @@ const {Canvas, CanvasGradient, CanvasPattern, CanvasTexture} = require('./classe
1414

1515
module.exports = {
1616
Canvas, CanvasGradient, CanvasPattern, CanvasTexture,
17-
Image, ImageData, loadImage, loadImageData,
17+
Image, ImageBitmap, ImageData, loadImage, loadImageData,
1818
Path2D, DOMPoint, DOMMatrix, DOMRect,
1919
FontLibrary, TextMetrics,
2020
CanvasRenderingContext2D,

0 commit comments

Comments
 (0)