Mastodon-Circles/image.js

140 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2022-11-19 12:01:33 -07:00
const toRad = (x) => x * (Math.PI / 180);
const dist = [200, 330, 450];
const numb = [8, 15, 26];
2023-08-31 12:49:04 -06:00
const radius = [64, 58, 50];
2022-11-19 12:01:33 -07:00
let userNum = 0;
2022-11-28 12:46:19 -07:00
let remainingImg = 0;
2023-08-31 12:49:04 -06:00
let totalImg = 0;
2022-11-19 12:01:33 -07:00
2023-07-19 17:16:22 -06:00
function render(users, selfUser) {
2022-11-19 14:14:44 -07:00
userNum = 0;
2022-11-28 12:46:19 -07:00
remainingImg = 0;
2022-11-19 14:14:44 -07:00
2022-11-19 12:01:33 -07:00
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
2023-08-31 12:49:04 -06:00
const width = canvas.width;
2022-11-19 12:01:33 -07:00
const height = canvas.height;
// fill the background
2022-11-20 05:03:18 -07:00
const bg_image = document.getElementById("mieke_bg");
ctx.drawImage(bg_image, 0, 0, 1000, 1000);
2022-11-19 12:01:33 -07:00
2023-08-31 12:49:04 -06:00
const tasks = [];
2022-11-19 12:01:33 -07:00
totalImg += 1;
remainingImg += 1;
2023-08-31 12:49:04 -06:00
loadImage(ctx,
selfUser.avatar,
(width / 2) - 110,
(height / 2) - 110,
110,
"@" + selfUser.handle,
tasks);
2022-11-19 12:01:33 -07:00
2023-08-31 12:49:04 -06:00
// loop over the layers
for (let layerIndex= 0; layerIndex < 3; layerIndex++) {
2022-11-19 12:01:33 -07:00
const angleSize = 360 / numb[layerIndex];
// loop over each circle of the layer
for (let i = 0; i < numb[layerIndex]; i++) {
2023-08-31 12:49:04 -06:00
// if we are trying to render a circle, but we ran out of users, just exit the loop. We are done.
if (userNum >= users.length) break;
totalImg += 1;
remainingImg += 1;
2023-08-31 12:49:04 -06:00
// We need an offset or the first circle will always on the same line, and it looks weird
2022-11-19 12:01:33 -07:00
// Try removing this to see what happens
const offset = layerIndex * 30;
// i * angleSize is the angle at which our circle goes
// We need to converting to radiant to work with the cos/sin
const r = toRad(i * angleSize + offset);
const centerX = Math.cos(r) * dist[layerIndex] + width / 2;
const centerY = Math.sin(r) * dist[layerIndex] + height / 2;
2023-08-31 12:49:04 -06:00
loadImage(
ctx,
users[userNum].avatar,
2022-11-19 12:01:33 -07:00
centerX - radius[layerIndex],
centerY - radius[layerIndex],
radius[layerIndex],
2023-08-31 12:49:04 -06:00
"@" + users[userNum].handle,
tasks
2022-11-19 12:01:33 -07:00
);
2023-08-31 12:49:04 -06:00
userNum++;
2022-11-19 12:01:33 -07:00
}
}
2023-08-31 12:49:04 -06:00
ctx.font = "12px sans-serif";
ctx.fillStyle = "silver";
2023-07-19 17:16:22 -06:00
ctx.fillText("Be gay do crime uwu", 10, 15);
2023-08-31 12:49:04 -06:00
ctx.fillStyle = "black";
ctx.fillText("https://data.natty.sh/fedi-circles", width - 170, height - 15, 160);
2022-11-19 12:01:33 -07:00
}
// Load the image from the URL and draw it in a circle
2023-08-31 12:49:04 -06:00
function loadImage(ctx, url, x, y, r, name, tasks) {
let progress = document.getElementById("outInfo");
2023-07-19 17:16:22 -06:00
const addText = () => {
ctx.font = "bold 11px sans-serif";
const textWidth = ctx.measureText(name).width;
ctx.fillStyle = "black";
const tx = textWidth > r * 2 ? x : x + r - textWidth / 2;
const ty = y + r * 2 + 3;
if (textWidth > r * 2) {
ctx.fillText(name, tx, ty + 1, r * 2);
ctx.fillStyle = "white";
ctx.fillText(name, tx, ty, r * 2);
} else {
ctx.fillText(name, tx, ty + 1);
ctx.fillStyle = "white";
ctx.fillText(name, tx, ty);
}
};
2023-08-31 12:49:04 -06:00
tasks.push(addText);
2023-07-19 17:16:22 -06:00
2023-08-31 12:49:04 -06:00
const decrementRemaining = () => {
2022-11-28 12:43:16 -07:00
remainingImg -= 1;
2023-08-31 12:49:04 -06:00
progress.innerText = `Loading avatars: ${totalImg - remainingImg}/${totalImg}`;
2022-11-28 12:46:19 -07:00
if (remainingImg <= 0) {
2023-08-31 12:49:04 -06:00
progress.innerText = "Done :3";
tasks.forEach((task) => task());
2022-11-28 12:43:16 -07:00
}
2023-08-31 12:49:04 -06:00
};
const img = new Image();
img.onload = function(){
ctx.save();
ctx.beginPath();
ctx.arc(x + r, y + r, r, 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();
ctx.drawImage(img, x, y, r * 2, r * 2);
ctx.beginPath();
ctx.arc(x + r, y + r, r, 0, Math.PI * 2, true);
ctx.clip();
ctx.closePath();
ctx.restore();
decrementRemaining();
};
2023-07-19 17:16:22 -06:00
img.onerror = function() {
console.error(`Error loading image: ${url}}`);
2023-08-31 12:49:04 -06:00
decrementRemaining();
2023-07-19 17:16:22 -06:00
};
2023-08-31 12:49:04 -06:00
img.src = url;
2022-11-19 12:01:33 -07:00
}