-
Notifications
You must be signed in to change notification settings - Fork 7
/
example2.html
89 lines (74 loc) · 2.79 KB
/
example2.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Using HtmCells as model</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://threejs.org/build/three.min.js"></script>
<script src="dist/cell-viz-3d-1.4.4.min.js"></script>
<script>
$(function() {
/* From http://stackoverflow.com/questions/7128675/from-green-to-red-color-depend-on-percentage */
function getGreenToRed(percent){
var r, g;
percent = 100 - percent;
r = percent < 50 ? 255 : Math.floor(255-(percent*2-100)*255/100);
g = percent > 50 ? 255 : Math.floor((percent*2)*255/100);
return rgbToHex(r, g, 0);
}
/* From http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb */
function rgbToHex(r, g, b) {
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function xyzToOneDimIndex(x, y, z, xMax, yMax, zMax) {
var result = (z * xMax * yMax) + (y * xMax) + x;
// console.log('%s, %s, %s : %s', x, y, z, result);
return result;
}
function colorCellsGradient(myCells) {
var xMax = cells.getX();
var yMax = cells.getY();
var zMax = cells.getZ();
var max = xMax * yMax * zMax;
var index;
var percent;
var color;
for (var cx = 0; cx < xMax; cx++) {
for (var cy = 0; cy < yMax; cy++) {
for (var cz = 0; cz < zMax; cz++) {
index = xyzToOneDimIndex(cx, cy, cz, xMax, yMax, zMax);
percent = (index / max) * 100;
color = new THREE.Color('#' + getGreenToRed(percent));
myCells.update(
cx, cy, cz,
{color: color}
);
}
}
}
}
// The Cells interface is how to update the visualization inside the canvas.
var cells = new HtmCells(10, 20, 4);
// The HtmCellVisualization object handles all interactions with the DOM.
var cellviz = new SingleLayerVisualization(cells);
cellviz.camera.translateX(500)
cellviz.camera.translateY(2200)
cellviz.camera.translateZ(6000)
cellviz.camera.rotateX(-0.3)
colorCellsGradient(cells);
// Renders the canvas with empty cells into the DOM and canvas.
cellviz.render();
setInterval(function() {
colorCellsGradient(cells);
cellviz.redraw();
}, 500);
});
</script>
</body>
</html>