dom-to-image icon indicating copy to clipboard operation
dom-to-image copied to clipboard

How to improve the image quality?

Open Sweet-KK opened this issue 4 years ago • 25 comments

domtoimage.toBlob(shareContent).then(function(blob) { FileSaver.saveAs(blob, _this.fileName + '.png') }) Hello, I has a problem,the picture is not really clear. How to improve the image quality? Can you add a option, like scale. We can scale the canvas before render.

Sweet-KK avatar Sep 02 '19 14:09 Sweet-KK

I had the same problem, How to improve the image quality?

wLove-c avatar Dec 16 '19 04:12 wLove-c

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

nsequeira87 avatar Jan 10 '20 16:01 nsequeira87

for fixing a display bug with responsive nodes see -> https://github.com/tsayen/dom-to-image/issues/69#issuecomment-486146688

moevbiz avatar Mar 15 '20 12:03 moevbiz

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

thanks a lot, it's work!

handycode avatar Mar 19 '20 08:03 handycode

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

The scale improves the quality of the image, but also increase the dimensions, but I really need the image with original width and height. Did someone find something helpful in that case?

kamilzki avatar Mar 19 '20 10:03 kamilzki

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

The scale improves the quality of the image, but also increase the dimensions, but I really need the image with original width and height. Did someone find something helpful in that case?

Check out this. It might help you. https://github.com/tsayen/dom-to-image/issues/332#issuecomment-633847362

akshaybosamiya avatar May 26 '20 07:05 akshaybosamiya

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

Can you explain the meaning of this code?

Rabbitzzc avatar Jul 15 '20 07:07 Rabbitzzc

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

awesomee!! thanks

elisma avatar Aug 02 '20 04:08 elisma

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

Can you explain the meaning of this code?

It applies a css scale tranformation to double the image size before exporting

csandman avatar Oct 24 '20 01:10 csandman

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

Hey can you tell how to implement it and plus can you explain?

priyanshu0171 avatar Nov 01 '20 17:11 priyanshu0171

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

Hey can you tell how to implement it and plus can you explain?

There isn't a lot to tell, this is just an implementation of the function from the docs, with custom dimensions and css styles. It doubles the width/height of the image, and sets a CSS transform:scale to double the dimensions of the images content. When you say, "how to implement" this is it, just add the function exactly as he has it and it will work. Look at the readme if you want more info on how the options parameter is defined.

csandman avatar Nov 18 '20 17:11 csandman

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

This works great for me but when I try to generate the image onto my page it is shifted 50% to the right. So I have 50% of the image just black and then 50% of my image that I wanted.... although the quality of that 50% is great lol.

Any ideas?

Gigamick avatar Dec 12 '20 23:12 Gigamick

A shot in the dark, try:

 transform: 'scale('+scale+') translateX(-50%)'

nsequeira87 avatar Dec 13 '20 18:12 nsequeira87

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

This works great for me but when I try to generate the image onto my page it is shifted 50% to the right. So I have 50% of the image just black and then 50% of my image that I wanted.... although the quality of that 50% is great lol.

Any ideas?

Do you have the transformOrigin: 'top left' in custom style?

csandman avatar Dec 14 '20 05:12 csandman

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

Hey can you tell how to implement it and plus can you explain?

transformOrigin: "50% 50%" as well.

huuphongnguyen avatar Nov 09 '21 10:11 huuphongnguyen

let scale = 5;
let style = {
    transform: `scale(${scale})`,
    transformOrigin: 'top left',
    width: domNode.clientWidth + 'px', // use original width of DOM element to avoid part of the image being cropped out
    height: domNode.clientHeight + 'px' // use original height of DOM element
};

domtoimage.toPng(domNode, {
    width: domNode.clientWidth * scale,
    height: domNode.clientHeight * scale,
    style: style })

istoan avatar Dec 21 '21 15:12 istoan

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

This works great for me but when I try to generate the image onto my page it is shifted 50% to the right. So I have 50% of the image just black and then 50% of my image that I wanted.... although the quality of that 50% is great lol. Any ideas?

Do you have the transformOrigin: 'top left' in custom style?

I had the same issue. The image generated onto my page it is shifted 50% to the right. But this issue happened occasionally. Failed case: image

successful example: image

with the following configuration: const scale = 2 image

any ideas for the issue? Thanks in advance!!

TonyZhang1993 avatar Mar 28 '22 02:03 TonyZhang1993

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

The solution brings up another problem -> Case: when the dom I want to download is too heigt, the picture at the end download is a full blank with the images at the first rendered well. failed picture: image the pic we want: image

I find the same open issue here: https://github.com/tsayen/dom-to-image/issues/323#issue-550687140

@csandman nsequeira87 any ideas?

TonyZhang1993 avatar Mar 29 '22 02:03 TonyZhang1993

你好,你的邮件已收到,我会尽快给您回复。

TonyZhang1993 avatar Jun 29 '22 15:06 TonyZhang1993

` Here scaling is done according to the width, you can even do same with the height. const scale=(element_width_expected)/(element_original_width);

domtoimage
  .toPng(PNG, { quality: 0.95 , width: PNG.clientWidth * scale,
    height: PNG.clientHeight * scale,
    style: {
     transform: 'scale('+scale+')',
     transformOrigin: 'top left'
   }})
  .then(function (dataUrl) {
   //this just download the specifc image automatic.
    var link = document.createElement("a");
    link.download = "my-page.png";
    link.href = dataUrl;
    link.click();
  })
  .then(function () {
    console.log("download Done");
    PNG.removeAttribute("style");
    PNG.classList.add("scrollable");
    focusedNode.classList.add("focusDiv");
  });`

Prhldchauhan avatar Jun 29 '22 15:06 Prhldchauhan

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

The solution brings up another problem -> Case: when the dom I want to download is too heigt, the picture at the end download is a full blank with the images at the first rendered well. failed picture: image the pic we want: image

I find the same open issue here: #323 (comment)

@csandman nsequeira87 any ideas?

just write transformOrigin: 'center' I succeeded

aurevae avatar Jul 20 '22 13:07 aurevae

你好,你的邮件已收到,我会尽快给您回复。

TonyZhang1993 avatar Oct 11 '22 09:10 TonyZhang1993

A simple trick could be something like:

var scale = 2;
domtoimage.toBlob(domNode, {
 width: domNode.clientWidth * scale,
 height: domNode.clientHeight * scale,
 style: {
  transform: 'scale('+scale+')',
  transformOrigin: 'top left'
})

Thanks, this worked..

uvistix avatar Mar 01 '24 09:03 uvistix

你好,你的邮件已收到,我会尽快给您回复。

TonyZhang1993 avatar Mar 01 '24 09:03 TonyZhang1993

你好,我已收到你的来信。辛苦了!!

forever0714yuan avatar Mar 01 '24 09:03 forever0714yuan