Animate to Target
function box(x, y, col = 'red', cursor){ const box = document.body.appendChild(
document.createElement`div`
)
box.classList.add(col + '-box')
Object.assign(box.style, { position: 'absolute',
left: `${x}%`, top: `${y}%`, width: '30px',
height: '30px',
background: col,
cursor: cursor || 'pointer',
color: 'white'
})
return box
}
const NUM = 10;
for (let i = 0; i < NUM; i++)
box(
Math.random() * 100,
Math.random() * 100)
let destX = destY = x = y = 0;
const blue = box(destX, destY, 'blue', 'default')
const info = box(0, 30, 'gray')
info.innerHTML = 'click the red boxes'
Object.assign(info.style, { width: '100%',
padding: '.5em',
fontFamily: 'sans-serif'
})
document.addEventListener('click', e => { const curr = e.target
if (curr.classList.contains('red-box')) { destX = curr.offsetLeft
destY = curr.offsetTop
curr.style.background = 'black'
setTimeout(() => curr.style.background = 'red', 700)
if (info.parentNode != null) { info.parentNode.removeChild(info);
}
}
})
function loop() { x += (destX - x) / 12
y += (destY - y) / 12
blue.style.transform = `translate3d(${x}px, ${y}px, 0)` requestAnimationFrame(loop)
}
loop()
Click a red box, watch the blue box animate…
Oscillating Canvas Wave
const c = document.body
.appendChild(document.createElement('canvas')) .getContext('2d');
Object.assign(document.body.style, { margin: 0,
height: '100%'
});
Object.assign(c.canvas.style, { position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: '100%'
});
let t = 0;
function resize() { c.canvas.width = innerWidth * 2;
c.canvas.height = innerHeight * 2;
draw();
}
window.addEventListener('resize', resize);resize();
function draw() { const { canvas: { width, height } } = c;
c.fillStyle = 'rgba(155, 155, 155, .4)';
c.fillRect(0, 0, width, height);
c.fillStyle = '#000';
let x = innerWidth;
let y = 0;
t += 0.05;
for (let i = 0; i < innerHeight; i++) { x += 2 * Math.cos(t + i * 0.1 * Math.cos(i / 200));
y += 2;
c.fillRect(x, y, 100, 1);
}
}
function loop() { draw();
window.requestAnimationFrame(loop);
}
loop();
Speed coded oscillating wave on canvas… Looks better in fullscreen.
Gravity Dots 2
d = document
b = d.body
b.style.margin = 0
b.style.background = 'black'
r = (v = 1) => Math.random() * v
with(
b.appendChild(
d.createElement`canvas`
).getContext`2d`) {
onresize = () => { canvas.width = innerWidth
canvas.height = innerHeight
}
onresize()
fillStyle = 'red'
dot = (
x = r(innerWidth),
y = 0,
mass = 20, sr = r(2) + 2,
R = sr,
dx, dy,
col = '#005eb0',
dist, vx = .2, vy = 0) => {
return () => { fillStyle = col
R = sr
if (r() < .001) { vx = r() * 4 - 2
R = sr * 2
fillStyle = 'red'
col = `hsl(${r(360)}deg, 50%, 50%)` }
dx = innerWidth / 2 - x
dy = innerHeight / 2 - y
dist = Math.sqrt(dx * dx + dy * dy)
dist *= dist
vx += dx / dist * mass
vy += dy / dist * mass
x += vx
y += vy
beginPath()
arc(x, y, R, 0, 6.29)
fill()
}
}
const dots = []
for (let i = 0; i < 100; i++) dots.push(dot())
loop = () => { fillStyle = 'rgba(0, 0, 0, 0.05)'
fillRect(0, 0, canvas.width, canvas.height)
dots.map(d => d())
}
setInterval(loop, 16)
}
A variation on this recent post.
Gravity Dots
d = document
b = d.body
b.style.margin = 0
b.style.background = 'black'
r = (v = 1) => Math.random() * v
with(
b.appendChild(
d.createElement`canvas`
).getContext`2d`) {
onresize = () => { canvas.width = innerWidth
canvas.height = innerHeight
}
onresize()
fillStyle = 'red'
dot = (
x = r(innerWidth),
y = r(innerHeight),
mass = 10, sr = r(8) + 4,
R = sr,
dx, dy,
dist, vx = .2, vy = 0) => {
return () => { fillStyle = '#005eb0'
R = sr
if (r() < .005) { vx = r() * 4 - 2
R = sr * 2
fillStyle = 'white'
}
dx = innerWidth / 2 - x
dy = innerHeight / 2 - y
dist = Math.sqrt(dx * dx + dy * dy)
dist *= dist
vx += dx / dist * mass
vy += dy / dist * mass
x += vx
y += vy
beginPath()
arc(x, y, R, 0, 6.29)
fill()
}
}
const dots = []
for (let i = 0; i < 10; i++) dots.push(dot())
loop = () => { fillStyle = 'rgba(0, 0, 0, 0.2)'
fillRect(0, 0, canvas.width, canvas.height)
dots.map(d => d())
}
setInterval(loop, 16)
}
Some speed coded dots that gravitate to the center of the screen and occasionally change direction.