A Pen By
Soós Tamás

JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.

Any URL's added here will be added as `<script>`

s in order, and run *before* the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.

If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.

You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.

+ add another resource

` ````
<html>
<head>
<meta charset="utf-8"/>
<title>Recamán</title>
<link rel="stylesheet" href="style.css"/>
<script src="recaman.js"></script>
</head>
<body>
<canvas id="canvas" width="512" height="512"></canvas>
</body>
</html>
```

` ````
body {
padding: 0;
background-color: black;
}
#canvas {
width: 512px;
height: 512px;
display: block;
margin: auto;
padding: 0;
}
```

` ````
let canvas, ctx;
let width, height;
let a = [];
let n, i;
let forward;
let s;
let c1, c2;
let l;
let g;
let t;
let start;
let fancy;
function setup() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
width = canvas.width;
height = canvas.height;
a[0] = 1;
n = 0;
i = 1;
s = 4;
l = 10000;
c1 = "#6599FF";
c2 = "#FF9900";
g = grid(width / s, s);
fancy = true;
start = performance.now();
requestAnimationFrame(loop);
}
function loop(time) {
//Sequence
if(n-i > 0 && !a[n-i]) {
n -= i;
forward = false;
} else {
n += i;
forward = true;
}
a[n] = 1;
i += 1;
t = time - start;
g.enable(n, forward);
g.update();
//Draw
if(fancy) {
ctx.fillStyle = "black";
ctx.globalAlpha = 1;
ctx.fillRect(0,0,width, height);
}
g.draw();
if(i < l)
requestAnimationFrame(loop);
}
function grid(n, s) {
function cell(x, y, s, type=-1) {
let o = 0;
let o1 = 0.8;
let o2 = 0.95;
let ts;
let ds = 750;
let dl = 60000;
function calcOpacity(o, f) {
let r = (64*o-32)/3*Math.pow(f,4)-(128*o-96)/3*Math.pow(f,3)+(92*o-94)/3*Math.pow(f,2) -(28*o-30)/3*f+o;
if(r < 0)
r = 0;
if(r > 1)
r = 1;
return r;
}
return {
enable: function(tp) {
type = tp;
ts = t;
ds += ts / 200;
if(o1 <= 1) {
o1 += 0.2;
}
},
update: function() {
if(type != -1) {
let f = (t-ts)/ds;
if(f < 1) {
if(type == 1)
o = calcOpacity(o1*Math.abs(Math.cos(ts/dl)), f);
else if(type == 0) {
o = calcOpacity(o2*Math.abs(Math.cos(ts/dl)), f);
}
}
}
},
draw: function() {
if(type == 1) {
ctx.fillStyle = c1;
if(fancy)
ctx.globalAlpha = o;
ctx.fillRect(x, y, s, s);
} else if(type == 0) {
ctx.fillStyle = c2;
if(fancy)
ctx.globalAlpha = o;
ctx.fillRect(x, y, s, s);
}
}
}
}
function psign(n) {
return 2 * Math.floor(2 * ((n / 2) % 1)) - 1;
}
function calcPos(n) {
let f = Math.floor(Math.sqrt(n));
let ps = psign(f);
let x = n - f*f;
let pos = {x: 0, y: 0};
//Squares
pos.x = ps * Math.floor((f - 1) / 2);
pos.y = ps * Math.ceil((f - 1) / 2);
//Offset
pos.x += -ps * (Math.floor((x - 0.5) / f)*Math.floor((x - 0.5) % f) - 1);
pos.y += ps * ((Math.abs(x - (f + 1)) - (f + 1)) - (Math.floor((x - 0.5) / f)*Math.floor((x - 0.5) % f) - 1));
return pos;
}
let cells = [];
for(let i = 0; i < n; i += 1) {
for(let j = 0; j < n; j += 1) {
cells.push(cell(j*s, i*s, s));
}
}
return {
enable: function(m, type) {
//Calculate position
let center = {x: n/2, y: n/2};
let pos = calcPos(m);
if(pos.x >= -n/2 && pos.x < n/2 && pos.y >= -n/2 && pos.y < n/2)
cells[(center.y + pos.y) * n + center.x + pos.x].enable(type);
},
update: function() {
cells.forEach((c) => c.update());
},
draw: function() {
cells.forEach((c) => c.draw());
}
}
}
window.onload = setup;
```

999px

Loading
..................

Alt F
Opt F
Find & Replace

Also see: Tab Triggers