-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialSetting.js
executable file
·72 lines (67 loc) · 1.78 KB
/
initialSetting.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {decode, encode} from "./util/arrayBufferToBase64.js";
/**
* @public
* @param {InitialSetting} setting
* @return {string}
*/
export const save = (setting) => {
const data = new ArrayBuffer(5 * Float64Array.BYTES_PER_ELEMENT + 8 * Int8Array.BYTES_PER_ELEMENT);
const f64 = new Float64Array(data, 0, 5);
const i8 = new Int8Array(data, 5 * Float64Array.BYTES_PER_ELEMENT, 8);
f64[0] = setting.radius;
f64[1] = setting.theta;
f64[2] = setting.phi;
f64[3] = setting.modelSetList[0].time;
f64[4] = setting.modelSetList[1].time;
i8.set(setting.modelSetList.map((model) => [model.isPlaying, model.x, model.y, model.z]).flat());
return "#" + encode(data);
}
/**
* @public
* @param {string} hashString
* @return {InitialSetting || null}
*/
export const load = (hashString) => {
if (hashString) {
try {
const data = decode(hashString.substring(1));
const f64 = new Float64Array(data, 0, 5);
const i8 = new Int8Array(data, 5 * Float64Array.BYTES_PER_ELEMENT, 8);
return {
radius: f64[0],
theta: f64[1],
phi: f64[2],
modelSetList: [0, 1].map((id) => {
const offset = id * 4;
return {
time: f64[3 + id],
isPlaying: i8[offset] === 1,
x: i8[offset + 1],
y: i8[offset + 2],
z: i8[offset + 3]
}
})
};
} catch (error) {
console.log(error);
}
}
return null;
}
/**
* @typedef {Object} InitialModelSetting
*
* @property {number} time
* @property {boolean} isPlaying
* @property {number} x
* @property {number} y
* @property {number} z
*/
/**
* @typedef {Object} InitialSetting
*
* @property {number} radius
* @property {number} theta
* @property {number} phi
* @property {InitialModelSetting[]} modelSetList
*/