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

Why is this happening #53

Open
tomimarkus991 opened this issue Jan 19, 2023 · 5 comments
Open

Why is this happening #53

tomimarkus991 opened this issue Jan 19, 2023 · 5 comments

Comments

@tomimarkus991
Copy link

Expected: text is centered like on webpage
image

Result: also the above icons are blurred, but that already has a PR
image

@sneuvonen
Copy link

I have the same issue; a large part of vertically centered texts get displaced, but not all.

@eugeneZolotarenko
Copy link

I have the same issue; a large part of vertically centered texts get displaced, but not all.

Have you managed to fix it?

@ronildo
Copy link

ronildo commented Sep 19, 2023

I have the same issue....

@eugeneZolotarenko
Copy link

The problem is specifically in html2canvas library.
Here's the PR with the potential fix: niklasvh/html2canvas#2938
Maybe we can use html2canvas-pro instead? It's an updated version with fixed bugs.

@lachlanjc
Copy link

If anyone else is looking to use html2canvas-pro in their project, here's the hook code adapted from this library, with TypeScript types:

// Adapted from https://github.com/vre2h/use-react-screenshot/blob/master/src/index.js
// but using html2canvas-pro for its upgrades

import { useState } from "react";
import html2canvas, { Options } from "html2canvas-pro";

export const useScreenshot = ({
  type,
  quality,
}: {
  type?: "image/jpeg" | "image/png";
  quality?: number;
} = {}): [
  string | null,
  (ref: HTMLDivElement) => Promise<string | void>,
  Object,
] => {
  const [image, setImage] = useState<string | null>(null);
  const [error, setError] = useState(null);

  const takeScreenShot = (
    node: HTMLElement,
    options: Partial<Options> = {},
  ) => {
    if (!node) {
      throw new Error("You should provide correct html node.");
    }
    return html2canvas(node, options)
      .then((canvas) => {
        const croppedCanvas = document.createElement("canvas");
        const croppedCanvasContext = croppedCanvas.getContext("2d");

        const cropPositionTop = 0;
        const cropPositionLeft = 0;
        const cropWidth = canvas.width;
        const cropHeight = canvas.height;

        croppedCanvas.width = cropWidth;
        croppedCanvas.height = cropHeight;

        croppedCanvasContext?.drawImage(
          canvas,
          cropPositionLeft,
          cropPositionTop,
        );

        const base64Image = croppedCanvas.toDataURL(type, quality);

        setImage(base64Image);
        return base64Image;
      })
      .catch(setError);
  };

  return [image, takeScreenShot, { error }];
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants