Resize the canvas output image to a specific size (width/height)

前端 未结 1 924
北恋
北恋 2021-02-09 17:57

I have a big canvas (5000x5000) and I want to take a picture of it and create a thumbnail on client side. I can capture the image using canvas.toDataURL

1条回答
  •  忘了有多久
    2021-02-09 18:15

    Something like this should work, given you don't have security restrictions on the original canvas element:

    var resizedCanvas = document.createElement("canvas");
    var resizedContext = resizedCanvas.getContext("2d");
    
    resizedCanvas.height = "100";
    resizedCanvas.width = "200";
    
    var canvas = document.getElementById("original-canvas");
    
    resizedContext.drawImage(canvas, 0, 0, 200, 100);
    var myResizedData = resizedCanvas.toDataURL();
    

    0 讨论(0)
提交回复
热议问题