Skip to content

Commit

Permalink
@novely/solid-renderer — kinda optimize character rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
yhdgms1 committed Jul 7, 2024
1 parent bc6bb1e commit 3453654
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 14 additions & 0 deletions packages/solid-renderer/src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ const createSolidRenderer = ({
canvas.dataset.resized = 'true';
}

if (root() !== context.root) {
const { clientWidth: mainClientWidth, clientHeight: mainClientHeight } = root();
const { clientWidth: contextClientWidth, clientHeight: contextClientHeight } = context.root;

const widthFactor = mainClientWidth / contextClientWidth;
const heightFactor = mainClientHeight / contextClientHeight;

const maxFactor = Math.ceil(Math.max(widthFactor, heightFactor));

canvas.dataset.scaleBy = (1 / maxFactor).toFixed(3);
} else {
canvas.dataset.scalyBy = '1';
}

canvasDrawImages(canvas, canvasContext, stored);
}
},
Expand Down
14 changes: 11 additions & 3 deletions packages/solid-renderer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,22 @@ const canvasDrawImages = async (canvas = createCanvas(), ctx = getContext(canvas
const sizesSorted = images.slice().sort((a, b) => b.width - a.width);
const sizes = sizesSorted[0];

canvas.width = sizes.naturalWidth;
canvas.height = sizes.naturalHeight;
const scaleBy = canvas.dataset.scaleBy ? Number(canvas.dataset.scaleBy) : 1;

/**
* Scale down, but not so much
*/
canvas.width = Math.min(sizes.naturalWidth * scaleBy * 2, sizes.naturalWidth);
canvas.height = Math.min(sizes.naturalHeight * scaleBy * 2, sizes.naturalHeight);

canvas.dataset.resized = 'true';
}

for (const image of images) {
ctx.drawImage(image, 0, 0);
/**
* In case images has different size, images with smaller size will be stretched to the canvas size
*/
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
}

return [canvas, ctx] as const;
Expand Down

0 comments on commit 3453654

Please sign in to comment.