Created
October 31, 2024 10:32
-
-
Save tiagorangel1/06fb6e2a340b938344ad2c1b272a63da to your computer and use it in GitHub Desktop.
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
const canvas = document.createElement('canvas'); | |
canvas.width = 1000; | |
canvas.height = 1000; | |
document.body.appendChild(canvas); | |
const ctx = canvas.getContext('2d'); | |
let currentIndex = 0; | |
const totalDigits = 1000000; | |
const digitsPerRequest = 1000; | |
function fetchAndDrawDigits(start) { | |
const url = `https://api.pi.delivery/v1/pi?start=${start}&numberOfDigits=${digitsPerRequest}`; | |
fetch(url) | |
.then(response => response.json()) | |
.then(data => { | |
const piDigits = data.content; | |
for (let i = 0; i < piDigits.length; i++) { | |
const digit = parseInt(piDigits[i]); | |
const grayscale = Math.floor((digit / 9) * 255); | |
const x = (currentIndex + i) % 1000; | |
const y = Math.floor((currentIndex + i) / 1000); | |
ctx.fillStyle = `rgb(${grayscale}, ${grayscale}, ${grayscale})`; | |
ctx.fillRect(x, y, 1, 1); | |
} | |
currentIndex += piDigits.length; | |
if (currentIndex < totalDigits) { | |
fetchAndDrawDigits(currentIndex); | |
} else { | |
console.log('Finished'); | |
} | |
}) | |
} | |
fetchAndDrawDigits(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment