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

Canvas within element not being rendered in SVG #181

Open
Zander1983 opened this issue Feb 15, 2023 · 3 comments
Open

Canvas within element not being rendered in SVG #181

Zander1983 opened this issue Feb 15, 2023 · 3 comments

Comments

@Zander1983
Copy link

Hi
The div element I want to convert to an SVG contains canvas elements, as well as regular HTML elemenrs. These canvas elements are not being rendered. What I would like rendered:
Screenshot 2023-02-15 at 17 58 35

What is actually being rendered:

Screenshot 2023-02-15 at 17 59 06

Is it possible to also render the canvas elements?

@LanderBeeuwsaert
Copy link

I have the same requirement, and indeed nothing being rendered. @Zander1983 did you find a solution eventually?

@jkurkure
Copy link

Hi, saw this thread. Just wanted to say, I had this exact same issue. What I did was to convert all the Canvas elements in my page into PNGs, and then convert to SVG, which causes the graphics to appear properly. As far as I can tell, there is no visual difference when I do this.

Here's the code I used to do the conversion:

// Get all canvas elements on the page
const canvases = document.querySelectorAll('canvas');

// Loop through each canvas
canvases.forEach(canvas => {
  // Create a temporary canvas to draw the image
  const tempCanvas = document.createElement('canvas');
  tempCanvas.width = canvas.width;
  tempCanvas.height = canvas.height;
  const tempContext = tempCanvas.getContext('2d');
  tempContext.drawImage(canvas, 0, 0);

  // Get the PNG data URL from the temporary canvas
  const pngDataUrl = tempCanvas.toDataURL('image/png');

  // Create a new image element with the PNG data URL
  const img = document.createElement('img');
  img.src = pngDataUrl;

  // Replace the canvas with the image
  canvas.parentNode.replaceChild(img, canvas);
});

@op3
Copy link

op3 commented Apr 9, 2024

I adapted your code to also work with hidpi canvases:

// Get all canvas elements on the page
const canvases = document.querySelectorAll('canvas');

// Loop through each canvas
canvases.forEach(canvas => {
  // Create a temporary canvas to draw the image
  const tempCanvas = document.createElement('canvas');
  tempCanvas.width = canvas.width;
  tempCanvas.height = canvas.height;
  rect = canvas.getBoundingClientRect();
  scaleX = rect.width / canvas.width;
  scaleY = rect.height / canvas.height;
  const tempContext = tempCanvas.getContext('2d');
  tempContext.drawImage(canvas, 0, 0);

  // Get the PNG data URL from the temporary canvas
  const pngDataUrl = tempCanvas.toDataURL('image/png');

  // Create a new image element with the PNG data URL
  const img = document.createElement('img');
  img.src = pngDataUrl;
  img.style.transform = "scale(" + scaleX + ", " + scaleY + ")";
  img.style.transformOrigin = "0 0";

  // Replace the canvas with the image
  canvas.parentNode.replaceChild(img, canvas);
});

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

4 participants