【发布时间】:2023-03-12 19:45:01
【问题描述】:
我正在加载图像,然后将该图像绘制到画布上。我也在缩小图像以适合画布。当我这样做时,图像以适当的宽度绘制到画布上,但高度约为画布实际高度的 1/4。
//Calculate height from canvas-to-image width ratio
var width = canvas.width;
var height = ( width / img.width ) * img.height;
//Draw scale image (This results in a squished height, despite "height" being correct)
context.drawImage( image, 0, 0, img.width, img.height, 0, 0, width, height );
即使height 是正确的(在控制台中检查),图像仍以小得多的高度绘制到画布上。
另外,当我检查canvas.width / canvas.height 与img.width / img.height 的比率时,我得到:.707234 与.707818。 这太接近了,无法解释巨大的高度问题。
为什么会这样?我该如何解决这个问题?
编辑:在 Chrome 上,这段代码正确显示。
【问题讨论】:
-
这是什么意思:var ratio = (width / img.width) * img.width;?
-
@能否给出缩放高度
-
(x/y) * y 就是 x。只要 y 不为零,它就不会改变。我认为这不是你想要的。
-
@CandiedOrange 请查看我对您的回答的评论。我错误地复制了我的代码,但发现另一个有真正错误的帖子。
标签: javascript ios ipad canvas mobile-safari