Draw ripples on mousemove as well

This commit is contained in:
arne 2024-01-27 09:47:37 +01:00
commit 398aa43338
9 changed files with 36 additions and 11 deletions

24
client/.gitignore vendored Normal file
View file

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

BIN
client/bun.lockb Executable file

Binary file not shown.

13
client/index.html Normal file
View file

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ripples | compost.party</title>
<link rel="stylesheet" href="/src/style.css" type="text/css" media="screen" />
</head>
<body>
<canvas id="canvas"></canvas>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

16
client/package.json Normal file
View file

@ -0,0 +1,16 @@
{
"name": "ripples",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "^5.2.2",
"typescript-language-server": "^4.3.1",
"vite": "^5.0.8"
}
}

1
client/public/vite.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

98
client/src/main.ts Normal file
View file

@ -0,0 +1,98 @@
const canvas = document.querySelector<HTMLCanvasElement>('#canvas')!
const ctx = canvas.getContext('2d')!
const MIN_RADIUS = 20
const MAX_RADIUS = 200
const MAX_AGE = 1000 // in milliseconds
type Point = [number, number]
type Particle = {
position: Point
age: number
maxRadius: number
}
const createParticle = (position: [number, number], maxRadius: number): Particle => ({
position,
maxRadius,
age: 0,
})
let state = {
particles: <Particle[]>[]
}
type State = typeof state
canvas.addEventListener('mousedown', (e) => {
// TODO Normalize x and y coords
const particle = createParticle([e.clientX, e.clientY], MAX_RADIUS)
state.particles.push(particle)
})
function* minDist(dist: number) {
let prev: Point
while (true) {
yield (cur: Point) => {
if (prev == null || Math.sqrt(Math.pow(prev[0]-cur[0], 2) + Math.pow(prev[1]-cur[1], 2)) >= dist) {
prev = cur
return true
}
return false
}
}
}
const hasSpace = minDist(70)
canvas.addEventListener('mousemove', (e) => {
const shouldSpawn = hasSpace.next().value!
if (shouldSpawn([e.clientX, e.clientY])) {
const particle = createParticle([e.clientX, e.clientY], MAX_RADIUS / 3*2)
state.particles.push(particle)
}
})
const update = (state: State, deltaTime: number): State => ({
particles: state.particles
.map(p => ({
...p,
age: p.age + deltaTime
}))
.filter(p => p.age < MAX_AGE)
})
const render = (state: State, ctx: CanvasRenderingContext2D) => {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
for (const particle of state.particles) {
const progress = (particle.age / MAX_AGE)
const opacity = Math.min(1, Math.pow(1-progress, 3)) * (particle.maxRadius / MAX_RADIUS)
ctx.beginPath()
ctx.strokeStyle = `rgba(40, 40, 40, ${opacity})`
const [x, y] = particle.position
const radius = (progress * (particle.maxRadius - MIN_RADIUS)) + MIN_RADIUS
ctx.ellipse(x, y, radius, radius, 0, 0, Math.PI * 2)
ctx.stroke()
}
}
let before = Date.now()
const loop = () => {
const now = Date.now()
const dt = now - before
state = update(state, dt)
render(state, ctx)
before = now
requestAnimationFrame(loop)
}
document.addEventListener('DOMContentLoaded', () => {
canvas.width = canvas.parentElement!.clientWidth
canvas.height = canvas.parentElement!.clientHeight
ctx.translate(0.5, 0.5)
loop()
})

10
client/src/style.css Normal file
View file

@ -0,0 +1,10 @@
html,
body {
min-width: 100wh;
min-height: 100vh;
padding: 0;
margin: 0;
}
canvas {
}

1
client/src/vite-env.d.ts vendored Normal file
View file

@ -0,0 +1 @@
/// <reference types="vite/client" />

23
client/tsconfig.json Normal file
View file

@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}