[英]Canvas drawImage is not accepting width and height
Image is getting created in full original size, even last two arguments 150, 150
are height and width context.drawImage(img, 0, 0, 150, 150);
图像将以原始大小创建,即使最后两个参数
150, 150
都是高度和宽度context.drawImage(img, 0, 0, 150, 150);
in the code below: 在下面的代码中:
function (file) { //uploaded files are always images
var reader = new FileReader(); //FileReader for uploading files from local stroge.
reader.onload = function () {
var links = document.createElement('a'); //link when image is clicked
var img = document.createElement('img');
img.src = reader.result; //src = url from uploaded file
img.className = 'images'; //css -> .images { margin-top: 30px; padding: 30px; }
img.onload = function () { //repaint image to 150 - 150 size with canvas, because setting width and height on image itself would just resize the image but I want to create new image with new size
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0, 150, 150) //draw image with canvas
}
links.href = reader.result; // url from local storage needed when image is clicked -
links.target = "_blank"; // open new blank page with original image
links.appendChild(img); // image is appended to <a>
document.body.appendChild(links); // <a> is appended to body, that body contains image thumbnail with a link linked to the image source
}
if (file) {
reader.readAsDataURL(file); // read uploaded files url
}
}
img.onload
does not making any sense here. img.onload
在这里没有任何意义。 result is the same even when I remove it. 即使删除它,结果也一样。
You are not drawing back the cropped image to your <img>
tag... you will have to create two image Objects, let's call the first originalImage
, and the second one croppedImage
. 您没有将裁剪的图像拖回
<img>
标记...,您将不得不创建两个图像对象,让我们分别调用第一个originalImage
和第二个croppedImage
。
The one you will append to the document is croppedImage
and originalImage
will just stay in the cache. 您将追加到文档的一个是
croppedImage
, originalImage
将仅保留在缓存中。
When originalImage
has loaded, you will paint it to a canvas, and then set croppedImage
to the result of the canvas' toDataURL()
method. 加载
originalImage
,将其绘制到画布上,然后将croppedImage
设置为画布的toDataURL()
方法的结果。
var read = function() { var file = this.files[0]; var reader = new FileReader(); reader.onload = function() { var links = document.createElement('a'); // this will be the appended image var croppedImage = new Image(); // do your DOM stuff croppedImage.className = 'images'; links.href = reader.result; links.target = "_blank"; links.appendChild(croppedImage); document.body.appendChild(links); // create a buffer image object var originalImage = new Image(); // set its load handler originalImage.onload = function() { // create a canvas var canvas = document.createElement('canvas'); // set canvas width/height canvas.width = canvas.height = 150; var context = canvas.getContext('2d'); // draw the buffered image to the canvas at required dimension context.drawImage(originalImage, 0, 0, 150, 150); // set the appended to doc image's src to the result of the cropping operation croppedImage.src = canvas.toDataURL(); } originalImage.src = reader.result; } if (file) { reader.readAsDataURL(file); } }; upload.onchange = read;
.images { margin-top: 30px; padding: 30px; }
<input type="file" id="upload" />
You could also have used only a single image object, but this would have required to reset the onload
event in the onload
event, to avoid an infinite loop, which is a little bit less clear : 您也可能只使用了一个图像对象,但这将需要在
onload
事件中重置onload
事件,以避免无限循环,这种循环不太清楚:
var read = function() { var file = this.files[0]; var reader = new FileReader(); reader.onload = function() { var links = document.createElement('a'); var img = new Image(); img.className = 'images'; links.href = reader.result; links.target = "_blank"; links.appendChild(img); document.body.appendChild(links); img.onload = function() { //reset the onload event so it does fire in a loop img.onload = function(){return;}; var canvas = document.createElement('canvas'); canvas.width = canvas.height = 150; var context = canvas.getContext('2d'); context.drawImage(this, 0, 0, 150, 150); this.src = canvas.toDataURL(); } img.src = reader.result; } if (file) { reader.readAsDataURL(file); } }; upload.onchange = read;
.images { margin-top: 30px; padding: 30px; }
<input type="file" id="upload" />
var reader = new FileReader();
reader.onload = function () {
var links = document.createElement('a');
var img = new Image();
img.src = reader.result;
img.className = 'images';
img.onload = function () {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
context.drawImage(this, 0, 0, 150, 150);
this.src = canvas.toDataURL(); // convert the canvas back to the image
links.appendChild(this); // append the updated image to the document
}
links.href = reader.result;
links.target = "_blank";
document.body.appendChild(links);
}
if (file) {
reader.readAsDataURL(file); //reads the data as a URL
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.