简体   繁体   English

Javascript错误:无法读取null的属性“ width”

[英]Javascript error: Cannot read property 'width' of null

I tried to get the color of pixels on an image. 我试图获取图像上像素的颜色。 I found the code here: How to get a pixel's x,y coordinate color from an image? 我在这里找到了代码: 如何从图像中获取像素的x,y坐标颜色? .

Error: 错误:

Uncaught TypeError: Cannot read property 'width' of null. 未捕获的TypeError:无法读取null的属性“宽度”。

<canvas id="main"></canvas>    

<script>

    var img = document.getElementById("https://static.wixstatic.com/media/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png/v1/fill/w_980,h_735,al_c,usm_0.66_1.00_0.01/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png");
    var canvas = document.createElement("main");

    canvas.width = img.width;     //<-- error her
    canvas.height = img.height;

    pix = canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);







</script>

I tried to get the color of pixels on an image. 我试图获取图像上像素的颜色。

Error: 错误:

Uncaught TypeError: Cannot read property 'width' of null. 未捕获的TypeError:无法读取null的属性“宽度”。

Looks like you want to select the image by its src property. 看起来您想通过其src属性选择图像 Use querySelector instead since it is unlikely that you have an image with this id . 请改用querySelector因为您不太可能拥有带有此id的图像。

Then replace this line 然后替换这条线

 var img = document.getElementById("https://static.wixstatic.com/media/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png/v1/fill/w_980,h_735,al_c,usm_0.66_1.00_0.01/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png");

by 通过

var img = document.querySelector("img[src='https://static.wixstatic.com/media/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png/v1/fill/w_980,h_735,al_c,usm_0.66_1.00_0.01/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png']");

This is because your script might be in the head or your image is getting loaded slowly so that it's getting executed after the script is run. 这是因为您的script可能位于head或者图像加载缓慢,因此在脚本运行后将其执行。

Use this: 用这个:

    var img = new Image();
    img.src = "https://static.wixstatic.com/media/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png/v1/fill/w_980,h_735,al_c,usm_0.66_1.00_0.01/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png"
    img.onload = function(){
      var height = img.height;
      var width = img.width;

      // code here to use the dimensions
    }

You are providing the image's src attribute, not the id . 您提供的是图片的src属性,而不是id

If your element has an ID, then it's very simple, you just specify it: 如果您的元素具有ID,则非常简单,只需指定它即可:

var img = document.getElementById("yourImgId");

If your element does not have an ID, I recommend you add one to it if you're going to access it from JavaScript. 如果您的元素没有ID,则建议您从JavaScript访问它,然后在其中添加一个ID。

If your element does not have an ID, and you cannot add it, then try this solution: 如果您的元素没有ID,并且您无法添加它,请尝试以下解决方案:

function getImgBySrc(src) {
    var allImages = document.getElementsByTagName("img");
    for(var i = 0; i < allImages.length; i++)
        if (allImages[i].src === src) {
            return allImages[i];
        }
    }
}

And then: 接着:

var img = getImgBySrc("https://static.wixstatic.com/media/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png/v1/fill/w_980,h_735,al_c,usm_0.66_1.00_0.01/50592f_dca6acd5bb474e61b306e1969e7e8f66~mv2_d_2339_1654_s_2.png");

canvas.width = (img && img.width) || 200; // Provide a default width if image was not found
canvas.height = (img && img.height) || 400; // Provide a default height if image was not found

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

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