Fake RNG
let anchors
let idx
let leng = 10
let size = 200
let px = 0
let py = 0
function seed() {
idx = 0
anchors = (Date.now() + '').split``
.reverse()
.map(v => parseFloat(v) / 10)
.splice(0, leng)
}
let last = 0
let zoom = 1
function rand() {
if (idx > size * size) seed()
px += zoom
py += ~~(px / size)
if (px >= size) px = 0
if (py >= size) py = 0
const point = {
x: anchors[idx % leng],
y: anchors[(idx + 1) % leng]
}
idx++
let dists = []
for (let i = 0; i < anchors.length; i += 2) {
let dx = px - anchors[i] * size
let dy = py - anchors[i + 1] * size
dists.push(Math.sqrt(dx * dx + dy * dy))
}
dists.sort()
last += (dists[0] / size - last) / 4
return last
}
seed()
let d = document
let b = d.body
with (b.appendChild(
Object.assign(d.createElement`canvas`, { width: 400, height: 400 })
).getContext`2d`) {
fillStyle = 'black'
fillRect(0, 0, 400, 400)
for (let i = 0; i < 200; i++) {
for (let j = 0; j < 200; j++) {
const c = rand() * 255
fillStyle = `rgb(${c}, ${c}, ${c})`
fillRect(j * 2, i * 2, 1, 2)
}
}
}
Another one for genuary “Create your own pseudo-random number generator and visually check the results.”
Raphaël Easing Equations
var ef = R.easing_formulas = {
linear: function (n) {
return n;
},
"<": function (n) {
return pow(n, 1.7);
},
">": function (n) {
return pow(n, .48);
},
"<>": function (n) {
var q = .48 - n / 1.04,
Q = math.sqrt(.1734 + q * q),
x = Q - q,
X = pow(abs(x), 1 / 3) * (x < 0 ? -1 : 1),
y = -Q - q,
Y = pow(abs(y), 1 / 3) * (y < 0 ? -1 : 1),
t = X + Y + .5;
return (1 - t) * 3 * t * t + t * t * t;
},
backIn: function (n) {
var s = 1.70158;
return n * n * ((s + 1) * n - s);
},
backOut: function (n) {
n = n - 1;
var s = 1.70158;
return n * n * ((s + 1) * n + s) + 1;
},
elastic: function (n) {
if (n == !!n) {
return n;
}
return pow(2, -10 * n) * math.sin((n - .075) * (2 * PI) / .3) + 1;
},
bounce: function (n) {
var s = 7.5625,
p = 2.75,
l;
if (n < (1 / p)) {
l = s * n * n;
} else {
if (n < (2 / p)) {
n -= (1.5 / p);
l = s * n * n + .75;
} else {
if (n < (2.5 / p)) {
n -= (2.25 / p);
l = s * n * n + .9375;
} else {
n -= (2.625 / p);
l = s * n * n + .984375;
}
}
}
return l;
}
};
ef.easeIn = ef["ease-in"] = ef["<"];
ef.easeOut = ef["ease-out"] = ef[">"];
ef.easeInOut = ef["ease-in-out"] = ef["<>"];
ef["back-in"] = ef.backIn;
ef["back-out"] = ef.backOut;
Another fun chunk of code directly from the Raphaël source. Makes me think of the Penner easing equations.
HSL to RGB JavaScript
const hsl2rgb = (h, s, l, o) => {
if (h > 1 || s > 1 || l > 1) {
h /= 360;
s /= 100;
l /= 100;
}
h *= 360;
let R, G, B, X, C;
h = (h % 360) / 60;
C = 2 * s * (l < .5 ? l : 1 - l);
X = C * (1 - Math.abs(h % 2 - 1));
R = G = B = l - C / 2;
h = ~~h;
R += [C, X, 0, 0, X, C][h];
G += [X, C, C, X, 0, 0][h];
B += [0, 0, X, C, C, X][h];
return `rgba(${~~(R * 255)}, ${~~(G * 255)}, ${~~(B * 255)}, ${o})`;
};
console.log(hsl2rgb(122, 50, 50, .5));
Taken from the Raphaël source… Always fun to browse – I’ve learned lots of great stuff from it 😀
Polar Forking Tweak
const FOUR_PI = 6 * Math.PI;
const { cos, sin } = Math;
const canvas = document.body.appendChild(
document.createElement('canvas')
);
const c = canvas.getContext('2d');
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
let inc = 0;
function draw() {
c.fillStyle = 'rgba(0, 0, 0, .3)'
c.fillRect(0, 0, canvas.width, canvas.height)
c.fillStyle = 'white';
const halfWidth = window.innerWidth / 2;
const halfHeight = window.innerHeight / 2;
let theta = 0,
a = 20 * Math.min(window.innerWidth, window.innerHeight) * 0.005,
x,
y;
c.save();
c.translate(halfWidth, halfHeight)
let b = 5 * cos(inc);
inc += .02;
for (let i = 0; theta < FOUR_PI; i++) {
let rad = a * (b + 10 * sin(theta / 3));
// randomly speed-coded and tweaked... leaving as is :D
x = rad * cos(theta + b / 10) * cos(b / 10 +theta * 2) * cos(theta * 2);
y = rad * sin(theta * 2) * cos(theta + b / 3) * cos(theta * 2);
c.fillRect(x,y, 2, 2);
theta += 0.04;
}
c.restore();
requestAnimationFrame(draw)
}
resize();
addEventListener('resize', resize);
draw();
Just randomly futzing with sin/cos…