tsayen / dom-to-image

Generates an image from a DOM node using HTML5 canvas
Other
10.23k stars 1.68k forks source link

How to improve the image quality? #310

Open Sweet-KK opened 4 years ago

Sweet-KK commented 4 years ago

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.

wLove-c commented 4 years ago

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

nsequeira87 commented 4 years ago

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'
})
moevbiz commented 4 years ago

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

handycode commented 4 years ago

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!

kamilzki commented 4 years ago

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?

akshaybosamiya commented 4 years ago

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

Rabbitzzc commented 4 years ago

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?

elisma commented 3 years ago

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

csandman commented 3 years ago

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

priyanshu0171 commented 3 years ago

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?

csandman commented 3 years ago

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.

Gigamick commented 3 years ago

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?

nsequeira87 commented 3 years ago

A shot in the dark, try:

 transform: 'scale('+scale+') translateX(-50%)'
csandman commented 3 years ago

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?

huuphongnguyen commented 2 years ago

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.

istoan commented 2 years ago
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 })
TonyZhang1993 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

Prhldchauhan commented 2 years ago

` 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");
  });`
aurevae commented 1 year ago

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

TonyZhang1993 commented 1 year ago

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

uvistix commented 4 months ago

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..

TonyZhang1993 commented 4 months ago

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

forever0714yuan commented 4 months ago

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