Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make transparent color white instead of black in html2canvas?

Tags:

html2canvas

I'm trying to take a screenshot using html2canvas:

var body = document.getElementById("body")
$(body).html2canvas({onrendered: function( canvas ) {  
     var urlData = canvas.toDataURL("image/jpeg");  
}});

My body's background is transparent, and therefore urlData because of jpeg has black background (not white like in browser).
How can I fix this behavior and make background color white in this case?

like image 231
Zelzer Avatar asked Jan 29 '13 14:01

Zelzer


3 Answers

I modified temporary: _html2canvas.Util.isTransparent from

_html2canvas.Util.isTransparent = function(backgroundColor) {
  return (backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)");
};

to

_html2canvas.Util.isTransparent = function(backgroundColor) {
  return (backgroundColor === "transparent" ||
    backgroundColor === "rgba(0, 0, 0, 0)" ||
    backgroundColor === undefined);
};

and after that it was enough to call html2canvas with background parameter set:

html2canvas(screenshotElement, {
  background: '#FFFFFF',
  onrendered: function (canvas) {
    // ...
  }
});

For me... it makes sense to consider transparent a background that is undefined.

like image 163
CRK Avatar answered Nov 19 '22 17:11

CRK


Try this

var body = document.getElementById("body") 
$(body).html2canvas({background:'#fff', onrendered: function( canvas ) {  
 var urlData = canvas.toDataURL("image/jpeg");  
}});
like image 10
Sergey L Avatar answered Nov 19 '22 17:11

Sergey L


I'd say, it's even simplier now(30/05/18) - just pass backgroundColor: null in html2canvas options:

html2canvas(
    document.querySelector("#some-id-to-export"),
    {backgroundColor: null}
).then(canvas => {$("#id-to-render-transparent-bg-img").html(canvas);});
like image 8
Oleksandr Khryplyvenko Avatar answered Nov 19 '22 16:11

Oleksandr Khryplyvenko