UNPKG

@nativescript-community/ui-htmlcanvasapi

Version:

An HTML Canvas API implementation on top of android and iOS native APIs

82 lines 2.56 kB
import { Canvas } from '@nativescript-community/ui-canvas'; import { ImageSource, Observable } from '@nativescript/core'; import { ImageBitmapRenderingContext } from '../contexts/ImageBitmapRenderingContext'; import { OffscreenCanvasRenderingContext2D } from '../contexts/OffscreenCanvasRenderingContext2D'; import { SCREEN_SCALE } from '../helpers'; class NSOffscreenCanvas extends Observable { constructor(width, height) { super(); this._width = width; this._height = height; this._updateNativeContext(); } _updateNativeContext() { if (this._nativeContext != null) { this._nativeContext.release(); } this._nativeContext = new Canvas(this._width * SCREEN_SCALE, this._height * SCREEN_SCALE); this._nativeContext.scale(SCREEN_SCALE, SCREEN_SCALE); } _isPixelScaleNeeded() { return true; } getContext(contextId, contextAttributes) { const self = this; if (!this._contexts) { this._contexts = new Map(); } if (this._contexts.has(contextId)) { return this._contexts.get(contextId); } let context; switch (contextId) { case '2d': context = new OffscreenCanvasRenderingContext2D(); break; case 'bitmaprenderer': context = new ImageBitmapRenderingContext(); break; default: return null; } Object.defineProperty(context, 'canvas', { get() { return self; }, }); this._contexts.set(contextId, context); return context; } transferToImageBitmap() { return new ImageSource(this._nativeContext.getImage()); } convertToBlob(options) { console.warn('Method convertToBlob is not implemented'); return null; } get nativeContext() { return this._nativeContext; } get width() { return this._width; } set width(val) { const oldVal = this._width; this._width = val; if (this._width !== oldVal) { this._updateNativeContext(); } } get height() { return this._height; } set height(val) { const oldVal = this._height; this._height = val; if (this._height !== oldVal) { this._updateNativeContext(); } } } export { NSOffscreenCanvas as OffscreenCanvas }; //# sourceMappingURL=OffscreenCanvas.js.map