html5 canvas is not showing full image

前端 未结 2 491
遇见更好的自我
遇见更好的自我 2020-12-11 17:17

My image size is 940 * 300 but html5 canvas showing only part of image in chrome. Can you please help how to fix this?

Below is the code



        
2条回答
  •  执念已碎
    2020-12-11 17:50

    Two things:

    • You were setting the context's size which doesn't make much sense. The canvas has a width and a height.

    • You should really use img_buffer.onload to make sure you only paint the image when it's loaded and not before it's completely loaded.

    Fiddle: http://jsfiddle.net/pimvdb/TpFQ8/

    So:

    
    

    and:

    var cv  = document.getElementById('paint_area'),
        ctx = ctx.getContext('2d');
    

    and:

    img_buffer.onload = function() {
        var imgWidth = img_buffer.width;
        var imgHeight = img_buffer.height;
        cv.width = imgWidth;
        cv.height = imgHeight;
        ctx.drawImage(img_buffer, 0, 0, imgWidth, imgHeight);
    }
    

提交回复
热议问题