Skip to content

Commit

Permalink
First commit of flow field project
Browse files Browse the repository at this point in the history
  • Loading branch information
andwin committed Feb 19, 2024
1 parent 8263e46 commit 1bd9db3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<script lang="ts">
const demos = [
{
title: 'Flow field',
link: './src/flowfield/',
},
{
title: 'Rektron logo',
link: './src/rektron/',
Expand Down
16 changes: 16 additions & 0 deletions src/flowfield/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<title>Flow field</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<style>
body {
padding: 0;
margin: 0;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
<script src="./index.js"></script>
</head>
<body></body>
</html>
52 changes: 52 additions & 0 deletions src/flowfield/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable no-undef */
const particles = []
const noiseScale = 0.01
const speed = 4
let bgColor

function setup() {
bgColor = color('#1A1B3E')
bgColor.setAlpha(40)

createCanvas(window.innerWidth, window.innerHeight)

particles.length = 0
const numberOfParticles = Math.min(height, width)
for (let i = 0; i < numberOfParticles; i++) {
const particle = {}
initParticle(particle)
particles.push(particle)
}
}
window.onresize = setup

function draw() {
background(bgColor)
strokeWeight(3)
stroke(230)

for (const particle of particles) {
drawParticle(particle)
moveParticle(particle)
if (isOffscreen(particle)) initParticle(particle)
}
}

const initParticle = (particle) => {
particle.position = createVector(random(width), random(height))
}

const drawParticle = (particle) => {
point(particle.position.x, particle.position.y)
}

const moveParticle = (particle) => {
const time = frameCount * 0.01
const noiseValue = noise(particle.position.x * noiseScale, particle.position.y * noiseScale, time)

const angle = 2 * PI * noiseValue
const v = p5.Vector.fromAngle(angle).setMag(speed)
particle.position.add(v)
}

const isOffscreen = particle => particle.position.x <= 0 || particle.position.x >= width || particle.position.y <= 0 || particle.position.y >= height
Binary file added src/flowfield/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1bd9db3

Please sign in to comment.