drawImage并调整大小到Canvas [英] drawImage and resize to Canvas
问题描述
我的图像是1836 x 3264我想 drawImage()
到画布并调整大小到739 x 1162.
I have an image which is 1836 x 3264 I want to drawImage()
to canvas and resize to 739 x 1162.
阅读文档后,我认为可以通过以下方式完成:
After reading the documentation I thought this could be accomplished with the following:
ctx.drawImage(image, 0, 0, 739, 1162);
我也试过:
ctx.drawImage(image, 0, 0, 1836, 3264, 0, 0, 739, 1162);
两者都只显示完整图像的一小部分而不是缩小。
Both show only a small part of the full image instead of shrinking it down.
如何通过值调整大小从1836 x 3264 - > 739 x 1162?
How do I pass through the values to resize from 1836 x 3264 -> 739 x 1162 ?
推荐答案
你看起来是正确的,所以你可以仔细检查一下错字。
What you have looks correct, so you might double-check for a typo somewhere.
:你的形象真的是1836x3264而不是3264x1836。]
: Is your image really 1836x3264 and not 3264x1836.]
这是工作代码和小提琴: http:// jsfiddle .net / m1erickson / MLGr4 /
Here is working code and a Fiddle: http://jsfiddle.net/m1erickson/MLGr4/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
img=new Image();
img.onload=function(){
canvas.width=400;
canvas.height=300;
ctx.drawImage(img,0,0,img.width,img.height,0,0,400,300);
}
img.src="http://www.onestopwebmasters.com/wp-content/uploads/2011/06/eitai-bridge.jpg";
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=100 height=100></canvas>
</body>
</html>
这篇关于drawImage并调整大小到Canvas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!