Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(YNOProject): Draw a pixel of random color to resized image #9235

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions websites/A/Anghami/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
},
"contributors": [
{
"name": "Aphex2in",
"id": "577284974001258507"
}
"name": "Aphex2in",
"id": "577284974001258507"
}
],
"service": "Anghami",
"altnames": [
Expand All @@ -19,7 +19,7 @@
"en": "Make some noise, the world is listening."
},
"url": "play.anghami.com",
"version": "1.4.0",
"version": "1.4.1",
"logo": "https://cdn.rcd.gg/PreMiD/websites/A/Anghami/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/A/Anghami/assets/thumbnail.jpg",
"color": "#FF00FE",
Expand Down
2 changes: 1 addition & 1 deletion websites/Y/YNOProject/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"ynoproject.net",
"www.ynoproject.net"
],
"version": "1.0.3",
"version": "1.0.4",
"logo": "https://cdn.rcd.gg/PreMiD/websites/Y/YNOProject/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/Y/YNOProject/assets/thumbnail.png",
"color": "#785377",
Expand Down
23 changes: 21 additions & 2 deletions websites/Y/YNOProject/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class GameState {

/**
* Resize pixelated image. Beware of high perf cost.
* The result image is unique even though it originates from the same source,
* for dealing with discord cache problem.
* @param href url
* @param dw destination width
* @param dh destination height
Expand All @@ -126,12 +128,29 @@ async function fetchWithResizePixelatedImage(
canvas.width = dw;
canvas.height = dh;
const g = canvas.getContext("2d");
g.fillStyle = `#${randomRGBColor()}01`;
g.fillRect(0, 0, 1, 1);
g.imageSmoothingEnabled = false;
g.drawImage(img, 0, 0, img.width, img.height, 0, 0, dw, dh);
return new Promise<Blob>(resolve => canvas.toBlob(resolve, "image/png"));
});
}

/**
* Pick a random RGB color in hex
* @example "665557", "0fce53", "87c9db"
*/
function randomRGBColor() {
const colorInHex = Math.floor(Math.random() * 0xffffff).toString(16),
slot = Array.from(Array(6), () => "0");
slot.splice(
slot.length - colorInHex.length,
colorInHex.length,
...colorInHex
);
return slot.join("");
}

/** We still need this function for inspecting what format the image is in */
async function blob2dataurl(blob: Blob) {
return new Promise<string>((resolve, reject) => {
Expand Down Expand Up @@ -211,9 +230,9 @@ class SimpleLRU<V = unknown> {
* Keys stand for characters' faces urls start with "blob",
* and the values are their faces but large-scaled in Data URLs
*
* In case of something glitches on re-sampling...
* In case of something glitches on re-sampling, or discord cache problem...
* Switching to other effects for 5 times or taking any animated actions
* to let the oldest cache is re-generated.
* to evict the oldest cache.
*/
const characterFacesCache = new SimpleLRU<string>(5),
/**
Expand Down