torque/examples/contour.html

104 lines
4.2 KiB
HTML
Raw Normal View History

2015-08-12 17:36:00 +08:00
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<canvas id = "sample" width = "600" height = "600"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
2015-08-12 17:36:00 +08:00
<script type="text/javascript">
var tile = {"x":[96,64,160,64,48,192,16,80,112,16,64,176,80,64,32,176,16,144,176,176,112,128,224,16,16,176,16,0,64,160,128,160,208,80,192,0,80,16,192,128,208,160,128,192,32,112,144,112,80,32,80,112,32,80,112,80,32,144,112,144,32,80,32,176,112,208,32,96,192,144,160,144,208,144,0,112,224,160,16,0,208,128,48,0,176,96,96,48,48,64,96,48,96,48,96],"y":[176,192,208,160,176,208,224,32,48,64,144,224,208,128,96,240,112,208,160,192,208,208,240,192,240,144,80,160,208,160,176,192,240,128,240,240,144,48,192,224,192,240,16,160,176,16,224,224,240,224,192,176,16,160,240,224,160,240,160,192,80,176,240,208,192,224,112,208,224,128,176,144,176,112,224,144,208,224,176,208,208,96,64,32,176,112,144,144,112,176,192,160,160,240,240],"z":6,"coord":{"x":18,"y":24,"z":6},"timeCount":[95],"timeIndex":[0],"renderDataPos":[0,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],"renderData":[3,4,3,3,3,4,2,1,1,1,1,5,2,3,1,11,2,5,2,6,2,4,4,5,1,1,1,1,1,1,2,1,8,7,15,1,8,1,1,4,3,6,1,1,1,2,2,1,5,2,4,1,1,2,2,1,1,5,2,4,1,1,2,2,2,5,2,6,8,1,2,1,1,2,1,1,1,2,3,1,4,1,2,1,3,1,4,1,1,2,1,2,2,4,2],"maxDate":0};
2015-08-12 17:36:00 +08:00
var grid = gridData(tile);
2015-08-12 17:36:00 +08:00
var canvas = document.getElementById("sample");
var ctx = canvas.getContext("2d");
for (var y = 0; y < grid.length; y++){
for (var x = 0; x < grid[0].length; x++){
ctx.beginPath();
ctx.arc(20 + x*20, 20 + y*20, 3, 0, 2 * Math.PI, false);
var value = grid[y][x];
ctx.fillStyle = "rgb("+0+", "+value * 50+", "+value * 50+")";
if(value === 0) ctx.fillStyle = "red";
ctx.fill();
}
}
2015-08-12 17:36:00 +08:00
var cellsX = grid[0].length-1;
var cellsY = grid.length-1;
var contourValues = [0,2];
for(var c = 0; c < contourValues.length; c++){
for (var y = 0; y < cellsY; y++){
for (var x = 0; x < cellsX; x++){
var currentCell = [grid[y][x], grid[y][x+1], grid[y+1][x+1], grid[y+1][x]];
var pipe = getPipe(currentCell, contourValues[c]);
if (pipe){
ctx.beginPath();
ctx.moveTo(20 + x*20 + 20 * pipe[0][0], 20 + y*20 + 20 * pipe[0][1]);
ctx.lineTo(20 + x*20 + 20 * pipe[1][0], 20 + y*20 + 20 * pipe[1][1]);
ctx.stroke();
if (pipe.length === 4){
ctx.beginPath();
ctx.moveTo(20 + x*20 + 20 * pipe[2][0], 20 + y*20 + 20 * pipe[2][1]);
ctx.lineTo(20 + x*20 + 20 * pipe[3][0], 20 + y*20 + 20 * pipe[3][1]);
ctx.stroke();
}
}
}
}
}
function gridData(tile){
var res = 16;
var grid = new Array(256/res);
for(var i =0; i<grid.length; i ++){
grid[i] = new Array(256/res);
for(var j =0; j < grid[i].length; j++){
grid[i][j] = 0;
}
}
for(var i =0; i < tile.renderData.length; i++){
var x = tile.x[i], y = tile.y[i];
grid[y/res][x/res] = tile.renderData[i];
}
return grid;
}
2015-08-12 17:36:00 +08:00
function getPipe(cell, contour){
var parsedCell = cell.map(function(cornerValue){
if (cornerValue > contour){
return "1";
}
return "0";
}).join("");
var type = parseInt(parsedCell, 2);
var N = [lerp(cell[1], cell[0], contour), 0],
S = [lerp(cell[2], cell[3], contour), 1],
E = [1, lerp(cell[2], cell[1], contour)],
W = [0, lerp(cell[3], cell[0], contour)]
// Blank
if (type === 0 || type === 15) return null;
// W - S
if (type === 1 || type === 14) return [W, S]
// S - E
if (type === 2 || type === 13) return [S, E]
// W - E
if (type === 3 || type === 12) return [W, E]
// N - E
if (type === 4 || type === 11) return [N, E]
// N - S
if (type === 6 || type === 9) return [N, S]
// W - N
if (type === 7 || type === 8) return [W, N]
// W - N / S - E
if (type === 5) return [W, N, S, E]
// W - S / N - E
if (type === 10) return [W, S, N, E]
}
// Linear interpolation
function lerp(valueA, valueB, contourValue){
return Math.max(Math.min(1 + (-0.5) * (contourValue - valueA) / (valueB - valueA), 0.8), 0.2);
}
2015-08-12 18:10:37 +08:00
</script>