Skip to content

Instantly share code, notes, and snippets.

@stryju

stryju/svg-size.js

Last active Aug 29, 2015
Embed
What would you like to do?
get inline SVG size in Firefox (AMD way)
/* global
define: false
*/
define( function ( ) {
'use strict';
function px( cs, prop ) {
return parseInt( cs.getPropertyValue( prop ) || 0, 10 );
}
function getWidth( cs, sizing ) {
var width = px( cs, 'width' );
if ( ! /border-box/i.test( sizing ) ) {
width += px( cs, 'borderLeftWidth' );
width += px( cs, 'paddingLeft' );
width += px( cs, 'paddingRight' );
width += px( cs, 'borderRightWidth' );
}
return width;
}
function getHeight( cs, sizing ) {
var height = px( cs, 'height' );
if ( ! /border-box/i.test( sizing ) ) {
height += px( cs, 'borderTopWidth' );
height += px( cs, 'paddingTop' );
height += px( cs, 'paddingBottom' );
height += px( cs, 'borderBottomWidth' );
}
return height;
}
function size( element ) {
var cs = getComputedStyle( element );
var sizing = cs.boxSizing || cs.webkitBoxSizing || cs.MozBoxSizing;
return {
width : getWidth( cs, sizing ),
height : getHeight( cs, sizing )
};
}
return size;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment