@nativescript-community/ui-htmlcanvasapi
Version:
An HTML Canvas API implementation on top of android and iOS native APIs
96 lines • 3.57 kB
JavaScript
var HTMLCanvasView_1;
import { Canvas, CanvasView, createRectF, Paint } from '@nativescript-community/ui-canvas';
import { booleanConverter, CSSType, Property } from '@nativescript/core';
import { HTMLCanvasElement } from '../elements/HTMLCanvasElement';
import { SCREEN_SCALE } from '../helpers';
function onLayoutChange(args) {
const view = args.object;
// During view layout, offscreen buffer is also resized
if (view.isOffscreenBufferEnabled) {
const measuredWidth = view.getMeasuredWidth();
const measuredHeight = view.getMeasuredHeight();
if (measuredWidth != 0 && measuredHeight != 0) {
view._resizeOffscreenBuffer(measuredWidth, measuredHeight);
}
else {
view._disposeOffscreenBuffer();
}
}
}
const isOffscreenBufferEnabledProperty = new Property({
name: 'isOffscreenBufferEnabled',
defaultValue: false,
valueConverter: booleanConverter,
});
let HTMLCanvasView = HTMLCanvasView_1 = class HTMLCanvasView extends CanvasView {
constructor() {
super();
this._canvasElement = new HTMLCanvasElement(this);
}
get canvasElement() {
return this._canvasElement;
}
get nativeContext() {
return this._offscreenContext != null ? this._offscreenContext : this._currentCanvas;
}
disposeNativeView() {
super.disposeNativeView();
this._disposeOffscreenBuffer();
}
getContext(contextId, contextAttributes) {
return this._canvasElement.getContext(contextId, contextAttributes);
}
transferControlToOffscreen() {
return this._canvasElement.transferControlToOffscreen();
}
toDataURL(type, encoderOptions) {
return this._canvasElement.toDataURL(type, encoderOptions);
}
isDrawing() {
return this._isDrawing;
}
onDraw(canvas) {
this._isDrawing = true;
this._currentCanvas = canvas;
super.onDraw(canvas);
if (this._offscreenContext != null) {
// Canvas instance might be view canvas or data export canvas
const rect = createRectF(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawBitmap(this._offscreenContext.getImage(), null, rect, this._offscreenPaint);
}
this._currentCanvas = null;
this._isDrawing = false;
}
_resizeOffscreenBuffer(width, height) {
if (this._offscreenContext != null) {
this._offscreenContext.release();
}
this._offscreenContext = new Canvas(width, height);
this._offscreenContext.scale(SCREEN_SCALE, SCREEN_SCALE);
}
_disposeOffscreenBuffer() {
if (this._offscreenContext != null) {
this._offscreenContext.release();
this._offscreenContext = null;
}
this._offscreenPaint = null;
}
[isOffscreenBufferEnabledProperty.setNative](value) {
if (value) {
this._offscreenPaint = new Paint();
this._offscreenPaint.setAntiAlias(true);
this.on(HTMLCanvasView_1.layoutChangedEvent, onLayoutChange);
}
else {
this.off(HTMLCanvasView_1.layoutChangedEvent, onLayoutChange);
this._disposeOffscreenBuffer();
}
}
};
HTMLCanvasView = HTMLCanvasView_1 = __decorate([
CSSType('HTMLCanvasView'),
__metadata("design:paramtypes", [])
], HTMLCanvasView);
isOffscreenBufferEnabledProperty.register(HTMLCanvasView);
export { HTMLCanvasView, isOffscreenBufferEnabledProperty, SCREEN_SCALE };
//# sourceMappingURL=index.js.map