-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.ts
More file actions
62 lines (56 loc) · 1.8 KB
/
utils.ts
File metadata and controls
62 lines (56 loc) · 1.8 KB
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
import {InMemoryLoadOptions, Scene, WonderlandEngine} from '@wonderlandengine/api';
import {fetchWithProgress, onImageReady} from '@wonderlandengine/api/utils/fetch.js';
import {projectURL} from './setup.js';
class ResourceLike {
_index = -1;
constructor(index: number) {
this._index = index;
}
equals(other: ResourceLike) {
return this._index === other._index;
}
}
/**
* Create a dummy image with dimensions `width * height`.
*
* @param width The image width
* @param height The image height
* @returns The image
*/
export function dummyImage(width: number, height: number): Promise<HTMLImageElement> {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const img = new Image(width, height);
img.src = canvas.toDataURL();
return onImageReady(img);
}
/**
* Create a mocked {@link Scene} object.
*
* @param index Index of the scene
* @returns An object with a similar structure to {@link Scene}.
*/
export function mockedScene(index: number = 0) {
return new ResourceLike(index) as unknown as Scene;
}
/**
* Load multiple project bins.
*
* @param filenames Name of each bin to load
* @returns A promise that resolve with an array of object
* that can be used with {@link WonderlandEngine.loadSceneAsGroupFromMemory}
* and {@link SceneGroup.loadSceneFromBuffer}.
*/
export async function loadProjectBins(...filenames: string[]) {
const bins: InMemoryLoadOptions[] = [];
const promises: Promise<InMemoryLoadOptions>[] = [];
for (const filename of filenames) {
promises.push(
fetchWithProgress(projectURL(filename)).then((buffer) => {
return {filename, baseURL: projectURL(''), buffer};
})
);
}
return Promise.all(promises);
}