An example of manipulating the pixels in an image to create an animation using P5.js with React.
A Pen by Monica.dev on CodePen.
<div id="root"></div> |
An example of manipulating the pixels in an image to create an animation using P5.js with React.
A Pen by Monica.dev on CodePen.
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 xoff = useRef(0.0); | |
const xoff1 = useRef(0); | |
const xoff2 = useRef(10000); | |
const sizeoff = useRef(2); | |
const imgDimensions = { width: 1667, height: 834 }; | |
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.01, 1); | |
for (let color of colors) { | |
let { x, y } = color; | |
let mx = p5.map(x, 0, imgDimensions.width, 20, p5.width); | |
let my = p5.map(y, 0, imgDimensions.height, 20, p5.width / 2); | |
if (x > 600) { | |
xoff.current += 0.01; | |
// const noiseSize = p5.map(p5.noise(xoff), 0, 1, 2, 5.75); | |
const randomSize = p5.random(2, 5.75) * scale; | |
p5.circle(mx, my, randomSize); | |
xoff.current += 1; | |
} else { | |
p5.fill("#7c99eb"); | |
p5.circle(mx, my, 3.75 * (x * 0.003 * scale)); | |
p5.fill("#c0def1"); | |
} | |
} | |
}, []); | |
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) => { | |
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 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( | |
"A message that says React Rally 23 with React Rally written with circles that appear to sparkle as they change size from frame to frame and 23 written with circles 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 event | |
p5.background(43, 33, 53); | |
p5.fill("#e9e0f0"); | |
}); | |
const draw = useCallback( | |
(p5) => { | |
resetCanvas(p5); | |
drawSymbols(p5); | |
drawSubtitle(p5); | |
drawP5Asterik(p5); | |
}, | |
[drawSubtitle, drawSymbols, drawP5Asterik, windowResized, resetCanvas] | |
); | |
return ( | |
<div className="App"> | |
<Sketch setup={setup} draw={draw} keyPressed={keyPressed} /> | |
</div> | |
); | |
}; | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script> |
@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; | |
} |