Last active
November 15, 2024 22:15
-
-
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
This file contains hidden or 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
// 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