console
class Particle {
constructor({
x = 0, y = 0,
tx = 0, ty = 0,
radius = 2, color = '#F00000'
}) {
this.x = x
this.y = y
this.tx = tx
this.ty = ty
this.radius = radius
this.color = color
}
draw(ctx) {
ctx.save()
ctx.translate(this.x, this.y)
ctx.fillStyle = this.color
ctx.beginPath()
ctx.arc(0, 0, this.radius, 0, (Math.PI * 2), true)
ctx.closePath()
ctx.fill()
ctx.restore()
return this
}
}
function drawFrame(particles, finished) {
const timer = window.requestAnimationFrame(() => {
drawFrame(particles, finished)
})
ctx.clearRect(0, 0, canvas.width, canvas.height)
const easing = 0.08
const finishedParticles = particles.filter(particle => {
const dx = particle.tx - particle.x
const dy = particle.ty - particle.y
let vx = dx * easing
let vy = dy * easing
if (Math.abs(dx) < 0.1 && Math.abs(dy) < 0.1) {
particle.finished = true
particle.x = particle.tx
particle.y = particle.ty
} else {
particle.x += vx
particle.y += vy
}
particle.draw(ctx)
return particle.finished
})
if (finishedParticles.length === particles.length) {
window.cancelAnimationFrame(timer)
finished && finished()
}
return particles
};
function getPixels(target, space = 5) {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
const viewWidth = window.innerWidth
const viewHeight = window.innerHeight
canvas.width = viewWidth
canvas.height = viewHeight
if (typeof target === 'string') {
ctx.font = '150px bold'
ctx.fillStyle = '#fff'
ctx.textBaseline = 'middle'
ctx.textAlign = 'center'
ctx.fillText(target, viewWidth / 2, (viewHeight) / 2)
} else {
ctx.drawImage(target, (viewWidth - target.width) / 2, (viewHeight - target.height) / 2, target.width, target.height)
}
const { data, width, height } = ctx.getImageData(0, 0, viewWidth, viewHeight)
const pixeles = []
for (let x = 0; x < width; x += space) {
for (let y = 0; y < height; y += space) {
const pos = (y * width + x) * 4
if (data[pos + 3] > 128) {
pixeles.push({ x, y, rgba: [data[pos], data[pos + 1], data[pos + 2], data[pos + 3]] })
}
}
}
return pixeles
}
function createParticles({ text, radius, space }) {
const pixeles = getPixels(text, space)
return pixeles.map(({ x, y, rgba: color }) => {
return new Particle({
x: Math.random() * (50 + window.innerWidth) - 50,
y: Math.random() * (50 + window.innerHeight) - 50,
tx: x, ty: y,
radius, color: `rgba(${color})`
})
})
}
function loop(targets, i = 0) {
return drawFrame(createParticles({ text: targets[i], radius: 2, space: 5 }), () => {
i++
if (i < targets.length) {
loop(targets, i)
}
})
}
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
loop(["\uD83D\uDC27", "\uD83D\uDC1F", "\uD83D\uDC0F", "\uD83D\uDC07", "\uD83D\uDC24", "\uD83D\uDC30", "\uD83D\uDC0D", "\uD83E\uDD8B", "\uD83D\uDC08", "\uD83D\uDC0C", "\uD83D\uDC33"])
<canvas id="canvas"></canvas>
body, html {
background-color: #000;
width: 100%;
height: 100%;
overflow: hidden;
margin: 0;
}