Update generate_png.js

This commit is contained in:
Bernard Xie 2023-04-13 13:40:58 -07:00
parent cce35e5d05
commit 51077e0b82
No known key found for this signature in database
GPG key ID: 3C3E0036CE0F892C

View file

@ -1,4 +1,4 @@
async ({imgString, scale}) => {
async ({ imgString, scale }) => {
const tempImg = new Image();
const loadImage = () => {
return new Promise((resolve, reject) => {
@ -13,6 +13,31 @@ async ({imgString, scale}) => {
const canvas = document.createElement("canvas");
canvas.width = img.width * scale;
canvas.height = img.height * scale;
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas
const MAX_DIMENSION = 32767;
const MAX_AREA = 268435456;
const ratio = img.width / img.height;
if (ratio > 1) {
if (canvas.width > MAX_DIMENSION) {
canvas.width = MAX_DIMENSION;
canvas.height = MAX_DIMENSION / ratio;
}
} else {
if (canvas.height > MAX_DIMENSION) {
canvas.height = MAX_DIMENSION;
canvas.width = MAX_DIMENSION * ratio;
}
}
const currentArea = canvas.width * canvas.height;
if (currentArea > MAX_AREA) {
const areaRatio = MAX_AREA / currentArea;
canvas.height = Math.floor(canvas.height * areaRatio);
canvas.width = Math.floor(canvas.width * areaRatio);
}
const ctx = canvas.getContext("2d");
if (!ctx) {
return new Error("could not get canvas context");