Skip to content

Commit cfcf259

Browse files
authored
Merge pull request #17 from NativeScript/v0.9.7
feat(): remote image loading with ImageAsset an HTMLImageElement
2 parents 4b4d967 + 1f85974 commit cfcf259

File tree

73 files changed

+1896
-1184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1896
-1184
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ Thumbs.db
4242

4343
*.tgz
4444
packages/**/angular/dist
45+
apps/demo/times.html

CanvasNative.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Pod::Spec.new do |s|
22

33
s.name = "CanvasNative"
44

5-
s.version = "0.9.4"
5+
s.version = "0.9.7"
66

77
s.summary = "A Canvas library"
88

packages/canvas-polyfill/DOM/Document.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export class Document extends Element {
6363

6464
getElementById(id) {
6565
const topmost = Frame.topmost();
66+
if (id instanceof Canvas) {
67+
const canvas = new HTMLCanvasElement();
68+
canvas._canvas = id;
69+
return canvas;
70+
}
6671
if (topmost) {
6772
const nativeElement = topmost.getViewById(id);
6873
if (nativeElement) {
@@ -73,4 +78,8 @@ export class Document extends Element {
7378
}
7479
return new Element("div");
7580
}
76-
}
81+
82+
querySelector(selector){
83+
return new Element(selector);
84+
}
85+
}

packages/canvas-polyfill/DOM/Element.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {Node} from "./Node";
2-
import {Canvas} from '@nativescript/canvas';
1+
import { Node } from "./Node";
2+
import { Canvas } from '@nativescript/canvas';
33

44
export class Element extends Node {
55
private doc: any;
@@ -9,7 +9,7 @@ export class Element extends Node {
99
namespaceURI: any;
1010
nativeElement: any;
1111

12-
constructor(tagName) {
12+
constructor(tagName, canvas = undefined) {
1313
super(tagName.toUpperCase());
1414

1515
this.doc = {
@@ -19,7 +19,11 @@ export class Element extends Node {
1919
};
2020
this._classList = new Set();
2121
if (tagName.toLowerCase() === 'canvas') {
22-
this._canvas = Canvas.createCustomView();
22+
if (canvas instanceof Canvas) {
23+
this._canvas = canvas;
24+
} else {
25+
this._canvas = Canvas.createCustomView();
26+
}
2327
}
2428
}
2529

@@ -111,5 +115,4 @@ export class Element extends Node {
111115
get ontouchstart() {
112116
return {};
113117
}
114-
}
115-
118+
}

packages/canvas-polyfill/DOM/HTMLImageElement.ts

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Element } from './Element';
2-
import { knownFolders, path, File, ImageSource } from '@nativescript/core';
2+
import { knownFolders, path, File } from '@nativescript/core';
33
import { ImageAsset } from '@nativescript/canvas';
4-
54
const background_queue = global.isIOS ? dispatch_get_global_queue(qos_class_t.QOS_CLASS_DEFAULT, 0) : undefined;
65
const main_queue = global.isIOS ? dispatch_get_current_queue() : undefined;
76
declare var NSUUID, java, NSData, android;
@@ -45,6 +44,8 @@ export class HTMLImageElement extends Element {
4544
_imageSource: any;
4645
__id: any;
4746

47+
decoding = 'auto';
48+
4849
get src() {
4950
return this.localUri;
5051
}
@@ -78,8 +79,7 @@ export class HTMLImageElement extends Element {
7879
super('image');
7980
this._asset = new ImageAsset();
8081
this.__id = getUUID();
81-
// this._load = this._load.bind(this);
82-
this._onload = () => {};
82+
this._onload = () => { };
8383
if (props !== null && typeof props === 'object') {
8484
this.src = props.localUri;
8585
this.width = props.width;
@@ -88,6 +88,7 @@ export class HTMLImageElement extends Element {
8888
}
8989
}
9090

91+
9192
_load() {
9293
if (this.src) {
9394
if (typeof this.src === 'string' && this.src.startsWith && this.src.startsWith('data:')) {
@@ -155,18 +156,54 @@ export class HTMLImageElement extends Element {
155156
})();
156157
return;
157158
}
158-
if (!this.width || !this.height) {
159-
this.complete = false;
160-
this._asset
161-
.loadFileAsync(this.src)
162-
.then(() => {
163-
this.width = this._asset.width;
164-
this.height = this._asset.height;
165-
this.complete = true;
166-
})
167-
.catch((e) => {
168-
this.emitter.emit('error', { target: this });
169-
});
159+
160+
if (typeof this.src === 'string') {
161+
let async = this.decoding !== 'sync';
162+
if (this.src.startsWith('http')) {
163+
if (!async) {
164+
const loaded = this._asset.loadFromUrl(this.src);
165+
if (loaded) {
166+
this.width = this._asset.width;
167+
this.height = this._asset.height;
168+
this.complete = true;
169+
} else {
170+
this.emitter.emit('error', { target: this });
171+
}
172+
} else {
173+
this._asset.loadFromUrlAsync(this.src).then(() => {
174+
this.width = this._asset.width;
175+
this.height = this._asset.height;
176+
this.complete = true;
177+
}).catch(e => {
178+
this.emitter.emit('error', { target: this });
179+
})
180+
}
181+
} else {
182+
if (!this.width || !this.height) {
183+
this.complete = false;
184+
if (!async) {
185+
const loaded = this._asset.loadFile(this.src);
186+
if (loaded) {
187+
this.width = this._asset.width;
188+
this.height = this._asset.height;
189+
this.complete = true;
190+
} else {
191+
this.emitter.emit('error', { target: this });
192+
}
193+
} else {
194+
this._asset
195+
.loadFileAsync(this.src)
196+
.then(() => {
197+
this.width = this._asset.width;
198+
this.height = this._asset.height;
199+
this.complete = true;
200+
})
201+
.catch((e) => {
202+
this.emitter.emit('error', { target: this });
203+
});
204+
}
205+
}
206+
}
170207
}
171208
}
172209
}

packages/canvas-polyfill/async/xhr/TNSXMLHttpRequest.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {CancellablePromise, Http} from '../http/http';
2-
import {HttpError, HttpRequestOptions, ProgressEvent} from '../http/http-request-common';
3-
import {FileManager} from '../file/file';
4-
import {isNullOrUndefined, isObject, isFunction} from '@nativescript/core/utils/types';
5-
import {knownFolders, path as filePath, File as fsFile} from '@nativescript/core';
1+
import { CancellablePromise, Http } from '../http/http';
2+
import { HttpError, HttpRequestOptions, ProgressEvent } from '../http/http-request-common';
3+
import { FileManager } from '../file/file';
4+
import { isNullOrUndefined, isObject, isFunction } from '@nativescript/core/utils/types';
5+
import { knownFolders, path as filePath, File as fsFile } from '@nativescript/core';
66

77
enum XMLHttpRequestResponseType {
88
empty = '',
@@ -126,7 +126,7 @@ export class TNSXMLHttpRequest {
126126
loaded: number;
127127
total: number;
128128
target: any;
129-
} = {lengthComputable: false, loaded: 0, total: 0, target: this};
129+
} = { lengthComputable: false, loaded: 0, total: 0, target: this };
130130

131131
private _headers: any;
132132
private _responseURL: string = '';
@@ -150,6 +150,8 @@ export class TNSXMLHttpRequest {
150150
return this._response;
151151
}
152152

153+
private _didUserSetResponseType = false;
154+
153155
get responseType(): any {
154156
return this._responseType;
155157
}
@@ -286,6 +288,7 @@ export class TNSXMLHttpRequest {
286288
}
287289

288290
set responseType(value: any) {
291+
this._didUserSetResponseType = true;
289292
if (
290293
value === XMLHttpRequestResponseType.empty ||
291294
value in XMLHttpRequestResponseType
@@ -421,7 +424,7 @@ export class TNSXMLHttpRequest {
421424
this.emitEvent('loadstart', startEvent);
422425

423426
this._updateReadyStateChange(this.LOADING);
424-
427+
425428
FileManager.readFile(path, {}, (error, data) => {
426429
if (error) {
427430
const errorEvent = new ProgressEvent(
@@ -449,7 +452,7 @@ export class TNSXMLHttpRequest {
449452

450453
this._updateReadyStateChange(this.DONE);
451454
} else {
452-
if (!this.responseType) {
455+
if (!this._didUserSetResponseType) {
453456
this._setResponseType();
454457
}
455458
this._status = 200;
@@ -692,11 +695,12 @@ export class TNSXMLHttpRequest {
692695

693696
this._currentRequest
694697
.then((res) => {
695-
this._setResponseType();
698+
if (!this._didUserSetResponseType) {
699+
this._setResponseType();
700+
}
696701
this._status = res.statusCode;
697702
this._httpContent = res.content;
698703
this._responseURL = res.url;
699-
700704
if (this.responseType === XMLHttpRequestResponseType.json) {
701705
if (typeof res.content === 'string') {
702706
this._responseText = res.content;
@@ -1097,7 +1101,7 @@ export class Blob {
10971101
public slice(start?: number, end?: number, type?: string): Blob {
10981102
const slice = this._buffer.slice(start || 0, end || this._buffer.length);
10991103

1100-
return new Blob([slice], {type: type});
1104+
return new Blob([slice], { type: type });
11011105
}
11021106

11031107
public stream() {
Binary file not shown.

0 commit comments

Comments
 (0)