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.