当试图使用context.drawImage()函数在画布上绘制图像而不是在画布上显示的完整图像(两者尺寸相同)时,它仅显示缩放区域图片。但是当我将图像附加到DOM时,它显示正确。你可以在jsfiddle中看到不同之处。顶部图像是画布,最下面的图像是直接附加到文档主体的图像。在html canvas中绘制的图像不是全尺寸的
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>
而不是'image.height =“400”; image.width =“800”;'write' image.style.height =“400px”; image.style.width =“800px”;' –
@RolandStarke我尝试过,但它将画布大小设置为图像的原始尺寸1174x681。 –