简体   繁体   English

Javascript Image()获得自然的宽度和高度

[英]Javascript Image() getting natural width and height

I have a problem on how I could extract the value(s) of an image( img )'s naturalWidth and naturalHeight upon loading it via Javascript. 我在通过Javascript加载图像时如何提取图像( img )的naturalWidthnaturalHeight时遇到问题。

Basically I have this code snippet: 基本上我有以下代码片段:

var newImage = new Image(),
    naturalWidth = 0,
    naturalHeight = 0;
newImage.onload = function() {
    naturalWidth = newImage.width;
    naturalHeight = newImage.height;
}
newImage.src = imageObject.imageArea.filename;
alert("Image Size: " + naturalWidth + "x"+  naturalHeight);

I don't know if the problem is passing the value of newImage.width to naturalWidth or whatsoever. 我不知道问题是否是将newImage.width的值newImage.widthnaturalWidth或其他。

How can this be resolved? 如何解决? So basically I wanted to the problem here is how to extract the values of newImage.width to pass it to naturalWidth . 因此,基本上我想在这里解决的问题是如何提取newImage.width的值以将其传递给naturalWidth

The problem is that the image loads asynchronously and onload is called sometime in the future AFTER your alert() code runs. 问题是图像异步加载,并且在您的alert()代码运行后的将来某个时间onload You can test the height and width inside the onload handler or you can call a function from there and pass it the height and width : 您可以在onload处理程序中测试heightwidth ,也可以从那里调用一个函数并将heightwidth传递给它:

var newImage = new Image(),
newImage.onload = function() {
    var naturalWidth = newImage.width;
    var naturalHeight = newImage.height;
    // use the height and width here
    alert("Image Size: " + naturalWidth + "x"+  naturalHeight);
}
newImage.src = imageObject.imageArea.filename;

OR 要么

var newImage = new Image(),
newImage.onload = function() {
    // pass the height and width in a function call
    callMyFunc(newImage.width, newImage.height);
}
newImage.src = imageObject.imageArea.filename;

FYI, there are also naturalHeight and naturalWidth properties (which are the unscaled height and width of the image) on the image element that you may also be interested in. 仅供参考,您可能也对图像元素感兴趣,还具有naturalHeightnaturalWidth属性(它们是图像的未缩放高度和宽度)。

Javascript is asynchronous, JavaScript是异步的,

alert gets called before you set naturalWidth and naturalHeight 在设置naturalWidthnaturalHeight之前调用alert

Try this, 尝试这个,

var newImage = new Image(),
    naturalWidth = 0,
    naturalHeight = 0;
newImage.onload = function() {
    naturalWidth = newImage.width;
    naturalHeight = newImage.height;
    alert("Image Size: " + naturalWidth + "x"+  naturalHeight);
}
newImage.src = imageObject.imageArea.filename;

Your alert is called before your image is loaded 在加载图像之前调用警报

var newImage = new Image(),
    naturalWidth = 0,
    naturalHeight = 0;
if (newImage.addEventListener) {
  newImage.addEventListener('load', function() {
    alert("load complete")
   naturalWidth = newImage.width;
   naturalHeight = newImage.height;

  });
} else {
  // it's IE!
  newImage.attachEvent('onload', function() {
    naturalWidth = newImage.width;
    naturalHeight = newImage.height;
  });
}

newImage.src = "http://fc05.deviantart.net/fs70/f/2010/020/f/0/Black_Twitter_icons_by_iconhive.jpg";
 document.getElementById("img_cont").appendChild(newImage)

You can check the working fiddle 您可以检查工作提琴

==================================== ====================================

if you want to use naturalWidth and naturalHeight valus out side the function then we have to poll for those values to be set. 如果您要使用naturalWidth和naturalHeight将该函数排除在外,则我们必须轮询要设置的那些值。

var timer = setInterval(function(){
  if(naturalWidth !=0) {
   clearInterval(timer);
   alert("Image Size: " + naturalWidth + "x"+  naturalHeight);
  }
},100);

Here is the updated fiddle for the updated code 这是更新代码的更新小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM