A simple way to use D3 to simulate depth using parallax. As the viewpoint moves side to side, the objects in the distance appear to move slower than the objects close to the camera.
It also fades and blurs the deeper objects to simulate atmosphere.
<html> | |
<head> | |
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
</head> | |
<body> | |
</body> | |
<script> | |
//Create set of datapoints with x,y, and z coordinates | |
function arrangeCoordinates(num_objects){ | |
var data=[]; | |
for (var i = 0; i < num_objects; i++) { | |
data.push({id:i, z:30*(num_objects - i - 1)}) | |
}; | |
return data; | |
} | |
//Depth function. Determines width and movement of objects according to their depth | |
function depth(x,z,distToScreen){ | |
return x*distToScreen/(z+distToScreen) | |
} | |
//Place the objects into 3d space | |
function placeObjects(){ | |
var data = arrangeCoordinates(10) | |
var | |
width = 960, | |
height = 500, | |
r = 100, | |
distToScreen = 10, //smaller numbers are faster | |
bgColor = "rgb(88,172,250)", | |
objFill = "rgb(200,0,0)", | |
objStroke = "rgb(150,0,0)" | |
var zmax = d3.max(data, function(d){ return d.z}) | |
//Simulate atmosphere: colors fade to background color as they get farther away | |
var colorFade = function(startingColor,z){ | |
var scale = d3.scale.linear() | |
.domain(d3.extent(data,function(d){return d.z})) | |
.range([startingColor, bgColor]); | |
return scale(z); | |
} | |
//Create SVG element | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
svg.append("rect") | |
.attr("width", width) | |
.attr("height", height) | |
.attr("fill", bgColor); | |
//Add a blur effect determined by the object depth | |
svg.selectAll("defs") | |
.data(data) | |
.enter() | |
.append("defs").append("filter") | |
.attr("id",function(d){ return "blur" + d.z; }) | |
.append("feGaussianBlur") | |
.attr("in","SourceGraphic") | |
.attr("stdDeviation",function(d){return Math.min(d.z/200,3)}) | |
//Draw circles | |
svg.selectAll("circle") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("cx", width/2) | |
.attr("cy", height/2) | |
.attr("r", function(d){ return depth(r,d.z,distToScreen)}) //size according to depth | |
.attr("fill", function(d){ return colorFade(objFill,d.z)}) //fade deeper objects | |
.attr("stroke",function(d){ return colorFade(objStroke,d.z)}) //fade deeper objects | |
.attr("filter",function(d){ return "url(#blur"+d.z+")" }); //implement blur | |
//Mouse moves the viewpoint | |
d3.select(window).on("mousemove", function() { | |
var x = d3.event.pageX - width / 2, | |
y = d3.event.pageY - height / 2; | |
svg.selectAll("circle") | |
.attr("cx", function(d) { return width/2 + depth(x,d.z,distToScreen)}) | |
.attr("cy", function(d) { return height/2 + depth(y,d.z,distToScreen)}) | |
}); | |
} | |
placeObjects() | |
</script> | |
</html> |
MIT License | |
Copyright (c) 2019 Charlie Guthrie | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |