Comments (7)
第一行代码就错了吧。。
from the-front-end-knowledge-you-may-not-know.
如果 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.
其实 onload
事件需要设置在 _image.src = img.src
语句之前
from the-front-end-knowledge-you-may-not-know.
之前也写过一篇,不借助ImageElement的 iamgqb/I-should-know#2
from the-front-end-knowledge-you-may-not-know.
其实
onload
事件需要设置在_image.src = img.src
语句之前
这里js执行比发起网络请求要快,所以不会影响吧。但放到前面会保险一些
from the-front-end-knowledge-you-may-not-know.
img = img || document.querySelector(img)
这句应该是不对的吧。看上去像是支持 DOM 和 选择器,但是其实并不会执行选择器部分。
img = img || document.querySelector('img')
或者是直接删掉?或者是应该判断是img是字符串,然后就去querySelector
from the-front-end-knowledge-you-may-not-know.
@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)
- Async Clipboard API:异步剪贴板 API HOT 3
- window.performance 性能对象 HOT 1
- navigator.vibrate 手机震动API
- Syntax Error of the Title HOT 1
- try-catch 退出 forEach 循环 HOT 10
- 用fetch在控制台测试接口 HOT 3
- 清空 input[type="file"] 的值 HOT 4
- pointer-events: none; 穿透当前元素,由“下面”的元素接收事件 HOT 4
- 利用 URLSearchParams 对象获取URL之中的查询字符串,即问号之后的部分 HOT 6
- 利用toString检测浏览器devTool是否为开启状态 HOT 7
- 面试题,css中哪些属性对页面repaint和reflow影响最大 HOT 4
- Array.isArray(Array.prototype) === true HOT 1
- 怎样阻止谷歌浏览器的密码提示? HOT 15
- 不打印出 console 信息出现在文件哪一行 HOT 1
- PC端 Vue-SPA项目 如何做好首屏渲染优化 HOT 3
- [Docs] README 错别字 顾名思义
- 小技巧:已知年月,求该月共多少天? HOT 6
- 通过AbortController中止fetch请求后,如何判断返回的error是否属于AbortError? HOT 1
- 如何禁止 chrome 弹窗提示保存密码呢
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from the-front-end-knowledge-you-may-not-know.