Testing line_strip with d3-3d
Last active
August 10, 2017 20:46
-
-
Save Niekes/479b47a1b650d24f42b078afa2cba3d1 to your computer and use it in GitHub Desktop.
d3-3d: 3D LINE_STRIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.d3=t.d3||{})}(this,function(t){"use strict";function e(t,e){return t.z=-t.z,t.y=-t.y,n(r(o(t,e.y),e.x),e.z)}function r(t,e){var r=Math.sin(e),o=Math.cos(e);return{x:t.x,y:t.y*o-t.z*r,z:t.y*r+t.z*o}}function o(t,e){var r=Math.sin(e),o=Math.cos(e);return{x:t.z*r+t.x*o,y:t.y,z:t.z*o-t.x*r}}function n(t,e){var r=Math.sin(e),o=Math.cos(e);return{x:t.x*o-t.y*r,y:t.y*r+t.y*o,z:t.z}}function a(t,e){return{x:e.origin[0]+e.scale*t.x,y:e.origin[1]+e.scale*t.y}}function d(t,r,o,n){for(var d=t.length-1;d>=0;d--){var c=t[d];c.rotated=e({x:o.x(c),y:o.y(c),z:o.z(c)},n),c.centroid=c.rotated,c.projected=a(c.rotated,r)}return t}function c(t,r,o,n){for(var d=t.length-1;d>=0;d--){var c=t[d],u=c[0],y=c[1];u.rotated=e({x:o.x(u),y:o.y(u),z:o.z(u)},n),y.rotated=e({x:o.x(y),y:o.y(y),z:o.z(y)},n),u.projected=a(u.rotated,r),y.projected=a(y.rotated,r),c.lng=Math.sqrt(Math.pow(y.rotated.x-u.rotated.x,2)+Math.pow(y.rotated.y-u.rotated.y,2)+Math.pow(y.rotated.z-u.rotated.z,2)),c.centroid={x:(u.x+y.x)/2,y:(u.y+y.y)/2,z:(u.z+y.z)/2}}return t}function u(t,r,o,n){for(var d=t.length-1;d>=0;d--)for(var c=t[d],u=c.length-1;u>=0;u--){var y=c[u];y.rotated=e({x:o.x(y),y:o.y(y),z:o.z(y)},n),y.centroid=y.rotated,y.projected=a(y.rotated,r)}return t}function y(t,r,o,n){for(var d=t.length-1;d>=0;d--){var c=t[d],u=c[0],y=c[1],i=c[2];u.rotated=e({x:o.x(u),y:o.y(u),z:o.z(u)},n),y.rotated=e({x:o.x(y),y:o.y(y),z:o.z(y)},n),i.rotated=e({x:o.x(i),y:o.y(i),z:o.z(i)},n),u.projected=a(u.rotated,r),y.projected=a(y.rotated,r),i.projected=a(i.rotated,r);var f=(y.rotated.x-u.rotated.x)*(y.rotated.y+u.rotated.y),x=(i.rotated.x-y.rotated.x)*(i.rotated.y+y.rotated.y),z=(u.rotated.x-i.rotated.x)*(u.rotated.y+i.rotated.y);c.ccw=f+x+z<0,c.centroid={x:(u.rotated.x+y.rotated.x+i.rotated.x)/3,y:(u.rotated.y+y.rotated.y+i.rotated.y)/3,z:(u.rotated.z+y.rotated.z+i.rotated.z)/3}}return t}function i(t){return"M"+t[0].projected.x+","+t[0].projected.y+"L"+t[1].projected.x+","+t[1].projected.y+"L"+t[2].projected.x+","+t[2].projected.y+"Z"}function f(t){for(var e=t[t.length-1],r="M"+e.projected.x+","+e.projected.y,o=t.length-2;o>=0;o--){var n=t[o].projected;r+="L"+n.x+","+n.y}return r}function x(t){return t[0]}function z(t){return t[1]}function p(t){return t[2]}var h=function(){function t(t){return j[v](t,{scale:r,origin:e},{x:h,y:l,z:g},{x:o,y:n,z:a})}var e=[0,0],r=1,o=0,n=0,a=0,h=x,l=z,g=p,v="POINTS",j={POINTS:d,LINES:c,LINES_STRIP:u,TRIANGLES:y},s={TRIANGLES:i,LINES_STRIP:f};return t.origin=function(r){return arguments.length?(e=r,t):e},t.scale=function(e){return arguments.length?(r=e,t):r},t.rotateX=function(e){return arguments.length?(o=e,t):o},t.rotateY=function(e){return arguments.length?(n=e,t):n},t.rotateZ=function(e){return arguments.length?(a=e,t):a},t.primitiveType=function(e){return arguments.length?(v=e,t):v},t.x=function(e){return arguments.length?(h="function"==typeof e?e:+e,t):h},t.y=function(e){return arguments.length?(l="function"==typeof e?e:+e,t):l},t.z=function(e){return arguments.length?(g="function"==typeof e?e:+e,t):g},t.draw=function(t){if("POINTS"!==v&&"LINES"!==v)return s[v](t)},t};t._3d=h,Object.defineProperty(t,"__esModule",{value:!0})}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="d3-3d.js"></script> | |
<body> | |
<svg width="960" height="500"></svg> | |
<script> | |
var origin = [480, 250], alpha = Math.PI/4, beta = 0.615472907, data = [], max = 4, min = -max, rad = Math.PI/180; | |
var svg = d3.select('svg'); | |
var color = d3.scaleOrdinal(d3.schemeCategory10); | |
var rn = function(min, max){ return Math.round(d3.randomUniform(min, max + 1)()); }; | |
d3.range(-4, 5, 2).forEach(function(i){ | |
var o = [ | |
[ 3, 0 , i], | |
[ 2, rn(min, max), i], | |
[ 1, rn(min, max), i], | |
[ 0, rn(min, max), i], | |
[-1, rn(min, max), i], | |
[-2, rn(min, max), i], | |
[-3, 0 , i] | |
]; | |
data.push(o); | |
}); | |
var _3d = d3._3d() | |
.scale(30) | |
.origin(origin) | |
.primitiveType('LINES_STRIP') | |
.rotateX(alpha) | |
.rotateY(beta); | |
var data3D = _3d(data); | |
function processData(data){ | |
var linesStrip = svg.selectAll('path').data(data); | |
linesStrip | |
.enter() | |
.append('path') | |
.merge(linesStrip) | |
.attr('fill', 'none') | |
.attr('stroke', function(d, i){ return color(i); }) | |
.attr('stroke-width', 2) | |
.sort(function(a, b){ return b[0].rotated.z - a[0].rotated.z; }) | |
.attr('d', _3d.draw); | |
linesStrip.exit().remove(); | |
} | |
processData(data3D); | |
setInterval(function(){ | |
beta += rad; | |
processData(_3d.rotateY(beta)(data)); | |
}, 40); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment