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

How to improve the image quality? #310

Open
Sweet-KK opened this issue Sep 2, 2019 · 25 comments
Open

How to improve the image quality? #310

Sweet-KK opened this issue Sep 2, 2019 · 25 comments

Comments

@Sweet-KK
Copy link

Sweet-KK commented Sep 2, 2019

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
Copy link

wLove-c commented Dec 16, 2019

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

@nsequeira87
Copy link

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
Copy link

moevbiz commented Mar 15, 2020

for fixing a display bug with responsive nodes see -> #69 (comment)

@handycode
Copy link

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
Copy link

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
Copy link

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.
#332 (comment)

@Rabbitzzc
Copy link

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
Copy link

elisma commented Aug 2, 2020

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

A shot in the dark, try:

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

@csandman
Copy link

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
Copy link

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
Copy link

istoan commented Dec 21, 2021

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
Copy link

TonyZhang1993 commented Mar 28, 2022

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
Copy link

TonyZhang1993 commented Mar 29, 2022

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?

@TonyZhang1993
Copy link

TonyZhang1993 commented Jun 29, 2022 via email

@Prhldchauhan
Copy link

`
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
Copy link

aurevae commented Jul 20, 2022

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
Copy link

TonyZhang1993 commented Oct 11, 2022 via email

@uvistix
Copy link

uvistix commented Mar 1, 2024

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
Copy link

TonyZhang1993 commented Mar 1, 2024 via email

@forever0714yuan
Copy link

forever0714yuan commented Mar 1, 2024 via email

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