joint icon indicating copy to clipboard operation
joint copied to clipboard

format.toSVG(): paper background image does not appear in exported png/svg/jpeg file

Open patilvishal755 opened this issue 4 years ago • 9 comments

paper.toPNG(function (dataURI) {
      const link = document.createElement("a");
      link.href = dataURI;
      link.setAttribute("download", 'fileName.png');
      document.body.appendChild(link);
      link.click();
    });

The above code is use to export in PNG format, but the paper background image is not applied to the downloaded image

The background image is applied as flows

paper.drawBackground({
        image: 'image url',
        position: 'center',
        repeat: 'no-repeat',
        size: 'cover',
        opacity: 1
      });

Acutal Graph contained papercontained Downloaded Image image

patilvishal755 avatar Aug 02 '21 08:08 patilvishal755

Hi, this is not currently supported. A workaround is to add the <image/> tag with the background image to the SVG before the export and remove the image after the export.

const imageVel = V('image').attr({
  'xlink:href': 'image.png',
  'width': 1000,
  'height': 1000
});
V(paper.getLayerNode('back')).append(imageVel);
paper.toPNG(function(dataURL) {
  imageVel.remove();
  /*...*/
});

kumilingus avatar Aug 02 '21 14:08 kumilingus

Hi @kumilingus, thanks for quick workaround, but the background image is not scaling as per the content on the graph/paper I am giving width and height based on graph.getBBox().inflate(10) also not working with paper.getContentBBox()
can you please let me know how to set the width and height of the background image(imageVel width and height ) based on content

Actual Graph contained GraphContained

Downloaded image Download image

thanks

patilvishal755 avatar Aug 02 '21 16:08 patilvishal755

It depends on the background configuration. In your case (cover, no-repeat, center) it should be like this:

const imageVel = V('image').attr({
  'xlink:href': 'image.png',
  'preserveAspectRatio': 'none',
  ...paper.getArea().toJSON()
});
V(paper.getLayerNode('back')).append(imageVel);

kumilingus avatar Aug 02 '21 17:08 kumilingus

thanks @kumilingus its working

patilvishal755 avatar Aug 03 '21 08:08 patilvishal755

Hi again @kumilingus, The background image is not appearing in the downloaded file something on chrome browser, but for Firefox it is working as expected, also the same issue is seen on print i.e. paper.print() where sometime the background image does not appear on print preview. The code is same for print and download as mention in above comment

Thanks

patilvishal755 avatar Aug 05 '21 15:08 patilvishal755

Perhaps you have to wait until the new <image> is loaded.

kumilingus avatar Aug 05 '21 16:08 kumilingus

This issue is stale because it has been open 60 days with no activity. Please remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar May 05 '22 00:05 github-actions[bot]

Did we get any update on same?

nisargrthakkar avatar Apr 07 '23 03:04 nisargrthakkar

This one has not been implemented yet (it has a lower priority since workarounds exist).

What is your paper background configuration? We can suggest a workaround.

kumilingus avatar Apr 07 '23 09:04 kumilingus