A Pen by Monica.dev on CodePen.
Created
August 25, 2023 14:27
-
-
Save M0nica/20a49e4d2feee01a1149af34776bc629 to your computer and use it in GitHub Desktop.
Abstract Confetti Pattern - Click to Regenerate
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
let xSize; | |
let ySize; | |
let noiseX = 0.1; | |
let noisePos = 0.1; | |
const colors = [ | |
"#ffadad", | |
"#ffd6a5", | |
"#fdffb6", | |
"#caffbf", | |
"#9bf6ff", | |
"#a0c4ff", | |
"#bdb2ff", | |
"#ffc6ff" | |
]; | |
function setup() { | |
createCanvas(windowWidth, windowHeight); | |
strokeCap(ROUND); | |
xSize = windowWidth; | |
ySize = windowHeight; | |
} | |
function draw() { | |
clear(); | |
noLoop(); | |
background("#2b2135"); | |
drawGrid(); | |
} | |
function drawGrid() { | |
let xMargin = xSize * 0.01; | |
let yMargin = ySize * 0.01; | |
let scale = 0.001 * windowHeight; | |
for (let y = yMargin * 2; y < ySize - yMargin; y += 70) { | |
for (let x = xMargin * 2; x < xSize - xMargin; x += 70) { | |
strokeWeight(map(noise(x), 0, 1, 5 * scale, 8 * scale)); | |
const xOffset = map(noise(noisePos + x + y), 0, 1, -35, 35); | |
let yOffset = map(noise(noiseX), 0, 1, 10, 20); | |
drawShape(x, y, xOffset, yOffset); | |
} | |
} | |
} | |
function drawShape(x, y, xOffset, yOffset) { | |
const currentColor = random(colors); | |
fill(currentColor); | |
stroke(currentColor); | |
const shape = random(["circle", "line", "arc"]); | |
const rand = random(10); | |
if (shape == "circle") { | |
rand > 5 ? circle(x, y, 15) : circle(x + xOffset, y + yOffset, 15); | |
} else if (shape === "line") { | |
rand > 5 | |
? line(x, y, x + xOffset, y + yOffset) | |
: line(x + xOffset, y + yOffset, x, y); | |
} else if (shape === "arc") { | |
noFill(); | |
rand > 5 | |
? arc(x, y - 10, 50, 50, 0, HALF_PI) | |
: arc(x - 10, y, 50, 50, 0, HALF_PI); | |
} | |
} | |
function mousePressed() { | |
//redraw on mouse press or touch | |
redraw(); | |
} | |
function keyPressed() { | |
const SPACEBAR = " "; | |
// redraw on spacebar press | |
if (key == SPACEBAR) { | |
redraw(); | |
} | |
// save image of canvas | |
if (key === "s") { | |
saveCanvas("canvas"); | |
} | |
} | |
function windowResized() { | |
xSize = windowWidth; | |
ySize = windowHeight; | |
resizeCanvas(windowWidth, windowHeight); | |
} |
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.6.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
canvas { | |
/* center canvas in middle of page */ | |
position: absolute; | |
left: 50%; | |
top: 50%; | |
transform: translate(-50%, -50%); | |
height: 50%; | |
text-align: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment