blobt
4 years ago
10 changed files with 164 additions and 26 deletions
-
0public/data/pic0.png
-
0public/data/pic1.jpg
-
0public/data/test.txt
-
0public/data/textureUV.jpg
-
31src/App.tsx
-
78src/renderi/commons/HttpRequest.ts
-
9src/renderi/core/ImageInfo.ts
-
50src/renderi/lib/AsyncLoadTestApplication.ts
-
16src/renderi/lib/CameraApplication.ts
-
6src/renderi/webgl/GLApplication.ts
@ -0,0 +1,78 @@ |
|||
import { rejects } from "node:assert"; |
|||
import { ImageInfo } from "../core/ImageInfo"; |
|||
|
|||
export class HttpRequest { |
|||
/** |
|||
* 读取图片 |
|||
* 这个函数要起作用,必须要在tsconfig.json中将default的es5改成ES2015 |
|||
* @param url |
|||
*/ |
|||
public static loadImageAsync(url: string): Promise<HTMLImageElement> { |
|||
return new Promise((resolve, reject): void => { |
|||
const image: HTMLImageElement = new Image(); |
|||
image.src = url; |
|||
image.onload = function () { |
|||
resolve(image); |
|||
} |
|||
image.onerror = function () { |
|||
reject(new Error("Cloud not load image '" + url + "'.")); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 安全读取图片 |
|||
* @param url |
|||
* @param name |
|||
* @returns |
|||
*/ |
|||
public static loadImageAsyncSafe(url: string, name: string = url): Promise<ImageInfo | null> { |
|||
return new Promise((resolve, rehect): void => { |
|||
let image: HTMLImageElement = new Image(); |
|||
image.onload = function () { |
|||
let info: ImageInfo = new ImageInfo(name, image); |
|||
resolve(info); |
|||
} |
|||
|
|||
image.onerror = function () { |
|||
resolve(null); |
|||
} |
|||
image.src = url; |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 读取文档 |
|||
* @param url |
|||
*/ |
|||
public static loadTextFileAsync(url: string): Promise<string> { |
|||
return new Promise((resolve, reject): void => { |
|||
let xhr: XMLHttpRequest = new XMLHttpRequest(); |
|||
xhr.onreadystatechange = (ev: Event): any => { |
|||
if (xhr.readyState === 4 && xhr.status === 200) { |
|||
resolve(xhr.responseText); |
|||
} |
|||
} |
|||
xhr.open("get", url, true, null, null); |
|||
xhr.send(); |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param url |
|||
*/ |
|||
public static loadArrayBufferAsync(url: string): Promise<ArrayBuffer> { |
|||
return new Promise((resolve, reject): void => { |
|||
let xhr: XMLHttpRequest = new XMLHttpRequest(); |
|||
xhr.responseType = "arraybuffer"; |
|||
xhr.onreadystatechange = (ev: Event): any => { |
|||
if (xhr.readyState === 4 && xhr.status === 2000) { |
|||
resolve(xhr.response as ArrayBuffer); |
|||
} |
|||
} |
|||
xhr.open("get", url, true, null, null); |
|||
xhr.send(); |
|||
}); |
|||
} |
|||
} |
@ -0,0 +1,9 @@ |
|||
export class ImageInfo { |
|||
public name: string; |
|||
public image: HTMLElement; |
|||
|
|||
public constructor(path: string, image: HTMLElement) { |
|||
this.name = path; |
|||
this.image = image; |
|||
} |
|||
} |
@ -0,0 +1,50 @@ |
|||
import { HttpRequest } from "../commons/HttpRequest"; |
|||
import { Application } from "../core/Application"; |
|||
|
|||
export class AsyncLoadTestApplication extends Application { |
|||
|
|||
/** |
|||
* @var 需要加载的图片 |
|||
*/ |
|||
private _urls: string[] = ["data/pic0.png", "data/pic1.jpg", "data/textureUV.jpg"]; |
|||
|
|||
/** |
|||
* 逐个加载 |
|||
* 函数体内用了await,所以函数申明必须使用async关键字 |
|||
*/ |
|||
public async loadImageSequence(): Promise<void> { |
|||
for (let i = 0; i < this._urls.length; i++) { |
|||
let image: HTMLImageElement = await HttpRequest.loadImageAsync(this._urls[i]); |
|||
console.log("Load image sequence ", i, image); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 并行加载图片 |
|||
*/ |
|||
public loadImageParallel(): void { |
|||
let _promises: Promise<HTMLImageElement>[] = []; |
|||
for (let i = 0; i < this._urls.length; i++) { |
|||
_promises.push(HttpRequest.loadImageAsync(this._urls[i])); |
|||
} |
|||
Promise.all(_promises).then((images: HTMLImageElement[]) => { |
|||
for (let i: number = 0; i < images.length; i++) { |
|||
console.log("Load image parallel", images[i]); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 加载一个txt文件 |
|||
*/ |
|||
public async loadTextFile(): Promise<void> { |
|||
let str: string = await HttpRequest.loadTextFileAsync("data/test.txt"); |
|||
console.log(str); |
|||
} |
|||
|
|||
public async run(): Promise<void> { |
|||
await this.loadImageSequence(); |
|||
await this.loadTextFile(); |
|||
this.loadImageParallel(); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue