【发布时间】:2018-03-01 04:49:52
【问题描述】:
我正在尝试使用 drawImage 调整图像大小但保持比例。到目前为止我有这个......
window.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 0, 0, 600, 428);
}
<p>Image to use:</p>
<img id="scream" width="400" height="300" src="http://lorempixel.com/1280/960/sports/" alt="The Scream">
<p>Canvas:</p>
<canvas id="myCanvas" width="428" height="600" style="border:2px solid black;">
Your browser does not support the HTML5 canvas tag.
</canvas>
我试图让图像填充容器以丢失底部的空白区域,如果我放入确切的尺寸,那么它就会被拉伸。
有没有人可以为我指明方向的例子?
【问题讨论】:
-
你的意思是保持这个比例?喜欢stackoverflow.com/questions/21961839/… 或stackoverflow.com/questions/34428723 的选项之一?
标签: javascript html css drawimage