A Pen by Monica.dev on CodePen.
Created
August 17, 2024 22:25
-
-
Save M0nica/21beb75127b34b5ac5f21101c5700699 to your computer and use it in GitHub Desktop.
React Miami - Different style, same algorithm -Grid with movement- react-wrapper
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
// runnable version at https://codepen.io/M0nica/pen/ZEZjgKO/53bdd7d5de3eb8b5043e764771c2f6c1?editors=0010 | |
import React from "https://cdn.skypack.dev/react@17.0.1"; | |
import ReactDOM from "https://cdn.skypack.dev/react-dom@17.0.1"; | |
import { ReactP5Wrapper } from "https://cdn.skypack.dev/@p5-wrapper/react@4.4.0"; | |
const App = () => { | |
function sketch(p5) { | |
p5.setup = () => { | |
p5.createCanvas(p5.windowWidth, p5.windowHeight); | |
p5.background("#f1b4c6"); | |
// renders atom as unicode instead of emoji on all devices | |
// renders atom as unicode instead of emoji on all devices | |
p5.textFont("monospace"); | |
p5.describe( | |
"a grid of pink asterisks arranged in a diamond-like pattern against a lght pink background where the size of the asterik increases as the mouse gets closer and shrinks as the mouse is further away moving the grid in a wave-like pattern in response to cursor movement" | |
); | |
}; | |
p5.draw = () => { | |
p5.background("#f1b4c6"); | |
let spacing = 50; | |
const elements = ["⚛", "⌑", "⊹"]; | |
let count = 0; | |
let margin = 150; | |
for (let x = margin; x < p5.width - margin; x += spacing) { | |
for (let y = margin; y < p5.height - margin; y += spacing) { | |
if ((x + y) % 20 == 0) { | |
let xDistance = p5.abs(p5.mouseX - x); | |
let yDistance = p5.abs(p5.mouseY - y); | |
const sz = p5.map( | |
xDistance + yDistance, | |
0, | |
p5.width + p5.height, | |
200, | |
5 | |
); | |
const op = Math.floor( | |
p5.map(xDistance + yDistance, 0, p5.width + p5.height, 99, 10) | |
); | |
p5.textSize(sz); | |
p5.fill(`#ed225d${op}`); | |
p5.text("⁎", x, y); | |
} | |
} | |
count++; | |
} | |
}; | |
p5.windowResized = () => { | |
p5.resizeCanvas(p5.windowWidth, p5.windowHeight); | |
p5.background("#f1b4c6"); | |
p5.drawGrid(); | |
}; | |
p5.keyPressed = () => { | |
const SPACEBAR = " "; | |
if (p5.key === "g") { | |
p5.saveGif("hello-react-miami", 5); | |
} | |
if (p5.key === "s" || p5.key === SPACEBAR) { | |
p5.saveCanvas("hello-react-miami"); | |
} | |
}; | |
} | |
return ( | |
<div className="App"> | |
<ReactP5Wrapper sketch={sketch} />{" "} | |
</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
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.min.js"></script> |
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=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap"); | |
canvas { | |
/* center canvas in middle of page */ | |
position: absolute; | |
left: 50%; | |
top: 50%; | |
transform: translate(-50%, -50%); | |
height: 50%; | |
text-align: center; | |
} | |
@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