Skip to content

Instantly share code, notes, and snippets.

@syntagmatic
Last active August 10, 2018 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save syntagmatic/3736720 to your computer and use it in GitHub Desktop.
Save syntagmatic/3736720 to your computer and use it in GitHub Desktop.
Zoomable Julia Set
<style type="text/css">
html, body {
padding; 0;
margin: 0;
}
body {
background:#000;
color:white;
font-family: Arial,Helvetica,Sans-serif;
}
a:hover {
text-decoration: none;
}
canvas {
position: absolute;
}
</style>
<canvas id="theCanvas" width="803" height="400"></canvas>
<canvas id="interactionCanvas" width="803" height="400"></canvas>
<script src="http://d3js.org/d3.v2.js"></script>
<script src="http://underscorejs.org/underscore.js"></script>
<script src="julia.js"></script>
<script>
var width = document.body.clientWidth;
var height = document.body.clientHeight;
var canvas = document.getElementById('theCanvas');
var interaction_canvas = document.getElementById('interactionCanvas');
var real_scale = d3.scale.linear()
.range([0, width])
.domain([-1.7,1.7]);
var imag_scale = d3.scale.linear()
.range([0, height])
.domain([-1,1]);
d3.selectAll("canvas")
.attr("width", width)
.attr("height", height);
var ctx = canvas.getContext('2d');
var interaction_context = interaction_canvas.getContext('2d');
jul = julia(width,height)
.context(ctx);
jul.color = d3.scale.linear()
.domain(d3.range(0,2000,20))
.range(d3.range(0,100).map(function(i) { return d3.hcl(190+120*i, 40+35*Math.round(Math.cos(i/5)), 18+6*(i%10)).toString(); }))
.interpolate(d3.interpolateLab);
var zoom = d3.behavior.zoom()
.x(real_scale)
.y(imag_scale)
.on("zoom", onzoom)
function onzoom() {
d3.select("#input-realMin").attr("value", real_scale.domain()[0]);
d3.select("#input-imagMin").attr("value", imag_scale.domain()[0]);
d3.select("#input-realMax").attr("value", real_scale.domain()[1]);
d3.select("#input-imagMax").attr("value", imag_scale.domain()[1]);
jul.zoom(real_scale.domain(), imag_scale.domain());
};
// bind inputs to julia parameters
d3.keys(jul.__).forEach(function(key) {
d3.select("#tools")
.append("div")
.html(key + "<br/>")
.append("input")
.attr({
id: "input-" + key,
type: "text",
value: jul[key]()
})
.on("keyup", function() {
jul[key](this.value);
});
});
jul.render();
jul.go();
// toggle tools visibility
var gear_active = false;
d3.select("#zoom-gear").on("click", function() {
gear_active = !gear_active;
d3.select("#tools").style("display", function() { return gear_active ? "block" : "none" });
d3.select(this).classed("active", gear_active);
});
d3.select("#zoom-reset").on("click", function() {
real_scale.domain([-1.7,1.7]);
imag_scale.domain([-1,1]);
zoom.scale(1)
.translate([0,0])
.x(real_scale)
.y(imag_scale);
onzoom();
});
d3.select(interaction_canvas)
.call(zoom);
window.onresize = function() {
width = document.body.clientWidth;
height = document.body.clientHeight;
real_scale.range([0, width]);
imag_scale.range([0, height]);
zoom
.x(real_scale)
.y(imag_scale)
.scale(1)
.translate([0,0]);
jul.x_extent(width).y_extent(height);
d3.selectAll("canvas")
.attr("width", width)
.attr("height", height);
onzoom();
};
</script>
<style type="text/css">
html, body {
padding; 0;
margin: 0;
}
body {
background:#000;
color:white;
font-family: Arial,Helvetica,Sans-serif;
font-size: 12px;
}
a:hover {
text-decoration: none;
}
canvas {
position: absolute;
cursor: move;
}
#tools {
color: #eee;
position: fixed;
top: 44px;
left: 12px;
line-height: 18px;
padding: 6px 8px 6px 8px;
z-index: 10;
background: rgba(0,0,0,0.6);
height: 40px;
border-radius: 4px;
}
#tools:hover {
opacity: 1;
}
#tools > div {
width: 80px;
float: left;
}
#tools input[type=text] {
color: #fff;
font-weight: bold;
width: 70px;
margin: 0 0 4px 0;
padding: 4px 5px;
background: rgba(0,0,0,0.1);
border: none;
}
#tools:hover input[type=text] {
color: #eee;
}
#zoom {
position: fixed;
top: 12px;
left: 12px;
font-size: 24px;
line-height: 24px;
font-weight: bold;
text-align: center;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#zoom div {
float: left;
width: 24px;
background: rgba(0,0,0,0.6);
border-radius: 4px;
margin-right: 4px;
padding: 2px;
}
#zoom div:hover {
cursor: pointer;
background: rgba(0,0,0,1);
}
#zoom-reset {
font-size: 16px;
}
#zoom #zoom-gear {
font-size: 16px;
color: rgba(255,255,255,0.3);
background: rgba(0,0,0,0.2);
}
#zoom #zoom-gear:hover {
color: rgba(255,255,255,1);
}
#zoom #zoom-gear.active {
color: inherit;
background: rgba(0,0,0,0.6);
}
</style>
<canvas id="theCanvas" width="803" height="400"></canvas>
<canvas id="interactionCanvas" width="803" height="400"></canvas>
<div id="zoom">
<div id="zoom-reset" title="Reset zoom">&sect;</div>
<div id="zoom-gear" title="Change fractal parameters">&#9881;</div>
</div>
<div id="tools" style="display:none">
</div>
<script src="http://d3js.org/d3.v2.js"></script>
<script src="http://underscorejs.org/underscore.js"></script>
<script src="julia.js"></script>
<script>
var width = document.body.clientWidth;
var height = document.body.clientHeight;
var canvas = document.getElementById('theCanvas');
var interaction_canvas = document.getElementById('interactionCanvas');
var real_scale = d3.scale.linear()
.range([0, width])
.domain([-1.7,1.7]);
var imag_scale = d3.scale.linear()
.range([0, height])
.domain([-1,1]);
d3.selectAll("canvas")
.attr("width", width)
.attr("height", height);
var ctx = canvas.getContext('2d');
var interaction_context = interaction_canvas.getContext('2d');
jul = julia(width,height)
.context(ctx)
.minResolution(21);
jul.color = d3.scale.linear()
.domain([0, 12, 30, 50, 100, 180, 260, 380, 600, 800, 1200, 1600,3200])
.range(["moccasin", "#999", "steelblue", "yellow", "brown", "#222", "pink", "purple", "#027", "#260", "orange", "yellow", "blue"])
.interpolate(d3.interpolateHcl);
var zoom = d3.behavior.zoom()
.x(real_scale)
.y(imag_scale)
.on("zoom", onzoom)
function onzoom() {
d3.select("#input-realMin").attr("value", real_scale.domain()[0]);
d3.select("#input-imagMin").attr("value", imag_scale.domain()[0]);
d3.select("#input-realMax").attr("value", real_scale.domain()[1]);
d3.select("#input-imagMax").attr("value", imag_scale.domain()[1]);
jul.zoom(real_scale.domain(), imag_scale.domain());
};
// bind inputs to julia parameters
d3.keys(jul.__).forEach(function(key) {
d3.select("#tools")
.append("div")
.html(key + "<br/>")
.append("input")
.attr({
id: "input-" + key,
type: "text",
value: jul[key]()
})
.on("keyup", function() {
jul[key](this.value);
});
});
jul.render();
jul.go();
// toggle tools visibility
var gear_active = false;
d3.select("#zoom-gear").on("click", function() {
gear_active = !gear_active;
d3.select("#tools").style("display", function() { return gear_active ? "block" : "none" });
d3.select(this).classed("active", gear_active);
});
d3.select("#zoom-reset").on("click", function() {
real_scale.domain([-1.7,1.7]);
imag_scale.domain([-1,1]);
zoom.scale(1)
.translate([0,0])
.x(real_scale)
.y(imag_scale);
onzoom();
});
d3.select(interaction_canvas)
.call(zoom);
window.onresize = function() {
width = document.body.clientWidth;
height = document.body.clientHeight;
real_scale.range([0, width]);
imag_scale.range([0, height]);
zoom
.x(real_scale)
.y(imag_scale)
.scale(1)
.translate([0,0]);
jul.x_extent(width).y_extent(height);
d3.selectAll("canvas")
.attr("width", width)
.attr("height", height);
onzoom();
};
</script>
function julia(x_extent, y_extent) {
var jul = {};
var __ = {
realMin: -1.7,
realMax: 1.7,
imagMin: -1,
imagMax: 1,
CR: -.8,
CI: .156,
maxIter: 2000,
minResolution: 40,
};
var events = d3.dispatch.apply(this, ["done"].concat(d3.keys(__)));
jul.color = d3.scale.sqrt()
.domain([0, __.maxIter])
.range(["white", "black"])
.interpolate(d3.interpolateLab);
jul.__ = __;
getset(jul, __, events);
d3.rebind(jul, events, "on");
var ctx;
var _x = 0;
var _y = 0;
var resolution = __.minResolution;
var done = false;
jul.zoom = function(reals,imags) {
done = true;
__.realMin = reals[0];
__.realMax = reals[1];
__.imagMin = imags[0];
__.imagMax = imags[1];
jul.resetForRender();
}
jul.iterate = function(real,imag) {
var zr = real;
var zi = imag;
var iterations = 0;
while (true) {
iterations++;
if ( iterations > __.maxIter ) return 0;
zr_next = zr * zr - zi * zi + __.CR;
zi_next = 2 * zi * zr + __.CI;
zr = zr_next;
zi = zi_next;
if ( zr > 4 ) return iterations;
if ( zi > 4 ) return iterations;
}
return iterations;
}
jul.render = function() {
if (done)
return;
if ( jul.boxes() )
return;
var realSpan = __.realMax - __.realMin;
var imagSpan = __.imagMax - __.imagMin;
var realMin = __.realMin;
var imagMin = __.imagMin;
var fast_color = _.memoize(jul.color);
var ll = _x + 6; // how many columns to render at once
for ( ; _x < ll; ++_x) {
for ( _y = 0; _y < y_extent; ++_y) {
var fx = _x / x_extent;
var fy = _y / y_extent;
var real = fx * realSpan + realMin;
var imag = fy * imagSpan + imagMin;
var iterations = jul.iterate(real,imag);
ctx.fillStyle = fast_color(iterations);
ctx.fillRect(_x,_y,1,1);
}
}
if ( _x >= x_extent ) {
done = true;
events.done.call(jul);
}
}
jul.boxes = function() {
if ( resolution <= 3 ) {
return false;
}
var realSpan = __.realMax - __.realMin;
var imagSpan = __.imagMax - __.imagMin;
var fast_color = _.memoize(jul.color);
for ( _x = 0; _x < x_extent; _x += resolution) {
for ( _y = 0; _y < y_extent; _y += resolution) {
var fx = (_x + resolution/2) / x_extent;
var fy = (_y + resolution/2) / y_extent;
var real = fx * realSpan + __.realMin;
var imag = fy * imagSpan + __.imagMin;
var iterations = jul.iterate(real,imag);
ctx.fillStyle = fast_color(iterations);
ctx.fillRect(_x,_y,resolution,resolution);
}
}
resolution -= 3;
_x = 0;
_y = 0;
return true;
}
jul.resetForRender = function() {
_x = 0;
_y = 0;
resolution = __.minResolution;
done = false;
}
jul.context = function(_) {
if (!arguments.length) return ctx;
ctx = _;
return this;
};
jul.x_extent = function(_) {
if (!arguments.length) return x_extent;
x_extent = _;
jul.resetForRender();
return this;
};
jul.y_extent = function(_) {
if (!arguments.length) return y_extent;
y_extent = _;
jul.resetForRender();
return this;
};
jul.go = function() {
var render = function() {
jul.render();
}
d3.timer(render);
}
// getter/setter with event firing
function getset(obj,state) {
d3.keys(state).forEach(function(key) {
obj[key] = function(x) {
if (!arguments.length) return state[key];
var old = state[key];
state[key] = typeof (x - 0) == "number" ? x - 0 : x;
events[key].call(jul,{"value": state[key], "previous": old});
obj.resetForRender();
return obj;
};
});
};
return jul;
}
Modified by Kai Chang, Copyright (c) 2012
Copyright (c) 2010 DougX.net
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.
<style type="text/css">
html, body {
padding; 0;
margin: 0;
}
body {
background:#000;
color:white;
font-family: Arial,Helvetica,Sans-serif;
}
a:hover {
text-decoration: none;
}
canvas {
position: absolute;
}
</style>
<canvas id="theCanvas" width="803" height="400"></canvas>
<canvas id="interactionCanvas" width="803" height="400"></canvas>
<script src="http://d3js.org/d3.v2.js"></script>
<script src="http://underscorejs.org/underscore.js"></script>
<script src="julia.js"></script>
<script>
var width = document.body.clientWidth;
var height = document.body.clientHeight;
var canvas = document.createElement('canvas');
var visible_canvas = document.getElementById('theCanvas');
canvas.width = width;
canvas.height = height;
d3.selectAll("canvas")
.attr("width", width)
.attr("height", height);
var ctx = canvas.getContext('2d');
var visible_ctx = visible_canvas.getContext('2d');
visible_ctx.fillStyle = "white";
visible_ctx.font = "12pt sans-serif";
visible_ctx.fillText("rendering... shrink + refresh to render frames faster", width/2, height/2);
jul = julia(width,height)
.context(ctx)
.minResolution(0)
.realMin(-1.7)
.realMax(1.7)
.imagMin(-1)
.imagMax(1);
jul.render();
var image_datas = [];
// this causes a recursive loop by re-rendering when CR set
jul.on("done", function() {
console.log("new iteration");
image_datas.push(ctx.getImageData(0,0,width,height));
jul.CI(jul.CI() + 0.0002);
})
// start rendering
jul.go();
// play frames
play();
var stop = false;
function play() {
// loop through rendered images
var i = 0;
var forwards = true;
function renderNext() {
if (stop) return true;
if (image_datas.length == 0) return;
if (i == image_datas.length) {
i--;
forwards = false;
return;
}
if (i == 0) {
visible_ctx.putImageData(image_datas[i], 0, 0);
i++;
forwards = true;
return;
}
visible_ctx.putImageData(image_datas[i], 0, 0);
forwards ? i++ : i--;
};
d3.timer(renderNext);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment