[英]how to get entire image inside a canvas element?
I have a canvas element and want to load an entire
image inside. 我有一个canvas元素,想在其中加载entire
图像。
The canvas should be max 50% width. 画布的最大宽度应为50%。
The loaded images have different widths and heights but in any case I need entire image inside and not just a part of them. 加载的图像具有不同的宽度和高度,但是无论如何我都需要整个图像而不是其中的一部分。
Is it possible? 可能吗?
var URL = window.webkitURL || window.URL; window.onload = function() { var input = document.getElementById('input'); input.addEventListener('change', handleFiles, false); } function handleFiles(e) { var ctx = document.getElementById('canvas').getContext('2d'); var url = URL.createObjectURL(e.target.files[0]); var img = new Image(); img.onload = function() { ctx.drawImage(img, 10, 10); } img.src = url; }
#canvas{ max-width:50%; }
<input type="file" id="input"/> <canvas id="canvas"/>
drawImage
's 4th and 5th parameter is the width and height. drawImage
的第4个和第5个参数是width和height。 The example below will occupy the whole canvas. 下面的示例将占据整个画布。
var URL = window.webkitURL || window.URL; window.onload = function() { var input = document.getElementById('input'); input.addEventListener('change', handleFiles, false); } function handleFiles(e) { var ctx = document.getElementById('canvas').getContext('2d'); var url = URL.createObjectURL(e.target.files[0]); var img = new Image(); img.onload = function() { ctx.drawImage(img, 0, 0, ctx.canvas.width, ctx.canvas.height); } img.src = url; }
#canvas { background-color: salmon; max-width: 50%; }
<input type="file" id="input" /> <canvas id="canvas" />
If you want a proportional image, we can use the solution here in calculating 如果您想要比例图像,我们可以在此处使用解决方案
var URL = window.webkitURL || window.URL; window.onload = function() { var input = document.getElementById('input'); input.addEventListener('change', handleFiles, false); } function handleFiles(e) { var ctx = document.getElementById('canvas').getContext('2d'); var url = URL.createObjectURL(e.target.files[0]); var img = new Image(); img.onload = function() { var newDimen = calculateAspectRatioFit(img.width, img.height, ctx.canvas.width, ctx.canvas.height); ctx.drawImage(img, 0, 0, newDimen.width, newDimen.height); } img.src = url; } function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) { var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight); return { width: srcWidth * ratio, height: srcHeight * ratio }; }
#canvas { background-color: salmon; max-width: 50%; }
<input type="file" id="input" /> <canvas id="canvas" />
If you want a centralized image, you can: 如果您想要一个集中的图像,则可以:
function handleFiles(e) {
var ctx = document.getElementById('canvas').getContext('2d');
var url = URL.createObjectURL(e.target.files[0]);
var img = new Image();
img.onload = function() {
var newDimen = calculateAspectRatioFit( img.width, img.height, ctx.canvas.width, ctx.canvas.height );
var tSpace = ( ctx.canvas.height - newDimen.height ) / 2;
var lSpace = ( ctx.canvas.width - newDimen.width ) / 2;
ctx.drawImage(img, lSpace, tSpace, newDimen.width, newDimen.height);
}
img.src = url;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.