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.”
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 = 300,
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 < 100; 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
S = 300
with(
b.appendChild(Object.assign(
d.createElement`canvas`, { width: S, height: S })
).getContext`2d`) {
seed = Math.random();
rando = () => {
seed = seed * 16807 % 0x7fffffff
return (seed - 1) / 0x7ffffffe
}
squares = {}
rot = (
cx, cy, X, Y, ang,
cos = Math.cos(-ang),
sin = Math.sin(-ang),
x = cos * (X - cx) + sin * (Y - cy) + cx,
y = cos * (Y - cy) - sin * (X - cx) + cy) => ({ x, y })
inc = 0
breaks = 0
delay = 0
ticks = 0
square = (x = S / 2, y = S / 2,
size = S * .7,
r = 0,
rad = .3,
hSize = size / 2,
t = rando() * 7,
vx = rando() * rad * Math.cos(t),
vy = rando() * rad * Math.sin(t),
vr = rando() * .02 - .01,
i, j,
o = {
id:inc++,
sr(R) {
r = R
},
divide(num = 2, cSize = size / num,
chSize = cSize / 2,
sx = x - hSize + chSize,
sy = y - hSize + chSize) {
save()
translate(-size * .1, -size * .1)
scale(1.1, 1.1)
o.render('rgba(255, 255, 255, .7)')
restore()
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
l = rot(x, y, sx + i * cSize, sy + j * cSize, r)
square(l.x, l.y, cSize, r, rad + 2)
}
}
},
render(fill = '#000') {
save()
translate(x, y)
rotate(r)
fillStyle = fill
strokeStyle = '#fff'
lineWidth = 2
fillRect(-hSize,-hSize, size, size)
strokeRect(-hSize,-hSize, size, size)
restore()
},
draw() {
x += vx
y += vy
r += o.id === 0 ? .01 : vr
o.render()
if (rando() < .05 && breaks < 10 &&
delay > 30 && ticks > 200) {
o.divide(~~(rando() * 3) + 2)
delete squares[o.id]
breaks++
delay = 0
}
}
}) => (squares[o.id] = o)
square()
C = Object.assign(
d.createElement`canvas`, { width: S, height: S }
).getContext`2d`
fillStyle = 'rgba(82, 82, 82, 1)'
fillRect(0, 0, S, S)
loop = _ => {
ticks++
fillStyle = 'rgba(82, 82, 82, 0.1)'
fillRect(0, 0, S, S)
globalAlpha = .4
drawImage(C.canvas, -1, 5, S + 1, S + 5)
globalAlpha = 1
save()
translate(20, 50)
scale(.9, .7)
for (i in squares) {
squares[i].draw()
}
restore()
delay++
C.drawImage(canvas, 0, 0)
requestAnimationFrame(loop)
}
loop()
}
Another thing for #genuary2022… destroy a square
D = document
B = D.body
S = 1200
with(
B.appendChild(Object.assign(
D.createElement`canvas`, {
width: S,
height: S
})).getContext`2d`) {
canvas.style.transformOrigin = '0 0'
canvas.style.transform = 'scale(.5)'
b = .9998
xn1 = 5
yn1 = 0
xn = yn = 0
scale = 20
iterations = 1000
f = x => {
x2 = x * x
return a * x + (2 * (1 - a) * x2) / (1 + x2)
}
mouseX = mouseY = 0
onpointermove = e => {
mouseX = e.clientX
mouseY = e.clientY
}
dwn = 0
fillStyle = '#000'
fillRect(0, 0, S, S)
loop = _ => {
a = mouseY / 1000
xn1 = mouseX / 30
yn1 = 0
dwn += 1
for (var i = 0; i < iterations; i++) {
xn = xn1;
yn = yn1;
fillStyle = `hsla(${i / 30}, ${dwn / 6}%, 50%, .2)`
xn1 = b * yn + f(xn)
yn1 = -xn + f(xn1)
fillRect(S * .5 + xn1 * scale, yn1 * scale / 2 + dwn, 1, 1)
}
requestAnimationFrame(loop)
}
loop()
}
Move your around as the Gumowski / Mira shape moves down the screen…