Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Oct 9, 2023
1 parent d941639 commit ac4a1c4
Show file tree
Hide file tree
Showing 42 changed files with 6,338 additions and 6,899 deletions.
4 changes: 2 additions & 2 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"zen-3d": "file:../../node_modules/zen-3d"
},
"devDependencies": {
"@nativescript/android": "^8.5.0-alpha.1",
"@nativescript/ios": "8.5.1-dev.0"
"@nativescript/android": "8.5.4",
"@nativescript/ios": "8.5.2"
}
}
5 changes: 3 additions & 2 deletions apps/demo/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"production": true,
"uglify": true,
"release": true,
"forDevice": true
"forDevice": true,
},
"dependsOn": [
{
Expand All @@ -38,7 +38,8 @@
"executor": "@nativescript/nx:build",
"options": {
"platform": "android",
"noHmr": true
"noHmr": true,
"flags": "--env.watchNodeModules"
},
"dependsOn": [
{
Expand Down
77 changes: 0 additions & 77 deletions apps/demo/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,83 +47,6 @@ Application.on('launch', (args) => {
monitor.appVersionHidden = true;
monitor.deviceVersionHidden = true;
}

const paths = new Array(10);
const length = paths.length;
console.time('data');
for (let i = 0; i < length; i++) {
const id = new ImageData(1000, 1000);
const size = id.width + id.height;
const length = id.data.length;
}
console.timeEnd('data');
});

Application.android.on('activityCreated', (args) => {
const canvas = Canvas.createCustomView();
const context = canvas.getContext('2d');
console.log(canvas.width, canvas.height);

const ctx = canvas.getContext('2d') as never as CanvasRenderingContext2D;

console.time('fill');
const fill = ctx.fillStyle;
console.timeEnd('fill');

//console.log(context.measureText('Osei Fortune'));
//console.log(context.createImageData(100,100));

// Create path
let region = new Path2D() as any;
region.moveTo(30, 90);
region.lineTo(110, 20);
region.lineTo(240, 130);
region.lineTo(60, 130);
region.lineTo(190, 20);
region.lineTo(270, 90);
region.closePath();

let path = new Path2D() as any;
path.addPath(region);

console.log('new', path.__toSVG());

/*
JS: CONSOLE LOG: M30 90L110 20L240 130L60 130L190 20L270 90L30 90Z
JS: CONSOLE TIME: svg: 1.376ms
*/

/*
JS: CONSOLE LOG: 300 150
JS: CONSOLE LOG: M30 90L110 20L240 130L60 130L190 20L270 90L30 90Z
JS: CONSOLE TIME: svg: 0.819ms
JS: CONSOLE LOG: M30 90L110 20L240 130L60 130L190 20L270 90L30 90Z
JS: CONSOLE TIME: svg: 0.424ms
*/

console.time('svg');
console.log(region.__toSVG());
console.timeEnd('svg');

setTimeout(() => {
const r = new Path2D(region) as any;
console.time('svg1');
console.log(r.__toSVG());
console.timeEnd('svg1');
}, 1000);

setTimeout(() => {
const r = new Path2D(region) as any;
console.time('svg2');
console.log(r.__toSVG());
console.timeEnd('svg2');
}, 1500);

// Fill path
ctx.fillStyle = 'green';
ctx.fill(region, 'evenodd');
//ctx.fillRect(0, 0, 100, 100);
android.util.Log.d('JS', canvas.toDataURL());
});

Application.run({ moduleName: 'app-root' });
7 changes: 4 additions & 3 deletions packages/canvas-polyfill/async/file/file.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ export class FileManager {
//const opts = new com.github.triniwiz.async.Async2.FileManager.Options();
//opts.asStream = options.asStream;
if (this._readFile === undefined) {
this._readFile = global?.CanvasJSIModule?.readFile;
//this._readFile = global?.CanvasModule?.readFile;
}
if (this._readFile) {
this._readFile(path, (error, buffer) => {
if (global?.CanvasModule?.readFile) {
global?.CanvasModule?.readFile(path, (error, buffer) => {
console.log(buffer);
if (error) {
callback(new Error(error), null);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-polyfill/async/file/file.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class FileManager {

public static readFile(path: string, options: Options = { asStream: false }, callback: (...args) => void) {
if (this._readFile === undefined) {
this._readFile = global?.CanvasJSIModule?.readFile;
this._readFile = global?.CanvasModule?.readFile;
}

if (this._readFile) {
Expand Down
1 change: 1 addition & 0 deletions packages/canvas-polyfill/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Document } from './DOM/Document';
import './window';
import './resize';
import './process';
import './localStorage';
import { TextDecoder, TextEncoder, ImageBitmap } from '@nativescript/canvas';
import { URL } from './URL';
(global as any).document = (global as any).window.document = (global as any).document || new Document();
Expand Down
159 changes: 159 additions & 0 deletions packages/canvas-polyfill/localStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { File, knownFolders } from '@nativescript/core';

let file: File;
let localStorageTimeout = null;

const internalSaveData = function (storage: Storage) {
try {
file.writeText(JSON.stringify(storage._localStorageData));
} catch (err) {
// This should never happen on normal data, but if they tried to put non JS stuff it won't serialize
console.log('localStorage: unable to write storage, error: ', err);
}
};

const saveData = function (storage: Storage) {
if (localStorageTimeout !== null) {
clearTimeout(localStorageTimeout);
}
localStorageTimeout = setTimeout(() => {
internalSaveData(storage);
}, 250);
};

class Storage {
_localStorageData = {};
getItem(name) {
if (this._localStorageData.hasOwnProperty(name)) {
return this._localStorageData[name];
}
return null;
}
key(id) {
const keys = Object.keys(this._localStorageData);
if (id >= keys.length) {
return null;
}
return keys[id];
}

setItemObject(name, value) {
this._localStorageData[name] = value;
saveData(this);
}
// Revamp this to be "String" only
// https://github.com/NathanaelA/nativescript-localstorage/issues/17
setItem(name, value) {
if (value == null) {
if (value === null) {
this._localStorageData[name] = 'null';
} else {
this._localStorageData[name] = 'undefined';
}
} else {
this._localStorageData[name] = value.toString();
}
saveData(this);
}
removeItem(name) {
if (this._localStorageData[name]) {
delete this._localStorageData[name];
saveData(this);
}
}

clear() {
this._localStorageData = {};
saveData(this);
}

get length() {
return Object.keys(this._localStorageData).length;
}
}

class SessionStorage {
private _storage = new Map();
getItem(name) {
return this._storage.get(name) ?? null;
}
key(id) {
const keys = Array.from(this._storage.keys());
if (id >= keys.length) {
return null;
}
return keys[id];
}

setItemObject(name, value) {
this._storage.set(name, value);
}

setItem(name, value) {
if (value == null) {
if (value === null) {
this._storage.set(name, 'null');
} else {
this._storage.set(name, 'undefined');
}
} else {
this._storage.set(name, value.toString());
}
}

removeItem(name) {
this._storage.delete(name);
}
clear() {
this._storage.clear();
}

get length() {
return this._storage.size;
}
}

if (!global.Storage) {
(<any>global).Storage = Storage;
}

if (!global.localStorage || (<any>module).hot) {
const path = knownFolders.documents().path + '/localStorage.db';
file = File.fromPath(path);
localStorageTimeout = null;

const loadData = function (storage: Storage) {
if (!File.exists(path)) {
return;
}

let data;
if (file.size === 0) {
return;
}
try {
let textData = file.readTextSync();
data = JSON.parse(textData);
storage._localStorageData = data;
} catch (err) {
console.log('localStorage: error reading storage, Error: ', err);
}
};

const storage = new Storage();
loadData(storage);
global.localStorage = storage;
}

if (!global.sessionStorage) {
global.sessionStorage = new SessionStorage();
}

export default global.localStorage;

if ((<any>module).hot) {
(<any>module).hot.accept();
(<any>module).hot.dispose(() => {
global.localStorage = undefined;
});
}
23 changes: 0 additions & 23 deletions packages/canvas/Canvas/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,6 @@ export class Canvas extends CanvasBase {
private _contextType = ContextType.None;
private _is2D = false;

_methodCache = new Map();

_getMethod(name: string) {
if (this.__native__context === undefined) {
return undefined;
}
const cached = this._methodCache.get(name);
if (cached === undefined) {
const ret = this.__native__context[name];
this._methodCache.set(name, ret);
return ret;
}

return cached;
}

constructor() {
super();
const activity = Application.android.foregroundActivity || Application.android.startActivity;
Expand Down Expand Up @@ -222,12 +206,6 @@ export class Canvas extends CanvasBase {
}

toDataURL(type = 'image/png', encoderOptions = 0.92) {
//const toDataURL = this._getMethod('__toDataURL');

// if (toDataURL === undefined) {
// return 'data:,';
// }

return this.native.__toDataURL(type, encoderOptions);
}

Expand Down Expand Up @@ -323,7 +301,6 @@ export class Canvas extends CanvasBase {
const opts = Object.assign({ version: 'v1' }, Object.assign(defaultOpts, this._handleContextOptions(type, options)));

this._canvas.initContext(type, opts.alpha, opts.antialias, opts.depth, opts.failIfMajorPerformanceCaveat, opts.powerPreference, opts.premultipliedAlpha, opts.preserveDrawingBuffer, opts.stencil, opts.desynchronized, opts.xrCompatible);

this._webglContext = new (WebGLRenderingContext as any)(this._canvas, opts);
(this._webglContext as any)._canvas = this;
this._webglContext._type = 'webgl';
Expand Down
Loading

0 comments on commit ac4a1c4

Please sign in to comment.