Skip to content

Instantly share code, notes, and snippets.

@Taureon
Last active November 15, 2024 22:15
Show Gist options
  • Save Taureon/faddd427b59cae7f7b1fbb4f7cff8f8f to your computer and use it in GitHub Desktop.
Save Taureon/faddd427b59cae7f7b1fbb4f7cff8f8f to your computer and use it in GitHub Desktop.
source code of a thing that drew a flower that i posted on wetdry.world
// gist for: https://wetdry.world/@taureon/113489275750335143
// NOTE: this is intended to just be pasted in a new tab's console
while (document.body.lastChild) document.body.lastChild.remove();
let canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');
canvas.width = 500;
canvas.height = 500;
canvas.style.width = canvas.width + 'px';
canvas.style.height = canvas.height + 'px';
canvas.style.border = '10px solid gray';
canvas.style.background = '#fff';
document.body.append(canvas);
// you can change these three
let base = 0.25, // how close the inner-pointing spike thingies should be, with 0 being "literally meeding in the middle" and 1 making the entire thing a circle
petals = 5, // integer amount of petals the flower has
exponent = 0.2; // higher = spikier petals. lower = rounder petals
let halfWidth = canvas.width / 2,
halfHeight = canvas.height / 2;
ctx.beginPath();
for (let i = 0; i < 360; i++) {
let angle = i * Math.PI / 180,
length = base + (1 - base) * (0.5 + 0.5 * Math.sin(angle * petals)) ** exponent;
let x = halfWidth + halfWidth * length * Math.sin(angle),
y = halfHeight + halfHeight * length * Math.cos(angle);
if (!i) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.closePath();
ctx.stroke();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment