Update generate_png.js
This commit is contained in:
parent
cce35e5d05
commit
51077e0b82
1 changed files with 26 additions and 1 deletions
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in a new issue