gpt4 book ai didi

javascript - HTML5 : canvas and image size

转载 作者:太空宇宙 更新时间:2023-11-04 01:59:27 26 4
gpt4 key购买 nike

我想用 JS 在 Canvas 上绘制图像。这是我的 html 代码:

<canvas style="width:1000px;height:600px;border:1px solid black;" id="canvas"> </canvas>

这是我的js代码:

    var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var img = new Image();
img.onload = function(){
ctx.drawImage(img, 0, 0,1000,600)
}
img.src = someUrlToJpgImage;

这是位于服务器上的原始 jpg 图像 (1000x600): enter image description here

这是我在浏览器中看到的结果 (1000x600): enter image description here

如您所见,这是缩放了图像的左上角,而不是预期的整个图像。我尝试添加到 js 代码中:

ctx.imageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;

但是没有用。

如何解决这个问题?

最佳答案

您应该在 Canvas 元素上设置所需的像素数量:

<canvas style="width:1000px;height:600px;border:1px solid black;" height="600" width="1000" id="canvas"> </canvas>

关于javascript - HTML5 : canvas and image size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42029754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com