admin管理员组文章数量:1336660
I have this HTML code :
<div>
<canvas id="canvas">
</canvas>
<img id="canvasimg" src="{{ $photo }}" style="display:none;"/>
</div>
and I have this javascript to draw an image into that HTML Canvas :
<script>
var canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
var deviceWidth = window.innerWidth;;
canvasWidth = deviceWidth - 40;
canvasHeight = deviceWidth - 40;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
var img = document.getElementById('canvasimg');
imgx = 0;
imgy = 0;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, imgx, imgy);
</script>
the problem is when I load this page into wide screen, it also produce a wider canvas. while the image has fix width and height.
how to stratch that image so that it will fill the canvas perfectly?
I have this HTML code :
<div>
<canvas id="canvas">
</canvas>
<img id="canvasimg" src="{{ $photo }}" style="display:none;"/>
</div>
and I have this javascript to draw an image into that HTML Canvas :
<script>
var canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
var deviceWidth = window.innerWidth;;
canvasWidth = deviceWidth - 40;
canvasHeight = deviceWidth - 40;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
var img = document.getElementById('canvasimg');
imgx = 0;
imgy = 0;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, imgx, imgy);
</script>
the problem is when I load this page into wide screen, it also produce a wider canvas. while the image has fix width and height.
how to stratch that image so that it will fill the canvas perfectly?
Share Improve this question asked Aug 14, 2015 at 15:52 Saint RobsonSaint Robson 5,52518 gold badges74 silver badges121 bronze badges1 Answer
Reset to default 5Use the dWidth
and dHeight
arguments for the drawImage
function (MDN).
ctx.drawImage(img, imgx, imgy, canvas.width, canvas.height);
本文标签: javascriptPut Image Into HTML Canvas (Fit Width and Height)Stack Overflow
版权声明:本文标题:javascript - Put Image Into HTML Canvas (Fit Width and Height) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742418240a2471135.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论