-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathindex.js
89 lines (76 loc) · 1.78 KB
/
index.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
import React, { memo, useEffect, useLayoutEffect, useRef } from 'react';
import SceneManager from './SceneManager';
let raf;
let resizeCanvas;
/**
* Three Instance Entrance Point
* @param {*} containerElement
*/
const threeEntryPoint = ({id, ...otherProps}) => {
const canvas = document.getElementById(id);
const sceneManager = SceneManager.getInstance(canvas, otherProps);
raf = null;
function bindEventListeners() {
window.addEventListener ("resize", resizeCanvas);
resizeCanvas();
}
resizeCanvas = function() {
canvas.style.width = '100%';
canvas.style.height = '100%';
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
sceneManager.onWindowResize();
}
const render = () => {
sceneManager.update();
raf = requestAnimationFrame(render);
}
bindEventListeners();
render();
const cleanFunc = () => {
const gl = canvas.getContext('webgl');
gl.clearColor(1.0, 1.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
window.removeEventListener("resize", resizeCanvas);
cancelAnimationFrame(raf);
SceneManager.destroyInstance();
}
return [cleanFunc];
}
/**
* Three Cube Class
*/
const ThreeCube = (props) => {
const {
data = [],
animate = true,
id = 'canvasGlobe',
visType = 'point',
moon = true
} = props;
useEffect(() => {
if (!data.length) return () => {};
const [cleanFunc] = threeEntryPoint({
id,
data,
animate,
visType, // 'cube', curve', 'point'
moon,
});
return () => {
cleanFunc();
};
});
return (
<canvas
id={id}
// ref={canvasEl}
style={{
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,1)'
}}
/>
);
};
export default ThreeCube;