Skip to content

Commit a2e6bd8

Browse files
committed
fix(android): polyfill early usage
closes : #21
1 parent 2589600 commit a2e6bd8

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

packages/canvas-polyfill/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
require('globals');
2-
import {TNSXMLHttpRequest, FileReader, Blob} from './async/async';
3-
import {Element} from './DOM/Element';
4-
import {Document} from './DOM/Document';
2+
3+
if (global.android && !(global as any).__canvasLoaded) {
4+
try {
5+
// load canvas lib if polyfill is called before
6+
java.lang.System.loadLibrary('canvasnative');
7+
(global as any).__canvasLoaded = true;
8+
} catch (e) {}
9+
}
10+
import { TNSXMLHttpRequest, FileReader, Blob } from './async/async';
11+
import { Element } from './DOM/Element';
12+
import { Document } from './DOM/Document';
513
import './window';
614
import './resize';
715
import './process';
8-
import {TextDecoder, TextEncoder} from '@nativescript/canvas';
9-
import {URL} from './URL';
16+
import { TextDecoder, TextEncoder } from '@nativescript/canvas';
17+
import { URL } from './URL';
1018
(global as any).document = (global as any).window.document = (global as any).document || new Document();
1119
Object.defineProperty(global, 'Element', {
1220
value: Element,
@@ -46,7 +54,6 @@ if (!((global as any).TextEncoder instanceof TextEncoder)) {
4654
});
4755
}
4856

49-
5057
if (!((global as any).URL instanceof URL)) {
5158
Object.defineProperty(global, 'URL', {
5259
value: URL,

packages/canvas/Canvas/index.android.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class Canvas extends CanvasBase {
3333
const activity =
3434
Application.android.foregroundActivity || Application.android.startActivity;
3535
this._canvas = new com.github.triniwiz.canvas.TNSCanvas(activity, useCpu);
36+
(global as any).__canvasLoaded = true;
3637
}
3738

3839
// @ts-ignore

packages/canvas/vue/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/canvas/vue/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
install(Vue) {
3+
Vue.registerElement('Canvas', () => require('../').Canvas);
4+
}
5+
}

0 commit comments

Comments
 (0)