-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
191 lines (165 loc) · 6.64 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import * as THREE from 'three';
import { OrbitControls } from 'OrbitControls';
import 'd3';
'use strict';
function getStageWidth() {
return Math.min(512, window.innerWidth);
};
function getStageHeight() {
return Math.min(512, window.innerHeight);
};
function getAspectRatio() {
return getStageWidth() / getStageHeight();
};
function animate() {
if (window.animating === false) { return; }
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(animate);
};
function updateBars(data) {
if (window.updating === false) { return; }
console.log('updateBars', data);
const duration = 2000;
const xScale = d3.scaleLinear()
.domain([0, d3.max(data, (d) => d.population)])
.range([0, 1]);
const yScale = d3.scaleLinear()
.domain([0, d3.max(data, (d) => d.population)])
.range([0, 1]);
const join = customContainer.selectAll('bar')
.data(data, (d, i) => d.value);
join.enter()
.append('bar')
.each(function(d, i, nodes) {
console.log('enter', i);
const group = new THREE.Group();
group.name = 'bar';
const geometry = new THREE.BoxGeometry(1, 1, 1);
// const material = new THREE.MeshPhongMaterial({ color: color, transparent: true, opacity: 0 });
const material = new THREE.MeshBasicMaterial({
color: d.color,
transparent: true,
opacity: 1,
});
const barMesh = new THREE.Mesh(geometry, material);
barMesh.position.x = i;
barMesh.position.y = yScale(d.population) / 2;
barMesh.scale.set(1, 0, 1);
barMesh.userData = {
targetY: yScale(d.population) / 2,
targetOpacity: 1,
};
barMeshes.push(barMesh);
group.add(barMesh);
scene.add(group);
// scene.add(barMesh);
nodes[i].group = group;
});
join
.each((d, i, nodes) => {
const barMesh = nodes[i].group.children[0];
console.log('update', i, barMesh.userData);
barMesh.userData.targetX = i;
barMesh.userData.targetY = yScale(d.population) / 2;
barMesh.userData.targetHeight = yScale(d.population);
barMesh.userData.targetOpacity ^= 1;
barMesh.material.color.set(d.color);
barMesh.position.y = yScale(d.population) / 2;
barMesh.scale.set(1, yScale(d.population), 1);
});
join.exit()
.each((d, i, nodes) => {
console.log('exit', i);
const barMesh = nodes[i].group.children[0];
barMesh.userData.targetOpacity = 0;
setTimeout(() => {
scene.remove(barMesh);
barMeshes = barMeshes.filter((b) => b !== barMesh);
}, duration)
});
// Animate the bars
barMeshes.forEach((barMesh) => {
d3.select(barMesh.material).transition().duration(duration)
// .tween('opacity', () => (t) => barMesh.material.opacity = d3.interpolate(barMesh.material.opacity, barMesh.userData.opacity)(t));
.tween('opacity', () => {
return (t) => {
// console.log(barMesh.material.opacity, barMesh.userData.targetOpacity);
barMesh.material.opacity = d3.interpolate(barMesh.material.opacity, barMesh.userData.targetOpacity)(t);
};
});
// .tween('opacity', () => {
// return (t) => {
// // console.log(t, barMesh.material.opacity, barMesh.userData.opacity, d3.interpolate(barMesh.material.opacity, barMesh.userData.opacity)(t));
// return barMesh.material.opacity = d3.interpolate(barMesh.material.opacity, barMesh.userData.opacity)(t);
// };
// });
d3.select(barMesh.position).transition().duration(duration)
.tween('position', () => {
return (t) => {
// console.log(t, barMesh.position.y, barMesh.userData.targetY, d3.interpolate(barMesh.position.y, barMesh.userData.targetY)(t));
const newX = d3.interpolate(barMesh.position.x, barMesh.userData.targetX)(t);
const newY = d3.interpolate(barMesh.position.y, barMesh.userData.targetY)(t);
const newHeight = d3.interpolate(barMesh.scale.getComponent(1), barMesh.userData.targetHeight)(t);
if (newX !== undefined) {
barMesh.position.x = newX;
}
// barMesh.position.x = newX;
barMesh.position.y = newY;
barMesh.scale.set(1, newHeight, 1);
};
});
});
animate();
}
window.THREE = THREE;
window.animating = true;
window.updating = true;
const scene = new THREE.Scene();
window.scene = scene;
scene.background = new THREE.Color(0xffffff);
// const camera = new THREE.PerspectiveCamera(75, aspectRatio, 0.1, 1000);
const camera = new THREE.OrthographicCamera(-10 * getAspectRatio(), 10 * getAspectRatio(), 10, -10, 0.1, 100);
window.camera = camera;
camera.position.set(1, 1, 1);
// camera.lookAt(0, 0, 0);
// camera.rotation.set(-Math.PI/2, 0, 0);
const renderer = new THREE.WebGLRenderer();
window.renderer = renderer;
renderer.setSize(getStageWidth(), getStageHeight());
const container = document.querySelector('#container');
// container.addEventListener('click', (e) => {
// console.log(e);
// window.animating = !window.animating;
// });
container.appendChild(renderer.domElement);
const light = new THREE.PointLight(0xffffff);
light.position.set(10, 10, 10);
scene.add(light);
// camera.position.set(0, 0, 10);
// camera.lookAt(0, 0, 0);
window.addEventListener('resize', () => {
camera.aspect = getAspectRatio();
camera.updateProjectionMatrix();
renderer.setSize(getStageWidth(), getStageHeight());
});
const controls = new OrbitControls(camera, renderer.domElement);
controls.update();
const customContainer = d3.select(document.createElement('custom'));
let barMeshes = [];
window.barMeshes = barMeshes;
let data = [
{ value: 'USA', color: 'red', population: 320 },
{ value: 'France', color: 'green', population: 66 },
{ value: 'Japan', color: 'blue', population: 127 },
];
updateBars(data);
setInterval(() => {
// Fisher-Yates https://blog.codinghorror.com/the-danger-of-naivete/
for (let i=0; i<data.length; i++) {
data[i].population = Math.random() * 300;
const j = Math.floor(Math.random() * (i + 1));
[data[i], data[j]] = [data[j], data[i]];
}
updateBars(data);
}, 1000);