示例: 获取图像的尺寸
// program to get the dimensions of an image const img = new Image(); // get the image img.src = '//cdn.programiz.com/sites/tutorial2program/files/cover-artwork.png'; // get height and width img.onload = function() { console.log('width ' + this.width) console.log('height '+ this.height); }
输出
width 1040 height 922
在上面的程序中,
new Image()
构造函数用于创建一个图像对象。
Image()
构造函数创建一个新的图像元素实例。
img.src
然后用于使用图像 URL 源添加图像。
img.onload()
函数用于访问图像的高度和宽度。