1.2.0 • Published 10 months ago

@nativescript-community/ui-htmlcanvasapi v1.2.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
10 months ago

@nativescript-community/ui-htmlcanvasapi

An HTML Canvas implementation on top of @nativescript-community/ui-canvas.

Supported classes:

  • OffscreenCanvas
  • CanvasRenderingContext2D
  • ImageBitmapRenderingContext
  • OffscreenCanvasRenderingContext2D
  • CanvasGradient
  • CanvasPattern
  • DOMMatrix
  • DOMPoint (not used yet)
  • Path2D
  • TextMetrics

Installation

npm install @nativescript-community/ui-htmlcanvasapi

Usage

Install polyfills (optional)

import { Application } from '@nativescript/core';
import { installPolyfills } from '@nativescript-community/ui-htmlcanvasapi';

installPolyfills();

Application.run({ moduleName: 'app-root' });

Then, set up your view

<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:canvas="@nativescript-community/ui-htmlcanvasapi">
  <Page.actionBar>
    <ActionBar title="ui-htmlcanvasapi" icon="" class="action-bar">
    </ActionBar>
  </Page.actionBar>
  <StackLayout class="p-20">
    <canvas:HTMLCanvasView width="400" height="400" hardwareAccelerated="true" draw="onDraw"/>
  </StackLayout>
</Page>
import { Canvas } from '@nativescript-community/ui-canvas';
import { HTMLCanvasView } from '@nativescript-community/ui-htmlcanvasapi';

export function onDraw(args: { object: HTMLCanvasView; canvas: Canvas }) {
	const ctx = args.object.getContext('2d');

	ctx.save();

	ctx.fillStyle = 'yellow';
	ctx.fillRect(10, 10, 200, 100);

	ctx.transform(1, 0.5, -0.5, 1, 30, 10);

	ctx.fillStyle = 'red';
	ctx.fillRect(10, 10, 200, 100);

	ctx.transform(1, 0.5, -0.5, 1, 30, 10);

	ctx.fillStyle = 'blue';
	ctx.fillRect(10, 10, 200, 100);

	ctx.restore();
}

Note: If you wish to draw outside of draw listener context, you should call HTMLCanvasView invalidate() method in order to request view to redraw itself.

import { HTMLCanvasView } from '@nativescript-community/ui-htmlcanvasapi';

function updateGraph(canvasView: HTMLCanvasView) {
	const ctx = canvasView.getContext('2d');

	ctx.save();
	ctx.fillStyle = 'yellow';
	ctx.fillRect(10, 10, 200, 100);
	ctx.restore();

	canvasView.invalidate();
}

Sometimes, there might be a need to draw things offscreen but keep reference to the view and eventually want to draw everything there.
HTMLCanvasView includes these custom methods:

  • enableOffscreenBuffer
  • disableOffscreenBuffer
  • isOffscreenBufferEnabled
  • drawOffscreenBuffer
const view = args.object;
const ctx = view.getContext('2d');

view.enableOffscreenBuffer();

ctx.save();
ctx.fillStyle = 'yellow';
ctx.fillRect(10, 10, 200, 100);
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 200, 100);
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = 'blue';
ctx.fillRect(10, 10, 200, 100);
ctx.restore();

view.drawOffscreenBuffer();
// If buffer is no longer needed, just discard it
view.disableOffscreenBuffer();

Note: This state is required to perform actions like toDataURL.

License

Apache License Version 2.0

1.2.0

10 months ago

1.1.0

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

1.0.0-alpha.25

10 months ago

1.0.0-alpha.24

10 months ago

1.0.0-alpha.23

10 months ago

1.0.0-alpha.22

10 months ago

1.0.0-alpha.21

11 months ago

1.0.0-alpha.20

11 months ago

1.0.0-alpha.19

11 months ago

1.0.0-alpha.18

11 months ago

1.0.0-alpha.17

11 months ago

1.0.0-alpha.16

11 months ago

1.0.0-alpha.15

11 months ago

1.0.0-alpha.14

11 months ago

1.0.0-alpha.13

11 months ago

1.0.0-alpha.12

11 months ago

1.0.0-alpha.11

11 months ago

1.0.0-alpha.10

11 months ago

1.0.0-alpha.9

11 months ago

1.0.0-alpha.8

11 months ago

1.0.0-alpha.7

11 months ago

1.0.0-alpha.6

11 months ago

1.0.0-alpha.5

11 months ago

1.0.0-alpha.4

11 months ago

1.0.0-alpha.3

11 months ago

1.0.0-alpha.2

11 months ago

1.0.0-alpha.1

11 months ago

1.0.0-alpha.0

11 months ago