Skip to content

Instantly share code, notes, and snippets.

@curran
Last active October 9, 2016 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curran/d2008a4cd6e865319c8f8eecea588633 to your computer and use it in GitHub Desktop.
Save curran/d2008a4cd6e865319c8f8eecea588633 to your computer and use it in GitHub Desktop.
Tree Fractal with COLORS!
license: mit

A tree fractal using SVG circles. The mouse position determines the branch angle and number of iterations. The number of SVG elements is shown on the bottom. Using this, you can get a feel for how much animation lag is introduced when the number of SVG elements gets high. You can feel the performance limits of SVG.

Built with blockbuilder.org

Forked from a previous version that uses Canvas - Tree Fractal with Circles.

forked from curran's block: Tree Fractal with SVG

forked from curran's block: Tree Fractal with SVG

forked from curran's block: Tree Fractal with COLORS!

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>fractal</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style> body: { margin: 0px }</style>
</head>
<body>
<svg width="960" height="500"></svg>
<script>
var svg = d3.select("svg"),
width = svg.attr("width"),
height = svg.attr("height"),
elementCount = svg.append("text")
.attr("x", width / 2)
.attr("y", height - 20)
.attr("text-anchor", "middle")
.attr("fill", "gray"),
format = d3.format(","),
nMax = 100,
mouseX = 800,
mouseY = 200,
scaleFactor = 0.96;
function redraw (){
var scale = 14,
angle = -mouseY / height * Math.PI / 8,
n = Math.floor(nMax * mouseX / width),
up = Math.PI;
render(fractal(width/2, height - 136, up, scale, angle, 0, n, false));
}
function fractal(x, y, direction, scale, angle, i, n, flip){
var circle = [{
x: x,
y: y,
radius: scale / 2
}];
if(i < n){
if(i % 11 === 0){
return circle
.concat(fractal(x, y, direction, scale, angle, i+1, n, flip))
.concat(fractal(x, y, direction, scale, angle, i+1, n, !flip));
} else {
return circle.concat(
fractal(
x + Math.sin(direction) * scale,
y + Math.cos(direction) * scale,
direction + angle * (flip ? 1 : -1),
scale * scaleFactor,
angle,
i+1,
n,
flip
)
);
}
}
return circle;
}
function r(){
return Math.floor(Math.random()*255);
}
function render(data){
var circles = svg.selectAll("circle").data(data);
circles
.enter()
.append("circle")
.merge(circles)
.attr("cx", function (d){ return d.x; })
.attr("cy", function (d){ return d.y; })
.attr("r", function (d){ return d.radius; })
.attr("fill", function (){
return [
"rgb(",
r(), ",",
r(), ",",
r(),
")"].join("");})
circles.exit()
.remove();
elementCount.text(format(data.length) + " SVG Circle elements");
}
redraw();
document.addEventListener("mousemove",function(e){
mouseX = e.x;
mouseY = e.y;
redraw();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment