Skip to content

feat(): remote image loading with ImageAsset an HTMLImageElement #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ Thumbs.db

*.tgz
packages/**/angular/dist
apps/demo/times.html
2 changes: 1 addition & 1 deletion CanvasNative.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pod::Spec.new do |s|

s.name = "CanvasNative"

s.version = "0.9.4"
s.version = "0.9.7"

s.summary = "A Canvas library"

Expand Down
11 changes: 10 additions & 1 deletion packages/canvas-polyfill/DOM/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export class Document extends Element {

getElementById(id) {
const topmost = Frame.topmost();
if (id instanceof Canvas) {
const canvas = new HTMLCanvasElement();
canvas._canvas = id;
return canvas;
}
if (topmost) {
const nativeElement = topmost.getViewById(id);
if (nativeElement) {
Expand All @@ -73,4 +78,8 @@ export class Document extends Element {
}
return new Element("div");
}
}

querySelector(selector){
return new Element(selector);
}
}
15 changes: 9 additions & 6 deletions packages/canvas-polyfill/DOM/Element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Node} from "./Node";
import {Canvas} from '@nativescript/canvas';
import { Node } from "./Node";
import { Canvas } from '@nativescript/canvas';

export class Element extends Node {
private doc: any;
Expand All @@ -9,7 +9,7 @@ export class Element extends Node {
namespaceURI: any;
nativeElement: any;

constructor(tagName) {
constructor(tagName, canvas = undefined) {
super(tagName.toUpperCase());

this.doc = {
Expand All @@ -19,7 +19,11 @@ export class Element extends Node {
};
this._classList = new Set();
if (tagName.toLowerCase() === 'canvas') {
this._canvas = Canvas.createCustomView();
if (canvas instanceof Canvas) {
this._canvas = canvas;
} else {
this._canvas = Canvas.createCustomView();
}
}
}

Expand Down Expand Up @@ -111,5 +115,4 @@ export class Element extends Node {
get ontouchstart() {
return {};
}
}

}
69 changes: 53 additions & 16 deletions packages/canvas-polyfill/DOM/HTMLImageElement.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Element } from './Element';
import { knownFolders, path, File, ImageSource } from '@nativescript/core';
import { knownFolders, path, File } from '@nativescript/core';
import { ImageAsset } from '@nativescript/canvas';

const background_queue = global.isIOS ? dispatch_get_global_queue(qos_class_t.QOS_CLASS_DEFAULT, 0) : undefined;
const main_queue = global.isIOS ? dispatch_get_current_queue() : undefined;
declare var NSUUID, java, NSData, android;
Expand Down Expand Up @@ -45,6 +44,8 @@ export class HTMLImageElement extends Element {
_imageSource: any;
__id: any;

decoding = 'auto';

get src() {
return this.localUri;
}
Expand Down Expand Up @@ -78,8 +79,7 @@ export class HTMLImageElement extends Element {
super('image');
this._asset = new ImageAsset();
this.__id = getUUID();
// this._load = this._load.bind(this);
this._onload = () => {};
this._onload = () => { };
if (props !== null && typeof props === 'object') {
this.src = props.localUri;
this.width = props.width;
Expand All @@ -88,6 +88,7 @@ export class HTMLImageElement extends Element {
}
}


_load() {
if (this.src) {
if (typeof this.src === 'string' && this.src.startsWith && this.src.startsWith('data:')) {
Expand Down Expand Up @@ -155,18 +156,54 @@ export class HTMLImageElement extends Element {
})();
return;
}
if (!this.width || !this.height) {
this.complete = false;
this._asset
.loadFileAsync(this.src)
.then(() => {
this.width = this._asset.width;
this.height = this._asset.height;
this.complete = true;
})
.catch((e) => {
this.emitter.emit('error', { target: this });
});

if (typeof this.src === 'string') {
let async = this.decoding !== 'sync';
if (this.src.startsWith('http')) {
if (!async) {
const loaded = this._asset.loadFromUrl(this.src);
if (loaded) {
this.width = this._asset.width;
this.height = this._asset.height;
this.complete = true;
} else {
this.emitter.emit('error', { target: this });
}
} else {
this._asset.loadFromUrlAsync(this.src).then(() => {
this.width = this._asset.width;
this.height = this._asset.height;
this.complete = true;
}).catch(e => {
this.emitter.emit('error', { target: this });
})
}
} else {
if (!this.width || !this.height) {
this.complete = false;
if (!async) {
const loaded = this._asset.loadFile(this.src);
if (loaded) {
this.width = this._asset.width;
this.height = this._asset.height;
this.complete = true;
} else {
this.emitter.emit('error', { target: this });
}
} else {
this._asset
.loadFileAsync(this.src)
.then(() => {
this.width = this._asset.width;
this.height = this._asset.height;
this.complete = true;
})
.catch((e) => {
this.emitter.emit('error', { target: this });
});
}
}
}
}
}
}
Expand Down
26 changes: 15 additions & 11 deletions packages/canvas-polyfill/async/xhr/TNSXMLHttpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {CancellablePromise, Http} from '../http/http';
import {HttpError, HttpRequestOptions, ProgressEvent} from '../http/http-request-common';
import {FileManager} from '../file/file';
import {isNullOrUndefined, isObject, isFunction} from '@nativescript/core/utils/types';
import {knownFolders, path as filePath, File as fsFile} from '@nativescript/core';
import { CancellablePromise, Http } from '../http/http';
import { HttpError, HttpRequestOptions, ProgressEvent } from '../http/http-request-common';
import { FileManager } from '../file/file';
import { isNullOrUndefined, isObject, isFunction } from '@nativescript/core/utils/types';
import { knownFolders, path as filePath, File as fsFile } from '@nativescript/core';

enum XMLHttpRequestResponseType {
empty = '',
Expand Down Expand Up @@ -126,7 +126,7 @@ export class TNSXMLHttpRequest {
loaded: number;
total: number;
target: any;
} = {lengthComputable: false, loaded: 0, total: 0, target: this};
} = { lengthComputable: false, loaded: 0, total: 0, target: this };

private _headers: any;
private _responseURL: string = '';
Expand All @@ -150,6 +150,8 @@ export class TNSXMLHttpRequest {
return this._response;
}

private _didUserSetResponseType = false;

get responseType(): any {
return this._responseType;
}
Expand Down Expand Up @@ -286,6 +288,7 @@ export class TNSXMLHttpRequest {
}

set responseType(value: any) {
this._didUserSetResponseType = true;
if (
value === XMLHttpRequestResponseType.empty ||
value in XMLHttpRequestResponseType
Expand Down Expand Up @@ -421,7 +424,7 @@ export class TNSXMLHttpRequest {
this.emitEvent('loadstart', startEvent);

this._updateReadyStateChange(this.LOADING);

FileManager.readFile(path, {}, (error, data) => {
if (error) {
const errorEvent = new ProgressEvent(
Expand Down Expand Up @@ -449,7 +452,7 @@ export class TNSXMLHttpRequest {

this._updateReadyStateChange(this.DONE);
} else {
if (!this.responseType) {
if (!this._didUserSetResponseType) {
this._setResponseType();
}
this._status = 200;
Expand Down Expand Up @@ -692,11 +695,12 @@ export class TNSXMLHttpRequest {

this._currentRequest
.then((res) => {
this._setResponseType();
if (!this._didUserSetResponseType) {
this._setResponseType();
}
this._status = res.statusCode;
this._httpContent = res.content;
this._responseURL = res.url;

if (this.responseType === XMLHttpRequestResponseType.json) {
if (typeof res.content === 'string') {
this._responseText = res.content;
Expand Down Expand Up @@ -1097,7 +1101,7 @@ export class Blob {
public slice(start?: number, end?: number, type?: string): Blob {
const slice = this._buffer.slice(start || 0, end || this._buffer.length);

return new Blob([slice], {type: type});
return new Blob([slice], { type: type });
}

public stream() {
Expand Down
Binary file modified packages/canvas-polyfill/platforms/android/async-release.aar
Binary file not shown.
Loading