-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.js
90 lines (82 loc) · 2.89 KB
/
utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import gifshot from "gifshot";
import * as THREE from "three";
export const UtilsMixin = (superclass) =>
class extends superclass {
// toggles a boolean variable and optionally sets all variables in the antagonists array to the opposite value
toggleBoolean(name, antagonistNames = []) {
this[name] = !this[name];
// disable all antagonists when `name` variable is set to true
const currentValue = this[name];
if (currentValue && antagonistNames.length) {
antagonistNames.forEach((antagonistName) => {
this[antagonistName] = !currentValue;
});
}
}
// eslint-disable-next-line class-methods-use-this
areTwoObjectsShallowEqual(o1, o2) {
return Object.keys(o1)
.map((key) => o1[key] === o2[key])
.reduce((a, b) => a && b);
}
// eslint-disable-next-line class-methods-use-this
getTwoObjectsShallowDifferentKeys(o1, o2) {
const resultingObject = {};
const differentKeysArray = Object.keys(o1).filter((key) => o1[key] !== o2[key]);
differentKeysArray.forEach((key) => (resultingObject[key] = true));
return resultingObject;
}
};
/**
* Applies glow to a THREE object.
* @param meshObjet {Object}: THREE mesh object.
* @param baseColor {String}: hex color string of the glow
* @param offset {Number}: can be a single digit number, 0 means no offset
*/
export const ApplyGlow = (meshObjet, baseColor, offset = 0) => {
const atomHSL = {};
new THREE.Color(baseColor).getHSL(atomHSL);
let hue, saturation;
if (offset !== 0) {
if (offset % 2 === 0) {
// even labels
hue = atomHSL.h + (offset * 0.1) / 2;
saturation = atomHSL.s + (offset * 0.1) / 2;
} else {
// odd labels
hue = atomHSL.h - ((offset + 1) * 0.1) / 2;
saturation = atomHSL.s + ((offset + 1) * 0.1) / 2;
}
// hue is cyclic
while (hue > 1) {
hue -= 1;
}
while (hue < 0) {
hue += 1;
}
saturation = Math.max(0, Math.min(1, saturation));
meshObjet.material.emissiveIntensity = 0.25;
meshObjet.material.emissive.setHSL(hue, saturation, atomHSL.l);
}
};
export function createGIFAsync({
images,
gifWidth,
gifHeight,
numFrames,
frameDuration,
sampleInterval,
}) {
return new Promise((resolve, reject) => {
gifshot.createGIF(
{ images, gifWidth, gifHeight, numFrames, frameDuration, sampleInterval },
(obj) => {
if (!obj.error) {
resolve(obj.image); // Resolve with the GIF data URL
} else {
reject(obj.error); // Reject with the error
}
},
);
});
}