├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── dist └── get-media-size.browser.js ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | Desktop.ini 4 | ._* 5 | Thumbs.db 6 | *.tmp 7 | *.bak 8 | *.log 9 | logs 10 | 11 | *.common-js.js 12 | *.es-modules.js 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '6' 4 | -------------------------------------------------------------------------------- /dist/get-media-size.browser.js: -------------------------------------------------------------------------------- 1 | /*! npm.im/get-media-size */ 2 | var getMediaSize=function(){"use strict";function t(t,e){if(!t)return{width:0,height:0};t.canvas&&(t=t.canvas);var i={width:t.getContext?t.width:t.naturalWidth||t.videoWidth||0,height:t.getContext?t.height:t.naturalHeight||t.videoHeight||0};return e&&(i.width/=e,i.height/=e),i}function e(e,i){return e?new Promise(function(n){var r=t(e,i);return r.width?n(r):"VIDEO"===e.tagName?e.addEventListener("loadedmetadata",function(){return n(t(e,i))}):void setTimeout(function r(){var h=t(e,i);h.width?n(h):setTimeout(r,100)},100)}):Promise.reject({width:0,height:0})}return e.sync=t,e}(); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * getMediaSize 5 | * @param {image|video|canvas|context} media The element to read the size from 6 | * @param {number} scale Optional. Convenience feature to transform the size if you're using retina canvas, for example 7 | * @return {object} Contains width and height of the passed media 8 | */ 9 | 10 | function getMediaSize(media, scale) { 11 | if (!media) { 12 | return { 13 | width: 0, 14 | height: 0 15 | }; 16 | } 17 | 18 | if (media.canvas) { // it's a ctx 19 | media = media.canvas; 20 | } 21 | 22 | const size = { 23 | width: media.getContext ? media.width : media.naturalWidth || media.videoWidth || 0, 24 | height: media.getContext ? media.height : media.naturalHeight || media.videoHeight || 0 25 | }; 26 | 27 | if (scale) { 28 | size.width /= scale; 29 | size.height /= scale; 30 | } 31 | 32 | return size; 33 | } 34 | 35 | export default function getMediaSizeAsync(media, scale) { 36 | if (!media) { 37 | return Promise.reject({ 38 | width: 0, 39 | height: 0 40 | }); 41 | } 42 | return new Promise(resolve => { 43 | const instantSize = getMediaSize(media, scale); 44 | if (instantSize.width) { 45 | return resolve(instantSize); 46 | } 47 | if (media.tagName === 'VIDEO') { 48 | return media.addEventListener('loadedmetadata', () => resolve(getMediaSize(media, scale))); 49 | } 50 | setTimeout(function check() { 51 | const size = getMediaSize(media, scale); 52 | if (size.width) { 53 | resolve(size); 54 | } else { 55 | setTimeout(check, 100); 56 | } 57 | }, 100); 58 | }); 59 | } 60 | 61 | getMediaSizeAsync.sync = getMediaSize; 62 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Federico Brigante (bfred.it) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "get-media-size", 3 | "version": "2.0.1", 4 | "description": "Get the real size of an ,