From 3131d2264a64ca359e5cdf7722289ce9801cc69f Mon Sep 17 00:00:00 2001 From: Boris Mesin Date: Tue, 13 Jan 2015 10:36:36 +0100 Subject: [PATCH] Dynamic font size dependant on height Default font-size is automatically adjusted to 45% of data-height. I added an option to pass percent as fontSize value. --- initial.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/initial.js b/initial.js index d6f822a..ca02ed9 100644 --- a/initial.js +++ b/initial.js @@ -14,13 +14,24 @@ textColor: '#ffffff', height: 100, width: 100, - fontSize: 60, + fontSize: 0.45, // default font size is 45% of height fontWeight: 400, fontFamily: 'HelveticaNeue-Light,Helvetica Neue Light,Helvetica Neue,Helvetica, Arial,Lucida Grande, sans-serif' }, options); // overriding from data attributes settings = $.extend(settings, e.data()); + + if (settings.fontSize < 1) { + // if font size < 1 presume that this is a percentage of height + // e.g. - settings.fontSize: .45 + settings.fontSize = parseInt(settings.height * settings.fontSize); + } + else if (settings.fontSize.toString().indexOf("%") > 1) { + // if font size has % character calculate it from height + // e.g. - settings.fontSize: "45%" + settings.fontSize = parseInt(settings.height * (parseInt(settings.fontSize.replace("%", "")) / 100)); + } // making the text object var c = settings.name.substr(0, settings.charCount).toUpperCase(); @@ -33,7 +44,7 @@ 'font-family': settings.fontFamily }).html(c).css({ 'font-weight': settings.fontWeight, - 'font-size': settings.fontSize+'px', + 'font-size': settings.fontSize + 'px', }); var colorIndex = Math.floor((c.charCodeAt(0) - 65) % colors.length); @@ -45,8 +56,8 @@ 'height': settings.height }).css({ 'background-color': colors[colorIndex], - 'width': settings.width+'px', - 'height': settings.height+'px' + 'width': settings.width + 'px', + 'height': settings.height + 'px' }); svg.append(cobj); @@ -58,4 +69,4 @@ }) }; -}(jQuery)); \ No newline at end of file +}(jQuery));