GithubHelp home page GithubHelp logo

Comments (7)

Dcatfly avatar Dcatfly commented on May 7, 2024 12

第一行代码就错了吧。。

from the-front-end-knowledge-you-may-not-know.

xg4 avatar xg4 commented on May 7, 2024 7

如果 image 的width,height被设置,是不准确的。

<img src="path.png" width="100" height="100" alt="image" />

or

const image = new Image()
image.width = 100
image.height = 100
image.src = 'path.png'

应该只能依据img.naturalWidth或者new Image()来判断

export default function(image: HTMLImageElement) {
  return new Promise((resolve, reject) => {
    if (image.naturalWidth) {
      resolve({ width: image.naturalWidth, height: image.naturalHeight })
    }
    const savedImage = new Image()
    savedImage.onload = () => {
      resolve({ width: savedImage.width, height: savedImage.height })
    }
    savedImage.onerror = err => {
      reject(err)
    }
    savedImage.src = image.src
  })
}

from the-front-end-knowledge-you-may-not-know.

 avatar commented on May 7, 2024 1

其实 onload 事件需要设置在 _image.src = img.src 语句之前

from the-front-end-knowledge-you-may-not-know.

iamgqb avatar iamgqb commented on May 7, 2024

之前也写过一篇,不借助ImageElement的 iamgqb/I-should-know#2

from the-front-end-knowledge-you-may-not-know.

chbro avatar chbro commented on May 7, 2024

其实 onload 事件需要设置在 _image.src = img.src 语句之前

这里js执行比发起网络请求要快,所以不会影响吧。但放到前面会保险一些

from the-front-end-knowledge-you-may-not-know.

github-linong avatar github-linong commented on May 7, 2024
img = img || document.querySelector(img)

这句应该是不对的吧。看上去像是支持 DOM 和 选择器,但是其实并不会执行选择器部分。

img = img || document.querySelector('img')

或者是直接删掉?或者是应该判断是img是字符串,然后就去querySelector

from the-front-end-knowledge-you-may-not-know.

chbro avatar chbro commented on May 7, 2024

@github-linong 是的。
工作两年半后,来更新下代码

function getImgRawSize(img: HTMLImageElement | string): Promise<{ width: number; height: number }> {
  const $img: HTMLImageElement = typeof img === 'string' ? document.querySelector(img) : img;

  return new Promise((resolve, reject) => {
    if ($img.naturalWidth) {
      resolve({ width: $img.naturalWidth, height: $img.naturalHeight });
    }
    if ($img.complete && !$img.getAttribute('width') && !$img.getAttribute('height')) {
      resolve({ width: $img.width, height: $img.height });
    }

    const image = new Image;
    image.onload = () => {
      resolve({ width: image.width, height: image.height })
    }
    image.onerror = reject;
    image.src = $img.src;
  })
}

from the-front-end-knowledge-you-may-not-know.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.