Search code examples
javascripthtml2canvas

Unable to set height of html2canvas


Configure the height of the generated canvas as follows , but the height of the actual generated canvas is the pixel height of the screen. I don't know why. Width also does not take effect

var opts = {
    logging: true,
    useCORS: true,  
    height:500,  
};

html2canvas(dom, opts).then(canvas =>{
    ...
})

Solution

  • The problem is solved,directly pass a canvas with its own width and height.

    var canvas = document.createElement('canvas');
    canvas.width = 500;
    canvas.height = 500;
    
    var opts = {
        canvas : canvas,
        logging: true,
        useCORS: true, 
    };
    
    html2canvas(dom, opts).then(canvas =>{
        ...
    })