簡體   English   中英

在html畫布中繪制的圖像不是完整尺寸

[英]Image drawn in html canvas is not full-sized

當嘗試使用context.drawImage()函數在畫布上繪制圖像時,而不是在畫布上顯示完整圖像(兩者大小相同)時,它僅顯示圖像的縮放區域。 但是,當我將圖像附加到DOM時,它可以正確顯示。 您可以在jsfiddle中看到區別。 頂部圖像是畫布,底部圖像是直接附加到文檔正文的圖像。

https://jsfiddle.net/3fnq62nx/

這也是代碼片段,這里可能出什么問題了?

<html>
<body>
<canvas id="canvas" style="border: 1px solid black">
</canvas>
<script type="text/javascript">
var canvas  = document.getElementById("canvas");
var image = new Image();
image.src = "room_default.jpg";
image.height = "400";
image.width = "800";
image.onload = function(){
    canvas.width = image.width;
    canvas.height = image.height;
    document.body.appendChild(image);
    var ctx= canvas.getContext("2d");
    ctx.drawImage(image,0,0);
}
</script>
</body>

是您的解決方案

var canvas  = document.getElementById("canvas");
var image = new Image();
image.src = "http://cdn.home-designing.com/wp-content/uploads/2011/06/livingroom_i_by_dragon2525-d3ioeyf.jpg";
image.height = "400";
image.width = "800";
image.onload = function(){
    canvas.width = image.width;
    canvas.height = image.height;
    document.body.appendChild(image);
    var ctx= canvas.getContext("2d");
    ctx.drawImage(image,0,0,image.width,image.height);
}

更新ctx.drawImage(image,0,0,image.width,image.height);

您可以將canvas圖像事件的高度和寬度作為drawImage函數的第4和第5參數傳遞

    hi please check this your issue resolvevar canvas  = document.getElementById("canvas");
var image = new Image();
image.src = "http://cdn.home-designing.com/wp-content/uploads/2011/06/livingroom_i_by_dragon2525-d3ioeyf.jpg";
image.height = "400";
image.width = "800";
image.onload = function(){
    canvas.width = image.width;
    canvas.height = image.height;
    document.body.appendChild(image);
    var ctx= canvas.getContext("2d");
    ctx.drawImage(image,0,0,image.width,image.height);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM