Skip to content

Instantly share code, notes, and snippets.

@etra0
Created September 25, 2017 17:54
Show Gist options
  • Save etra0/852c614a8bc3334dcc2ab1a82ba43c88 to your computer and use it in GitHub Desktop.
Save etra0/852c614a8bc3334dcc2ab1a82ba43c88 to your computer and use it in GitHub Desktop.
license: gpl-3.0
height: 600
scrolling: no
border: yes
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style>
.container {
display: flex;
}
</style>
<body>
<div class='container'>
<div class='canvas'>
</div>
<button onclick="add_step(true)">bigger</button>
</div>
<script src='https://d3js.org/d3.v4.min.js'></script>
<script src='main.js'></script>
</body>
</html>
var width = 800,
height = 600;
var currentX = 0,
currentY = 0,
step = 20;
var color_scale = d3.scaleLinear()
.domain([0, 1])
.range(['blue', 'red']);
function generate_line() {
var x0 = currentX,
y0 = currentY,
x1, y1;
var random_value = Math.random()
if (random_value < 0.25) {
x1 = currentX + step;
y1 = currentY + step;
} else if (random_value < 0.5) {
x0 = currentX + step;
x1 = currentX;
y1 = currentY + step;
} else if (random_value < 0.75) {
x0 = currentX;
x1 = currentX;
y0 = currentY;
y1 = currentY + step;
} else {
x0 = currentX;
x1 = currentX + step;
y0 = currentY + step;
y1 = currentY + step;
}
currentX += step;
if (currentX > width) {
currentX = 0;
currentY += step;
}
return `M${x0},${y0}L${x1},${y1}`;
}
var canvas = d3.select('.canvas')
.append('svg')
.attr('width', width)
.attr('height', height);
function t() {
canvas.append('path')
.attr("d", generate_line)
.attr("stroke", 'black')
.attr("stroke-width", 2);
};
for (i = 0; i < width*height/(step*(step - 1)); i++)
t();
function generate() {
currentX = 0;
currentY = 0;
d3.selectAll('path')
.transition(200)
.attr('d', generate_line)
.attr('stroke', color_scale(Math.random()));
}
d3.interval(generate, 500);
function add_step(add) {
if (add)
step++;
else
step--;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment