Skip to content

Commit 0bdd67c

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

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
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.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ export class Image extends EventEmitter {
153153
decode(): Promise<Image>
154154
}
155155

156+
export class ImageBitmap {
157+
constructor(buffer: Buffer)
158+
get width(): number
159+
get height(): number
160+
close(): void
161+
}
162+
156163
//
157164
// DOMMatrix
158165
//

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,

test/canvas.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const _ = require('lodash'),
44
fs = require('fs'),
55
tmp = require('tmp'),
66
glob = require('glob').sync,
7-
{Canvas, Image} = require('../lib');
7+
{Canvas, Image, ImageBitmap} = require('../lib');
88

99
const BLACK = [0,0,0,255],
1010
WHITE = [255,255,255,255],
@@ -542,4 +542,10 @@ describe("Canvas", ()=>{
542542
})
543543
})
544544

545+
test('transferToImageBitmap', () => {
546+
const imageBitmap = canvas.transferToImageBitmap()
547+
expect(imageBitmap).toBeInstanceOf(ImageBitmap)
548+
expect(imageBitmap.width).toBe(canvas.width)
549+
expect(imageBitmap.height).toBe(canvas.height)
550+
})
545551
})

0 commit comments

Comments
 (0)