Skip to content

Commit

Permalink
feat: Download image
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Dec 10, 2024
1 parent e5ebba3 commit ef50841
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/components/detail-page/download-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,31 @@ export type Download =

const getImageDataFromElement = async (elementId: string): Promise<string> => {
const element = document.getElementById(elementId);
const canvas = await html2canvas(element as HTMLElement);
return canvas.toDataURL("image/png");
const canvas = await html2canvas(element as HTMLElement, {
// Should remove the grey areas
// https://github.com/niklasvh/html2canvas/issues/2183
scale: 2,
});

// create downsized canvas (scale reduced by 2)
// copy the current canvas to the downsized canvas reducing the scale by 2
const downsizedCanvas = document.createElement("canvas");
downsizedCanvas.width = canvas.width / 2;
downsizedCanvas.height = canvas.height / 2;
const ctx = downsizedCanvas.getContext("2d");
ctx!.drawImage(
canvas,
0,
0,
canvas.width,
canvas.height,
0,
0,
downsizedCanvas.width,
downsizedCanvas.height
);

return downsizedCanvas.toDataURL("image/png");
};

// helper to wait a frame
Expand Down

0 comments on commit ef50841

Please sign in to comment.