-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets-functions.ts
69 lines (63 loc) · 1.47 KB
/
assets-functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { vec3, vec2 } from 'gl-matrix';
import { Camera } from '../../camera/camera';
/**
* load json with ajax
*
* @param url url to load json file
*/
export function getAjaxJson(url: string, callback: Function) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
const resp: string = xhr.responseText;
const respJson = JSON.parse(resp);
callback(respJson);
// resolve(respJson);
} else {
// reject(xhr.status);
}
} else {
// reject(xhr.status);
}
};
xhr.send();
}
/**
*
* @param imageUrl
*/
export function getImage(imageUrl: string, callback: Function) {
const image: HTMLImageElement = new Image();
image.onload = () => {
callback(image);
};
image.onerror = () => {
console.warn(`image(${imageUrl}) load err`);
};
image.src = imageUrl;
}
/**
*
* @param dracoUrl
* @param callback
*
* https://github.com/kioku-systemk/dracoSample/blob/5611528416d4e0afb10cbec52d70493602d8a552/dracoloader.js#L210
*
*/
export function loadDraco(dracoUrl: string, callback: Function) {
let xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200 || xhr.status === 0) {
callback(xhr.response);
} else {
console.error("Couldn't load [" + dracoUrl + '] [' + xhr.status + ']');
}
}
};
xhr.open('GET', dracoUrl, true);
xhr.send(null);
}