-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRiveLoader.js
46 lines (42 loc) · 1.07 KB
/
RiveLoader.js
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
import Rive from 'rive-canvas';
/* eslint-disable no-undef */
let RiveModule = null;
let isLoadingModule = false;
const callbacks = [];
function loadRiveModule(cb) {
if (isLoadingModule) {
callbacks.push(cb);
} else if (RiveModule) {
cb(RiveModule);
} else {
console.log('loading module');
isLoadingModule = true;
Rive({
locateFile: (file) => `https://unpkg.com/rive-canvas@0.6.10/${file}`,
}).then((module) => {
isLoadingModule = false;
RiveModule = module;
cb(RiveModule);
for (let cb of callbacks) {
cb(RiveModule);
}
});
}
}
export default function loadRive(url) {
return new Promise((resolve, reject) => {
loadRiveModule((rive) => {
const { load } = rive;
const assetRequest = new Request(url);
fetch(assetRequest)
.then((response) => {
return response.arrayBuffer();
})
.then((buffer) => {
// Load Rive file from buffer.
const file = load(new Uint8Array(buffer));
resolve({ rive, file });
});
});
});
}