我正在使用cropper.js库从图像获取裁剪坐标,然后创建画布以下载裁剪后的图像。
我的固定裁切尺寸最大为200x200像素
问题在于,裁剪后的图像不能完全拉伸到其应画布的200 px宽度。见下图
检查这个小提琴(感谢@entio作为小提琴)
下面是代码
<body>
<img src="Desert.jpg" id="image" style="float:left;"/>
<canvas id="myCanvas" style="height:200px; width:200px; position:absolute; right:50px; top:20px; border:1px dotted black;"></canvas>
<div>
<label><em>y</em>coordinate</label>
<input type="text" name="y" id="y-1" class="form-control" value="0" />
<label><em>x</em> coordinate</label>
<input type="text" name="x" id="x-1" class="form-control" value="0" />
<label>Selected <em>width</em></label>
<input type="text" name="width" id="width-1" class="form-control" value="0" />
<label>Selected <em>height</em></label>
<input type="text" name="width" id="height-1" class="form-control" value="0" />
</div>
</body>
<script type="text/javascript">
document.getElementById('image').onload = function() {
new Cropper(image, {
// options
max_width: 200,
max_height: 200,
update: function(coordinates) {
console.log(coordinates);
console.log(image);
for (var i in coordinates) {
document.getElementById(i + '-1').value = coordinates[i];
}
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.drawImage(image, coordinates['x'],coordinates['y'], coordinates['width'],coordinates['height'], 0, 0, 200, 200);
}
});
}
</script>
有什么帮助吗?
画布使用宽度和高度属性。
<canvas id="myCanvas" width="200" height="200" style="height:200px; width:200px; posit...
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html
“ width属性默认为300,height属性默认为150。” 因此,除非您指定是否,否则它没有您想要的大小。
内容: http : //jsfiddle.net/RgGJh/2/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句