Alphabet Array Golfed
l=[]
for(i=26;i--;)l[i]=(10+i).toString(36)
console.log(l)
A nasty golfed way to fill an array with letters a-z.
I usually do (as seen in another post):
let letters = 'abcdefghijklmopqrstuvwxyz'.split``
l=[]
for(i=26;i--;)l[i]=(10+i).toString(36)
console.log(l)
A nasty golfed way to fill an array with letters a-z.
I usually do (as seen in another post):
let letters = 'abcdefghijklmopqrstuvwxyz'.split``
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.”
document.addEventListener('touchmove', (e) => e.preventDefault(), {
passive: false,
});
// this is just lazy - obviously goes in your stylesheet.... :P
document.body.innerHTML += `
<style>
body, html {
padding: 0;
height: 100%;
margin: 0;
}
</style>
`;
const temp = document.createElement('div');
temp.innerHTML = `<svg width="100%" height="100%" viewBox="0 0 500 500"></svg>`;
const svg = document.body.appendChild(temp.querySelector('svg'));
const CIRCLE_NUM = 4;
// make some randomly positioned circles
for (let i = 0; i < CIRCLE_NUM; i++) {
const x = Math.random() * 150 + 150;
const y = Math.random() * 150 + 150;
svg.innerHTML += `<circle
cx="${x}" cy="${y}" r="60"
stroke="#000" fill="rgba(81, 121, 200, 0.3)"
style="cursor:pointer" />`;
}
function touch(e) {
const pt = svg.createSVGPoint();
pt.x = e.clientX;
pt.y = e.clientY;
return pt.matrixTransform(svg.getScreenCTM().inverse());
}
let down, ox, oy, curr;
document.addEventListener('pointerdown', (e) => {
down = true;
if (e.target.tagName === 'circle') {
curr = e.target;
const { x, y } = touch(e);
ox = curr.cx.baseVal.value - x;
oy = curr.cy.baseVal.value - y;
}
});
document.addEventListener('pointermove', (e) => {
if (down && curr) {
const { x, y } = touch(e);
curr.cx.baseVal.value = x + ox;
curr.cy.baseVal.value = y + oy;
}
});
document.addEventListener('pointerup', (e) => {
down = false;
curr = null;
});
Drag some svg circles on mobile and desktop…
const canvas = document.body.appendChild(
document.createElement('canvas')
);
const c = canvas.getContext('2d');
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
draw();
}
function draw() {
c.clearRect(0, 0, canvas.width, canvas.height);
Math.min(window.innerWidth, window.innerHeight) * 0.0015;
const iter = 100,
halfWidth = window.innerWidth / 2,
halfHeight = window.innerHeight / 2;
let rad = 50, rad2 = 50, theta = 0, x, y;
let x2, y2;
for (let i = 0; i < iter; i++) {
c.fillStyle = 'blue';
x = halfWidth + rad * Math.cos(theta);
y = halfHeight + rad * Math.sin(theta);
c.fillRect(x, y, 5, 5);
c.fillStyle = 'red';
rad2 = 80 + rad * Math.cos(theta * 3);
x2 = halfWidth + rad2 * Math.cos(theta);
y2 = halfHeight + rad2 * Math.sin(theta);
c.fillRect(x2, y2, 5, 5);
c.fillStyle = 'green';
c.fillRect((x2 + x) / 2, (y2 + y) / 2, 5, 5);
theta += .1;
}
}
resize();
window.addEventListener('resize', resize);
d = document
b = d.body
b.style.margin = 0
with(Math) {
S = min(innerHeight * 2, innerWidth * 2)
hs = S / 2
with(
b.appendChild(Object.assign(
d.createElement`canvas`, {
width: S,
height: S
})).getContext`2d`) {
// array of xy coords, closed boolean
function bezierSkin(bez, closed = true) {
const avg = calcAvgs(bez);
const leng = bez.length;
let i, n;
if (closed) {
moveTo(avg[0], avg[1]);
for (i = 2; i < leng; i += 2) {
n = i + 1;
quadraticCurveTo(bez[i], bez[n], avg[i], avg[n]);
}
quadraticCurveTo(bez[0], bez[1], avg[0], avg[1]);
} else {
moveTo(bez[0], bez[1]);
lineTo(avg[0], avg[1]);
for (i = 2; i < leng - 2; i += 2) {
n = i + 1;
quadraticCurveTo(bez[i], bez[n], avg[i], avg[n]);
}
lineTo(bez[leng - 2], bez[leng - 1]);
}
}
// create anchor points by averaging the control points
function calcAvgs(p) {
const avg = [];
const leng = p.length;
let prev;
for (var i = 2; i < leng; i++) {
prev = i - 2;
avg.push((p[prev] + p[i]) / 2);
}
// close
avg.push((p[0] + p[leng - 2]) / 2);
avg.push((p[1] + p[leng - 1]) / 2);
return avg;
}
canvas.style.transformOrigin = '0 0'
canvas.style.transform = 'scale(.4)'
rinit = _ => {
t = 0
tinc = .1
rad = hs * .8
pupil = random() * .25
radA = pupil + random() * .25
}
dx = dy = hs
cx = cy = hs
rinit()
fillStyle = 'black'
fillRect(0, 0, S, S);
fillStyle = 'white'
beginPath()
moveTo(hs, hs)
arc(hs, hs, rad, 0, 7)
fill()
outer = _ => {
dx = cx + rad * cos(t)
dy = cy + rad * sin(t)
if (t > 7 && random() < .3) {
fnIdx++
}
}
shutter = () => hs * radA + random() * hs * (.6 - pupil)
innerA = _ => {
tinc = .05
rad = shutter()
dx = cx + rad * cos(t)
dy = cy + rad * sin(t)
if (t > 21 && random() < .3) {
fnIdx++
}
}
oa = 7 * 3
innerB = _ => {
tinc = .05
T = t * random();
dx = cx + hs * radA * cos(T)
dy = cy + hs * radA * sin(T)
if (t > 28 + oa && random() < .3) {
rad = hs * .8
fnIdx++
}
}
outerA = _ => {
R = (rad - hs * .1) + random() * hs * .1;
dx = cx + R * cos(t)
dy = cy + R * sin(t)
if (t > 35 + oa && random() < .3) {
fnIdx++
}
}
outerB = _ => {
tinc = .01;
R = rad
if (random() < .5) R = shutter()
dx = cx + R * cos(t)
dy = cy + R * sin(t)
if (t > 42 + oa && random() < .3) {
fnIdx++
ct = t
}
}
t2 = 0
outerC = _ => {
tinc = .1;
t2 += .01;
R = hs * .3
RR = (R + t2 + random() * 10);
dx = cx + R * .84 + RR * cos(t)
dy = cy - R * .84 + RR * sin(t)
if (t > 70 + oa && random() < .3) {
fnIdx++
}
}
outerD = _ => {
tinc = .1;
t2 += .01;
R = hs * .1
RR = (R + t2 + random() * 10);
dx = cx + hs * .3 + RR * cos(t)
dy = cy + R * .84 + RR * sin(t)
if (t > 91 + oa && random() < .3) {
fnIdx++
}
}
outerE = _ => {
tinc = .1;
rad -= random() * .1;
dx = cx + rad * cos(t)
dy = cy + rad * sin(t)
if (t > 112 + oa && random() < .3) {
fnIdx++
}
}
count = 0
last = _ => {
done = true;
fillStyle = 'black'
fillRect(0, 0, S, S);
fillStyle = 'white'
beginPath()
moveTo(hs, hs)
arc(hs, hs, hs * .8, 0, 7)
fill()
beginPath();
moveTo(0, 0);
bezierSkin(pnts, false)
stroke()
return
count++
if (count < 1) {
rinit()
setOff()
t = 0
fnIdx = 0
}
}
fns = [outer, innerA, innerB, outerA, outerB, outerC, outerD, outerE, last]
fnIdx = 0
outer()
drawX = dx
drawY = dy
pDrawX = 0
pDrawY = 0
strokeStyle = 'rgba(0, 0, 0, 0.8)'
lineWidth = 1;
tt = 0
ox = 0;
oy = 0;
setOff = _ => {
return
ox = S * 1.2 * random() - S / 2
oy = S * 1.2 * random() - S / 2
sl = .1 + random() * .9;
}
sl = 1
pnts = []
done = false
loop = _ => {
if (done) {
return;
}
shadowColor = 'rgba(155, 255, 255, .5)';
shadowBlur = 15;
save()
scale(1, 1)
lineWidth = 2;
for (i = 0; i < 20; i++) {
t += tinc / 2
fns[fnIdx]()
drawX += ((dx + ox) * sl - drawX) / 2;
drawY += ((dy + oy) * sl - drawY) / 2;
if (drawX != 0 && pDrawX) {
beginPath()
moveTo(pDrawX, pDrawY);
lineTo(drawX, drawY);
pnts.push(drawX, drawY);
stroke()
}
pDrawX = drawX
pDrawY = drawY
}
restore()
requestAnimationFrame(loop)
}
loop()
}
}
Another thing for #genuary2022… a single curve…