-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add dpi/scale options for custom resolution #1087
Conversation
1c0dfa7
to
8c72fcc
Compare
8c72fcc
to
90f59e6
Compare
How to use this option? |
I've added two settings to the
(You can see the other settings in the html2canvas documentation). Here's some example usage: // Create a canvas with double-resolution.
html2canvas(element, {
scale: 2,
onrendered: myRenderFunction
});
// Create a canvas with 144 dpi (1.5x resolution).
html2canvas(element, {
dpi: 144,
onrendered: myRenderFunction
}); This pull request hasn't been merged into |
Hi eKoopmans, I have downloaded the latest build from above link and tried below coded, but I don't see any changes in the resolution. Just want to know if I am missing anything else to achieve high resolution html2canvas(element, { |
@hkreddy I've made a jsFiddle to demonstrate. You should notice the difference in resolution (blurriness) of the three canvases that are created. All that is necessary is to set the |
@eKoopmans , Thank you very much it is working now. |
eKoopmans I don't know you. You don't know me. We may not have any mutual family, or even friends in common. But... I love you. Thank you for this! |
Haha you're welcome @cornerbodega! |
Fantastic! I had a div I needed to screenshot and let the user download, but both text and images was somewhat blurry with the standard scaling. Using scale: 2 and resizing the image back to the desired size (the size the standard scaling would output) before download fixed the issue (used html canvas drawImage for resizing the image). Thank you very much, eKoopmans! |
You're welcome, I'm happy to help! |
@eKoopmans Thanks for your work. It really helps out. Though, I don't see it working with background images. Have you also seen that issue? |
I've figured out the work that needs to be done, the canvas created in resizeImage and used for createPattern needs to be scaled up (Just as the base canvas is scaled up), and before the fill is applied, the scale on the main ctx needs to be reset to 1, and then back again afterwards. Let me know if you'd like me to give you a pull request for the changes. |
Sure that would be great, thanks @eggers! |
Handle dpi/scale for background images
It takes better quality "screenshots" of html elements. Found it from here: niklasvh/html2canvas#1087 (comment) If that gets merged in then I'll stop using a custom build.
@eKoopmans Thanks for this change. Question on how to use it with Currently I am using |
Hey @jpatel, good question! If you're talking about |
Hey @hailehong, thanks for the comment and sorry I didn't respond earlier. Are you using the latest version of html2canvas, v1.0.0-alpha.x? I notice alpha.3 has some fixes for background size... |
Yes @eKoopmans , I am using the old version. Thank you! |
No problem. So does the latest version of html2canvas resolve your issue? |
Hi @eKoopmans, I have retested with the latest version html2canvas 1.0.0-alpha.8 |
I am trying to use the scale property with the latest version i.e. html2canvas 1.0.0-alpha.8 on an angular 2 project but I'm getting an error which says that " 'scale' does not exist in type 'Html2CanvasOptions' ".
Above is the code with which I am passing the scale value. Is my method wrong or am I passing the wrong variable? I have shared the error which i received below. Thanks in advance. |
@HarshSaudagar is that a typescript error? You'll have to update the declaration manually, or submit a pull request to the DefinitelyTyped html2canvas declaration. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/html2canvas/index.d.ts |
Thanks @eggers ! That solved my issue |
Hey @hailehong, thanks for the heads-up about blurry background images, I'll take a look into it when I have a chance. |
Hi @eKoopmans Your scaling solution has saved my life so thanks! However, I also fell foul of the background-image blurriness using your 0.5.0-beta4 version. Took me quite a while to realise that's what it was. I've been able to convert my bg images to img tags now so I'm all good but are you still going to look at this or should we move to the 1.0.0 version in future? Thanks again! |
Hi @eKoopmans .
|
You can use Scale like this. html2canvas(document.getElementById('receiptId'),{scale: 1}).then(function(canvas) { |
Hi Corey, html2canvas(document.getElementById('receiptId'),{scale: 1}).then(function(canvas) { |
Thanks, although I am little confused. Why the use of jsPDF for this? I just want a PNG. |
@eKoopmans Can you please share the code to resize the image back to its original dimensions after scale changes to 2, and before canvas.toDataURL. Here is my code. var container = document.getElementById("temp1"); |
Hi eKoopmans, |
For what it's worth, I've never been able to get the scale option to make a difference with this issue. It seems to crank up the clarity of text and regular DOM elements properly, but images remain grainy (when using jsPDF with html2canvas's result). The only workaround I've come up with is to make the dimensions of whatever your final product is double what you actually want, so for example if you're making something sized for an 8.5 x 11 piece of paper, instead make it 17 x 22. Then when you go to print, choose "scale to fit page" or whatever, and it shrinks it down and you've effectively got double resolution. |
I think i did it :) I have clear text
|
Thank you very much @eKoopmans, this fixed my issue with zoom. With option scale i can capture the element with different types of zoom and maintain the original resolution. |
the |
|
hello! i am facing the similar issue, could u pls attach demo code to show how to use the lib u mentioned? thanks! |
Feature: Custom resolution with DPI/scale options
Simple support for increasing/decreasing the canvas resolution with the new
dpi
andscale
options.Implementation
When a custom
scale
is set, the canvas' width/height are multiplied by that scale while keeping its CSS width/height at the original. Thenctx.scale
is used to scale all future canvas actions (see here for more info).The option
{dpi: 96}
is equivalent to{scale: 1}
, and larger values of either option will increase the resolution. If both options are present,scale
is ignored in favour ofdpi
.Related issues/pull requests
#127, #176, #241, #373, #379, #390, #621, #842, #900, #947, #1039, #1057