Skip to content

Instantly share code, notes, and snippets.

@pbeshai
Last active January 25, 2019 21:35
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pbeshai/da904ef5fd7f451e04e3b04568fef270 to your computer and use it in GitHub Desktop.
Line Circle Illusion
license: mit
height: 540
border: no
function colorScale(e){return rawColorScale(colorScaleMapper(e))}function createCircles(){for(var e=[],t=0;t<=maxCirclePower;t++)e=e.concat(createCirclePower(t));return e}function createCirclePower(e){var t=Math.pow(2,e),r=Math.pow(2,Math.max(0,e-1)),a=d3.range(r).map(function(e){var r=angleScale((2*e+1)/t);return createCircle(r)});return a}function createCircle(e){var t=.5,r=positionScale(e),a=t*Math.cos(e)+t,o=t*Math.sin(e)+t,c=t*Math.cos(e+Math.PI)+t,i=t*Math.sin(e+Math.PI)+t,l=d3.easeQuad(r),n=(1-l)*a+l*c,s=(1-l)*o+l*i;return{tPosition:r,direction:1,color:3*r,x:n,y:s,sx:a,sy:o,tx:c,ty:i}}function draw(e){ctx.save();for(var t,r=0;r<e.length&&r<numCirclesToShow;++r)t=e[r],ctx.beginPath(),ctx.arc(xScale(t.x),yScale(t.y),radius,0,tau),ctx.fillStyle=colorScale(t.color),ctx.fill(),ctx.closePath();ctx.restore()}function update(e){for(var t,r=0;r<e.length;++r){t=e[r],t.tPosition=Math.max(0,Math.min(t.tPosition+tickAmount*t.direction,1)),1!==t.tPosition&&0!==t.tPosition||(t.direction*=-1);var a=d3.easeQuad(t.tPosition);t.x=(1-a)*t.sx+a*t.tx,t.y=(1-a)*t.sy+a*t.ty,t.color=t.color+tickAmount,t.color+1e-6>3&&(t.color=0)}}function updateConfig(){radius=+d3.select("#radius").node().value,plotAreaWidth=width-2*radius,plotAreaHeight=height-2*radius,xScale.range([0,plotAreaWidth]),yScale.range([0,plotAreaHeight]),tickAmount=+d3.select("#tickAmount").node().value,colorInterpolator=d3[d3.select("#colorInterpolator").node().value],rawColorScale.interpolator(colorInterpolator),ctx.restore(),ctx.save(),ctx.translate(radius,radius),ctx.clearRect(-radius,-radius,width,height)}var tau=2*Math.PI,width=400,height=400,radius=10,plotAreaWidth=width-2*radius,plotAreaHeight=height-2*radius,tickAmount=.01,maxCirclePower=9,circlePower=0,numCirclesToShow=Math.pow(2,circlePower),numLoopsAtMax=4,resetCountdown=numLoopsAtMax,colorInterpolator=d3.interpolateRainbow,screenScale=window.devicePixelRatio||1,canvas=d3.select("canvas").attr("width",width*screenScale).attr("height",height*screenScale).style("width",width+"px").style("height",height+"px"),ctx=canvas.node().getContext("2d");ctx.scale(screenScale,screenScale),ctx.save(),ctx.translate(radius,radius);var xScale=d3.scaleLinear().domain([0,1]).range([0,plotAreaWidth]),yScale=d3.scaleLinear().domain([0,1]).range([0,plotAreaHeight]),angleScale=d3.scaleLinear().domain([0,1]).range([0,Math.PI]),positionScale=d3.scaleLinear().domain([0,Math.PI]).range([1,0]),colorScaleMapper=d3.scaleLinear().domain([0,1,1.5,2,3]).range([0,.6666666667,1,.666666667,0]),rawColorScale=d3.scaleSequential(colorInterpolator),circles=createCircles();draw(circles);var timer=d3.timer(function(e){update(circles),draw(circles),0===circles[0].tPosition&&(circlePower<maxCirclePower?(circlePower+=1,numCirclesToShow=Math.pow(2,circlePower)):0===resetCountdown?(circlePower=0,numCirclesToShow=Math.pow(2,circlePower),resetCountdown=numLoopsAtMax,ctx.clearRect(-radius,-radius,width,height)):resetCountdown-=1,console.log("Showing "+numCirclesToShow+" circles. Reset Countdown = "+resetCountdown))});d3.select(".controls").selectAll("input, select").on("change",updateConfig);
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyJdLCJuYW1lcyI6WyJjb2xvclNjYWxlIiwidCIsInJhd0NvbG9yU2NhbGUiLCJjb2xvclNjYWxlTWFwcGVyIiwiY3JlYXRlQ2lyY2xlcyIsImxldCIsImNpcmNsZXMiLCJpIiwibWF4Q2lyY2xlUG93ZXIiLC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zAsIHBsb3RBcmVhV2lkdGhdKTtcbiAgeVNjYWxlLnJhbmdlKFswLCBwbG90QXJlYUhlaWdodF0pO1xuXG5cdHRpY2tBbW91bnQgPSArZDMuc2VsZWN0KCcjdGlja0Ftb3VudCcpLm5vZGUoKS52YWx1ZTtcblx0Y29sb3JJbnRlcnBvbGF0b3IgPSBkM1tkMy5zZWxlY3QoJyNjb2xvckludGVycG9sYXRvcicpLm5vZGUoKS52YWx1ZV07XG5cdHJhd0NvbG9yU2NhbGUuaW50ZXJwb2xhdG9yKGNvbG9ySW50ZXJwb2xhdG9yKTtcblxuXHRjdHgucmVzdG9yZSgpO1xuXHRjdHguc2F2ZSgpO1xuXHRjdHgudHJhbnNsYXRlKHJhZGl1cywgcmFkaXVzKTtcblx0Y3R4LmNsZWFyUmVjdCgtcmFkaXVzLCAtcmFkaXVzLCB3aWR0aCwgaGVpZ2h0KTtcbn1cblxuZDMuc2VsZWN0KCcuY29udHJvbHMnKS5zZWxlY3RBbGwoJ2lucHV0LCBzZWxlY3QnKVxuXHQub24oJ2NoYW5nZScsIHVwZGF0ZUNvbmZpZyk7XG5cblxuIl19
<!DOCTYPE html>
<title>Line Circle Illusion</title>
<style>
body {
padding: 30px;
font-family: sans-serif;
}
.controls {
margin-top: 30px;
}
label {
display: inline-block;
font-size: 12px;
text-transform: uppercase;
font-weight: 600;
margin-right: 25px;
}
input, select {
display: block;
margin-top: 3px;
}
input {
width: 50px;
}
</style>
<body>
<canvas></canvas>
<div class="controls">
<label>
Radius
<input type="number" id="radius" value="10" minValue="1" maxValue="100" step="1" />
</label>
<label>
Tick Amount
<input type="number" id="tickAmount" value="0.01" minValue="0.01" maxValue="0.2" step="0.01" />
</label>
<label>
Color
<select id="colorInterpolator">
<option value="interpolateRainbow">Rainbow</option>
<option value="interpolateMagma">Magma</option>
<option value="interpolateInferno">Inferno</option>
<option value="interpolateViridis">Viridis</option>
<option value="interpolateWarm">Warm</option>
<option value="interpolateCool">Cool</option>
</select>
</label>
</div>
<script src='https://d3js.org/d3.v4.min.js'></script>
<script src='dist.js'></script>
</body>
const tau = Math.PI * 2;
const width = 400;
const height = 400;
let radius = 10;
let plotAreaWidth = width - 2 * radius;
let plotAreaHeight = height - 2 * radius;
// corresponds to speed of circles. can be fun to play with.
let tickAmount = 0.01;
// we get 2^power number of circles drawn
const maxCirclePower = 9;
let circlePower = 0;
let numCirclesToShow = Math.pow(2, circlePower);
// after we have reached max power, how many loops
// before resetting to 1 circle?
const numLoopsAtMax = 4;
let resetCountdown = numLoopsAtMax;
// set the color scheme here:
let colorInterpolator = d3.interpolateRainbow;
// create the canvas
const screenScale = window.devicePixelRatio || 1;
const canvas = d3.select('canvas')
.attr('width', width * screenScale)
.attr('height', height * screenScale)
.style('width', `${width}px`)
.style('height', `${height}px`)
const ctx = canvas.node().getContext('2d');
ctx.scale(screenScale, screenScale);
ctx.save();
ctx.translate(radius, radius);
// position scales
const xScale = d3.scaleLinear().domain([0, 1]).range([0, plotAreaWidth]);
const yScale = d3.scaleLinear().domain([0, 1]).range([0, plotAreaHeight]);
const angleScale = d3.scaleLinear().domain([0, 1]).range([0, Math.PI]);
const positionScale = d3.scaleLinear().domain([0, Math.PI]).range([1, 0])
// colors-- we want to repeat every 3 trips while looping from one end
// to the other and back to the beginning.
const colorScaleMapper = d3.scaleLinear()
.domain([0, 1, 1.5, 2, 3])
.range([0, 0.6666666667, 1, 0.666666667, 0])
// t goes from 0 to 2
const rawColorScale = d3.scaleSequential(colorInterpolator);
function colorScale(t) {
return rawColorScale(colorScaleMapper(t))
}
// create circles by adding in levels based on powers of 2
function createCircles() {
let circles = [];
for (let i = 0; i <= maxCirclePower; i++) {
circles = circles.concat(createCirclePower(i));
}
return circles;
}
/*
create circles for a given power of 2.
e.g.:
power=0 1,
power=1 1/2,
power=2 1/4 3/4
power=3 1/8 3/8 5/8 7/8
power=4 1/16 3/16 5/16 7/16 9/16 11/16
*/
function createCirclePower(power) {
const denominator = Math.pow(2, power);
const numCirclesToAdd = Math.pow(2, Math.max(0, power - 1))
const circleRing = d3.range(numCirclesToAdd).map((i) => {
const angle = angleScale(((2 * i) + 1) / denominator);
return createCircle(angle);
});
return circleRing;
}
// create a single circle at a given angle
function createCircle(angle) {
const baseRadius = 0.5;
const tPosition = positionScale(angle);
// use polar coordinates
const sx = baseRadius * Math.cos(angle) + baseRadius;
const sy = baseRadius * Math.sin(angle) + baseRadius;
const tx = baseRadius * Math.cos(angle + Math.PI) + baseRadius;
const ty = baseRadius * Math.sin(angle + Math.PI) + baseRadius;
// linear interpolate with quadratic easing (easing should match update func)
const t = d3.easeQuad(tPosition);
const x = (1 - t) * sx + t * tx;
const y = (1 - t) * sy + t * ty;
return {
tPosition,
direction: 1,
color: tPosition * 3,
x,
y,
sx,
sy,
tx,
ty,
};
}
// draw circles on screen
function draw(circles) {
ctx.save();
let circle;
for (let i = 0; i < circles.length && i < numCirclesToShow; ++i) {
circle = circles[i];
ctx.beginPath();
ctx.arc(xScale(circle.x), yScale(circle.y), radius, 0, tau)
ctx.fillStyle = colorScale(circle.color);
ctx.fill()
ctx.closePath();
}
ctx.restore();
}
// update circle positions
function update(circles) {
let circle;
for (let i = 0; i < circles.length; ++i) {
circle = circles[i];
// update position
circle.tPosition = Math.max(0, Math.min(circle.tPosition + tickAmount * circle.direction, 1));
if (circle.tPosition === 1 || circle.tPosition === 0) {
circle.direction *= -1;
}
// important to use quadratic easing to get the circle illusion shape
const t = d3.easeQuad(circle.tPosition);
circle.x = (1 - t) * circle.sx + t * circle.tx;
circle.y = (1 - t) * circle.sy + t * circle.ty;
// update color
circle.color = circle.color + tickAmount;
// fix rounding error
if (circle.color + 1e-6 > 3) {
circle.color = 0;
}
}
}
// create the circles and draw them
const circles = createCircles();
draw(circles);
// begin a timer for doubling the number of circles at regular intervals
let timer = d3.timer((elapsed) => {
update(circles);
draw(circles);
// if the first circle is back in start position, add a new ring
if (circles[0].tPosition === 0) {
if (circlePower < maxCirclePower) {
circlePower += 1;
numCirclesToShow = Math.pow(2, circlePower);
} else if (resetCountdown === 0) {
circlePower = 0;
numCirclesToShow = Math.pow(2, circlePower);
resetCountdown = numLoopsAtMax;
ctx.clearRect(-radius, -radius, width, height);
} else {
resetCountdown -= 1;
}
console.log(`Showing ${numCirclesToShow} circles. Reset Countdown = ${resetCountdown}`);
}
});
// add controls
function updateConfig() {
radius = +d3.select('#radius').node().value;
plotAreaWidth = width - 2 * radius;
plotAreaHeight = height - 2 * radius;
xScale.range([0, plotAreaWidth]);
yScale.range([0, plotAreaHeight]);
tickAmount = +d3.select('#tickAmount').node().value;
colorInterpolator = d3[d3.select('#colorInterpolator').node().value];
rawColorScale.interpolator(colorInterpolator);
ctx.restore();
ctx.save();
ctx.translate(radius, radius);
ctx.clearRect(-radius, -radius, width, height);
}
d3.select('.controls').selectAll('input, select')
.on('change', updateConfig);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment