From cef6e0504b3496408131973f536fd2f7d442c54e Mon Sep 17 00:00:00 2001 From: Vaivaswat Date: Sat, 8 Aug 2026 16:54:51 +0530 Subject: [PATCH] fix: update import for pngs --- .github/workflows/contributors-png.yml | 2 +- utils/contributors-png.mjs | 54 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 utils/contributors-png.mjs diff --git a/.github/workflows/contributors-png.yml b/.github/workflows/contributors-png.yml index 27d6dd939f..f1258ad4c2 100644 --- a/.github/workflows/contributors-png.yml +++ b/.github/workflows/contributors-png.yml @@ -26,7 +26,7 @@ jobs: run: npm install canvas - name: Run contributors-png generator - run: node utils/contributors-png.js + run: node utils/contributors-png.mjs - name: Reset all changes except contributors.png run: | diff --git a/utils/contributors-png.mjs b/utils/contributors-png.mjs new file mode 100644 index 0000000000..ec13c50c8f --- /dev/null +++ b/utils/contributors-png.mjs @@ -0,0 +1,54 @@ +import { createCanvas, loadImage } from 'canvas'; +import fs from 'fs'; + +const data = fs.readFileSync('.all-contributorsrc', 'utf-8'); +const parsed = JSON.parse(data); +const contributors = parsed.contributors; + +const AVATAR_SIZE = 50; +const GAP = 4; +const COLS = 40; +const ROWS = Math.ceil(contributors.length / COLS); + +const width = COLS * AVATAR_SIZE + (COLS - 1) * GAP; +const height = ROWS * AVATAR_SIZE + (ROWS - 1) * GAP; + +const canvas = createCanvas(width, height); +const ctx = canvas.getContext('2d'); + +async function loadAvatar(url) { + try { + const res = await fetch(url); + if (!res.ok) throw new Error(`HTTP ${res.status}`); + const buffer = Buffer.from(await res.arrayBuffer()); + return await loadImage(buffer); + } catch (err) { + return null; + } +} + +for (let i = 0; i < contributors.length; i++) { + const c = contributors[i]; + const col = i % COLS; + const row = Math.floor(i / COLS); + const x = col * (AVATAR_SIZE + GAP); + const y = row * (AVATAR_SIZE + GAP); + const img = await loadAvatar(c.avatar_url); + + ctx.save(); + ctx.beginPath(); + ctx.arc( + x + AVATAR_SIZE / 2, + y + AVATAR_SIZE / 2, + AVATAR_SIZE / 2, + 0, + Math.PI * 2 + ); + ctx.clip(); + if (img) { + ctx.drawImage(img, x, y, AVATAR_SIZE, AVATAR_SIZE); + } + ctx.restore(); +} + +fs.writeFileSync('contributors.png', canvas.toBuffer('image/png')); \ No newline at end of file