Created by Christopher Manning
Last active
December 10, 2015 10:39
-
-
Save christophermanning/4422612 to your computer and use it in GitHub Desktop.
Café wall illusion
This file contains hidden or 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> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Café wall illusion</title> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script> | |
<style type="text/css"> | |
body { | |
padding: 0; | |
margin: 0; | |
} | |
rect { | |
stroke: #7c7c7c; | |
fill: black; | |
stroke-width: 3px; | |
} | |
path { | |
stroke: #7c7c7c; | |
stroke-width: 3px; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
width = window.innerWidth | |
height = window.innerHeight | |
var config = { "x": 50, "width" : 50, "height": 50, "velocity": .25, "amplitude": 12, "period": 32, "lines" : true, "squares" : true }; | |
var gui = new dat.GUI(); | |
gui.close(); | |
gui.add(config, "x", 0, 100) | |
gui.add(config, "width", 1, 100) | |
gui.add(config, "height", 3, 100) | |
gui.add(config, "velocity", 0, 10) | |
gui.add(config, "amplitude", 0, 100) | |
gui.add(config, "period", 1, height/2) | |
gui.add(config, "lines") | |
gui.add(config, "squares") | |
config.random = function(){ | |
gui.__controllers.forEach(function(c){ | |
if(c.property!="random"&&c.property!="velocity"&&c.property!="lines"&&c.property!="squares"){ | |
c.setValue(Math.floor(Math.random() * c.__max) + c.__min) | |
} | |
}) | |
} | |
gui.add(config, "random") | |
svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var paths = [] | |
var path = svg.selectAll("path") | |
var rects = [] | |
var rect = svg.selectAll("rect") | |
var angle = 0 | |
d3.timer(function(t){ | |
paths = [] | |
rects = [] | |
for (var y = 0; y <= height; y++) { | |
if (rects.length > 5000) break | |
if (config["lines"]) paths.push([[0, y], [width, y]]) | |
for (var x = 20 + (config["amplitude"]*Math.sin((2*Math.PI*angle)/3000+y/config["period"])); x <= width; x++) { | |
if (config["squares"]) rects.push([x, y]) | |
x += config["width"] + config["x"] | |
} | |
y += config["height"] - 1 // -1 to compensate for borders | |
angle += config["velocity"] | |
} | |
rect = rect | |
.data(rects) | |
rect.enter() | |
.insert("rect") | |
rect | |
.attr("x", function(d) { return d[0] }) | |
.attr("y", function(d) { return d[1] }) | |
.attr("width", function(d) { return config["width"] }) | |
.attr("height", function(d) { return config["height"] }) | |
rect.exit() | |
.remove() | |
path = path | |
.data(paths) | |
path.enter() | |
.insert("path") | |
path | |
.attr("d", function(d) { return "M" + d.map(function(d){ return d.join(",") }).join("L") }) | |
path.exit() | |
.remove() | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment