A Pen by Monica.dev on CodePen.
Created
August 14, 2023 03:17
-
-
Save M0nica/f1ba6a179f1b0d31c3d071ce479fd66c to your computer and use it in GitHub Desktop.
React Rally Text x Blobs! (P5.js + React)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="root"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { | |
useCallback, | |
useRef | |
} from "https://cdn.skypack.dev/react@17.0.1"; | |
import ReactDOM from "https://cdn.skypack.dev/react-dom@17.0.1"; | |
import Loadable from "https://cdn.skypack.dev/@loadable/component@5.15.3"; | |
import { colors } from "https://codepen.io/M0nica/pen/mdQZYZa/be099074545bfdf8ff8eed63078c8357.js"; | |
// make sure we only load react-p5 on the client-side | |
// since we need access to the WINDOW | |
const Sketch = Loadable(() => | |
import("https://cdn.skypack.dev/react-p5@1.3.35") | |
); | |
const App = () => { | |
// useRef allows us to update these values in our drawing hooks and | |
// maintain the value from render to render | |
// otherwise we get an error like: | |
/* "Assignments to the 'sizeoff' variable from | |
inside React Hook useCallback will be lost after each render. | |
To preserve the value over time, store it in a useRef Hook and | |
keep the mutable value in the '.current' property. | |
Otherwise, you can move this variable directly inside useCallback. | |
(react-hooks/exhaustive-deps) */ | |
const img = useRef(); | |
const xoff = useRef(0.0); | |
const xoff1 = useRef(0); | |
const xoff2 = useRef(10000); | |
const sizeoff = useRef(2); | |
const drawSymbols = useCallback((p5) => { | |
p5.textSize(12); | |
// adjust the size of the circles based on canvas width and use frameCount increase the size of the circles as the canvas loads to make them appear to fade in | |
let scale = p5.width * 0.0009; // * //p5.min(p5.frameCount * 0.02, 1); | |
let margin = 50 * scale; | |
for (let color of colors) { | |
let { x, y } = color; | |
let mx = p5.map(x, 0, img.current.width, margin, p5.width - margin); | |
let my = p5.map(y, 0, img.current.height, margin, p5.width / 2 - margin); | |
if (x > 600) { | |
p5.fill("#7c99eb"); | |
xoff.current += 0.01; | |
// const noiseSize = p5.map(p5.noise(xoff), 0, 1, 2, 5.75); | |
const randomSize = p5.random(10, 20) * scale; | |
p5.circle(mx, my, randomSize); | |
xoff.current += 1; | |
} else { | |
p5.fill("#ff8db0"); | |
p5.circle(mx, my, 3.75 * (x * 0.003 * scale)); | |
p5.fill("#7c99eb"); | |
} | |
} | |
}, []); | |
const drawP5Asterik = useCallback((p5) => { | |
const x = p5.map(p5.noise(xoff1.current), 0, 1, 0, p5.width); | |
const y = p5.map(p5.noise(xoff2.current), 0, 1, 100, p5.height - 100); | |
// show random movement of p5js logo to compare to noise | |
// const x = map(random(), 0, 1, 0, width); | |
// const y = map(random(), 0, 1, 100, height - 100); | |
const scale = p5.width * 0.002; | |
const size = p5.map(p5.noise(sizeoff.current), 0, 1, 75, 200) * scale; | |
sizeoff.current += 0.08; | |
xoff1.current += 0.003; | |
xoff1.current += 0.003; | |
p5.fill("white"); | |
p5.textSize(size); | |
// if (p5.frameCount > 30) { | |
p5.fill("#ee1c5d"); | |
p5.text("⁎", x, y); | |
// } | |
}, []); | |
const drawSubtitle = useCallback((p5) => { | |
p5.fill("#fff"); | |
const width = p5.width; | |
const height = p5.height; | |
const scale = width * 0.0009; | |
p5.textSize(48 * scale); | |
const xOffSet = width * 0.08; | |
p5.text("Let's Sketch!", xOffSet, height * 0.78); | |
p5.textSize(32 * scale); | |
p5.text("Using p5.js & React to fill a Canvas", xOffSet, height * 0.85); | |
p5.text("Presentation By: Monica.dev 👩🏾💻", xOffSet, height * 0.92); | |
}, []); | |
const preload = useCallback((p5) => { | |
img.current = p5.loadImage( | |
"https://assets.codepen.io/209274/ReactRallyLogo.png" | |
); | |
}, []); | |
const setup = (p5, parentRef) => { | |
const width = p5.windowWidth; | |
p5.createCanvas(width, width * 0.5).parent(parentRef); | |
p5.background(43, 33, 53); | |
p5.textFont("Vidaloka"); | |
p5.noStroke(); | |
p5.frameRate(10); | |
p5.describe( | |
"React Rally 23 with React Rally written with distinct circles that appear to sparkle as they grow larger and eventually transition into blobby shape as the circles become so large that they overlap. There is a 23 written to the left constructed of dots within half of the React atom logo. There is a floating p5.js asterik logo that is easing its way across the image, growing and shrinking." | |
); | |
}; | |
const windowResized = useCallback((p5) => { | |
// maintain aspect ratio when window is resized | |
p5.resizeCanvas(window.innerWidth, window.innerWidth / 2); | |
}, []); | |
const keyPressed = useCallback((p5) => { | |
if (p5.key === "s") { | |
p5.saveGif("react-rally-welcome", 20); | |
} | |
if (p5.key === "i") { | |
p5.saveCanvas("react-rally-welcome"); | |
} | |
}); | |
const resetCanvas = useCallback((p5) => { | |
p5.clear(); | |
windowResized(p5); // work around to access resize event | |
p5.background(43, 33, 53); | |
}); | |
const draw = useCallback( | |
(p5) => { | |
resetCanvas(p5); | |
drawSymbols(p5); | |
drawSubtitle(p5); | |
drawP5Asterik(p5); | |
}, | |
[drawSubtitle, drawSymbols, drawP5Asterik, windowResized] | |
); | |
return ( | |
<div className="App"> | |
<Sketch | |
setup={setup} | |
draw={draw} | |
preload={preload} | |
keyPressed={keyPressed} | |
/> | |
</div> | |
); | |
}; | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import url("https://fonts.googleapis.com/css2?family=Vidaloka&display=swap"); | |
.App { | |
font-family: sans-serif; | |
text-align: center; | |
} | |
canvas { | |
/* center canvas in middle of page */ | |
position: absolute; | |
left: 50%; | |
top: 50%; | |
transform: translate(-50%, -50%); | |
height: 50%; | |
padding: 20px; | |
text-align: center; | |
} | |
body { | |
color: #c0def1; | |
background-color: #51455e; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment