Skip to content

Commit

Permalink
Update p5 sketch to use react wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
aleannab committed Dec 9, 2023
1 parent edad356 commit 389b4d3
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 169 deletions.
4 changes: 1 addition & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
<title>Antoinette Bumatay-Chan's Portfolio</title>
<link href="https://fonts.googleapis.com/css2?family=Raleway:wght@300;400;500;700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Comfortaa:wght@300&display=swap" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

<script type="text/javascript" src="src/sketch.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script> -->
</head>
<body>
<div id="root"></div>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"deploy": "vite build && gh-pages -d dist"
},
"dependencies": {
"@p5-wrapper/react": "^4.3.2",
"@react-spring/parallax": "^9.7.3",
"@react-spring/web": "^9.7.3",
"cssnano": "^6.0.1",
Expand Down
143 changes: 143 additions & 0 deletions src/components/BgSketch.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import * as React from 'react';
import { ReactP5Wrapper } from '@p5-wrapper/react';

function sketch(p5) {
let dots = [];
let numDots = 7;
let timeWarp = 0.0005;

// size variables
let pLength;
let sDotMin = 20;
let sDotMax = 60;
let sCircleMin = 100;
let sCircleMax = 450;
let sVarMin = 10;
let sVarMax = 50;

// motion variables
let ampMin = 0.3;
let ampMax = 0.5;

let colorPalette = ['#0096cc'];
let hueVal;

let canvas;

p5.setup = () => {
p5.frameRate(30);
initSketch();
};

p5.windowResized = () => {
initSketch();
};

p5.draw = () => {
p5.randomSeed(90);
p5.clear();

// Update positions/size
dots.forEach((d) => {
d.run(p5.millis());
});

// Draw ring connections
for (let i = 0; i < dots.length; i++) {
let circle = dots[i];
let next = dots[(i + 1) % dots.length];
drawRing(circle, next);
}

// Draw dots
p5.noStroke();
dots.forEach((d) => {
p5.fill(d.colorDot);
p5.circle(d.position.x, d.position.y, d.sizeCircle);

p5.fill(255);
p5.circle(d.position.x, d.position.y, d.sizeDot);
});
};

function initSketch() {
let length = p5.windowWidth > p5.windowHeight ? p5.windowWidth : p5.windowHeight;
canvas = p5.createCanvas(p5.windowWidth, length);
canvas.position(0, 0);
canvas.style('z-index', -20);
p5.colorMode(p5.HSB, 100);

// Distribute inital positions radially from screen mid point

let inc = p5.TWO_PI / numDots;
let delta = 0.3 * inc;

pLength = 0.3 * length;
sDotMin = 0.01 * length;
sDotMax = 0.03 * length;
sCircleMin = 0.05 * length;
sCircleMax = 0.2 * length;
sVarMin = 0.005 * length;
sVarMax = 0.02 * length;

dots = [];
hueVal = p5.hue(p5.random(colorPalette));
for (let i = 0; i < numDots; i++) {
let theta = i * inc + p5.random(-delta, delta);
let x = p5.windowWidth * (0.3 * p5.cos(theta) + 0.5);
let y = p5.windowHeight * (0.3 * p5.sin(theta) + 0.5);
let d = new Dottie(x, y);
dots.push(d);
}
}

function drawRing(a, b) {
let centerX = (a.position.x + b.position.x) / 2;
let centerY = (a.position.y + b.position.y) / 2;
let diameter = p5.dist(a.position.x, a.position.y, b.position.x, b.position.y);

let c = p5.color(hueVal, p5.random(0, 100), p5.random(20, 30), 50);
p5.stroke(c);
p5.strokeWeight(2);
p5.noFill();

p5.circle(centerX, centerY, diameter);
}

class Dottie {
constructor(x, y) {
// init positions
this.origin = p5.createVector(x, y);
this.position = p5.createVector(x, y);

// init sizes
let s = p5.floor(p5.random(sCircleMin, sCircleMax));
this.sizeCircle = s;
this.sizeCircleOG = s;
this.sizeVar = p5.random(sVarMin, sVarMax);
this.sizeDot = p5.floor(p5.random(sDotMin, sDotMax));

// init color;
this.colorDot = p5.color((hueVal + p5.random(-10, 10)) % 100, 100, 90, p5.random(10, 30));

// init motion vars
let ampX = (p5.random(0, 1) < 0.5 ? -1 : 1) * p5.random(ampMin, ampMax) * pLength;
let ampY = (p5.random(0, 1) < 0.5 ? -1 : 1) * p5.random(ampMin, ampMax) * pLength;
this.amplitude = p5.createVector(ampX, ampY);
this.offset = p5.createVector(p5.random(0, p5.TWO_PI), p5.random(0, p5.TWO_PI));
this.frequency = p5.random(0.5, 1.0) * timeWarp;
}

run(t) {
this.position.x = this.origin.x + p5.sin(t * this.frequency + this.offset.x) * this.amplitude.x;
this.position.y = this.origin.y + p5.cos(t * this.frequency + this.offset.y) * this.amplitude.y;
this.sizeCircle = this.sizeCircleOG + (p5.sin(t * 0.0003 + this.offset.x) + 1) * this.sizeVar;
}
}
}

const BgSketch = () => {
return <ReactP5Wrapper sketch={sketch} />;
};

export default BgSketch;
6 changes: 2 additions & 4 deletions src/components/Hero.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React, { useRef } from 'react';
import left from '../assets/temp/abstract-left.svg';
import right from '../assets/temp/abstract-right.svg';
import FadeInOnScroll from './FadeInOnScroll';
import BgSketch from './BgSketch';

const Hero = () => {
return (
<div id="home" className="hero relative min-h-screen overflow-y-visible">
{/* <img src={left} className="absolute top-0 left-0 min-h-full object-cover z-[-20] max-w-2xl" alt="" />
<img src={right} className="absolute top-0 right-0 min-h-full object-cover z-[-30] max-w-2xl" alt="" /> */}
<BgSketch className="absolute flex w-full h-full justify-center items-center"></BgSketch>
<FadeInOnScroll>
<div className="hero-content flex flex-col relative text-center gap-10">
<div className="flex flex-col max-w-6xl reveal-slide-hero gap-2 rounded-lg bg-accent/80 lg:bg-transparent p-10">
Expand Down
135 changes: 0 additions & 135 deletions src/sketch.js

This file was deleted.

Loading

0 comments on commit 389b4d3

Please sign in to comment.