1import Page from './index'; 2let offscreenCanvas; 3 4export class OffscreenCanvas { 5 private _bridge: OffscreenCanvasBridge; 6 private _width: number; 7 private _height: number; 8 constructor(page: Page, width: number, height: number) { 9 offscreenCanvas = page.requireModule('system.offscreenCanvas'); 10 this._height = height; 11 this._width = width; 12 this._bridge = offscreenCanvas.create({ 13 width: this._width, 14 height: this._height 15 }); 16 } 17 public getContext(contextId: '2d', options?: CanvasRenderingContext2DSettings): OffscreenCanvasRenderingContext2D { 18 return this._bridge.getContext('2d'); 19 } 20 21 public transferToImageBitmap(): ImageBitmap { 22 return this._bridge.transferToImageBitmap(); 23 } 24 25 public toDataURL(type?: string, quality?: number): string { 26 return this._bridge.toDataURL(type, quality); 27 } 28 29 public get width() { 30 return this._width; 31 } 32 33 public set width(width) { 34 this._width = width; 35 } 36 37 public get height() { 38 return this._height; 39 } 40 41 public set height(height) { 42 this._height = height; 43 } 44} 45 46export interface OffscreenCanvasRenderingContext2D { 47} 48 49export interface OffscreenCanvasBridge { 50 getContext: (contextId: '2d', options?: CanvasRenderingContext2DSettings)=>OffscreenCanvasRenderingContext2D; 51 transferToImageBitmap: ()=>ImageBitmap; 52 toDataURL:(type?: string, quality?: number)=>string; 53} 54 55export interface CanvasRenderingContext2DSettings { 56 alpha?: boolean; 57 desynchronized?: boolean; 58} 59 60export class ImageBitmap { 61 readonly height: number; 62 readonly width: number; 63 private _bridgeId: number; 64 constructor(bridgeId: number) { 65 this._bridgeId = bridgeId; 66 } 67 68 public get bridgeId() { 69 return this._bridgeId; 70 } 71 72 public set bridgeId(bridgeId) { 73 this._bridgeId = bridgeId; 74 } 75} 76