diff --git a/docs/fontScale.js.html b/docs/fontScale.js.html new file mode 100644 index 0000000..25178b6 --- /dev/null +++ b/docs/fontScale.js.html @@ -0,0 +1,86 @@ + + + + + JSDoc: Source: fontScale.js + + + + + + + + + + +
+ +

Source: fontScale.js

+ + + + + + +
+
+
/** @module fontScale */
+
+/**
+ * Scale an element's font size to fit inside the given width.
+ * @param {HTMLElement} element 
+ * @param {Int} width defaults to `window.innerWidth`
+ * @returns {Promise<Int>} resulting font size in px
+ */
+export function fontScale(element,width=innerWidth){
+    return new Promise(function(resolve){
+        let fontSize=0
+        element.style.fontSize=fontSize+'px'
+        width-=element.clientWidth
+        function down(){
+            if(element.clientWidth>=width){
+                fontSize-=1
+                element.style.fontSize=fontSize+'px'
+                requestAnimationFrame(down)
+            }else{
+                resolve(fontSize)
+            }
+        }
+        function up(){
+            if(element.clientWidth<width){
+                fontSize+=10
+                element.style.fontSize=fontSize+'px'
+                requestAnimationFrame(up)
+            }else{
+                requestAnimationFrame(down)
+            }
+        }
+        requestAnimationFrame(up)
+    })
+}
+
+export default fontScale
+
+
+ + + + +
+ + + +
+ + + + + + + diff --git a/docs/module-fontScale.html b/docs/module-fontScale.html new file mode 100644 index 0000000..74eccab --- /dev/null +++ b/docs/module-fontScale.html @@ -0,0 +1,313 @@ + + + + + JSDoc: Module: fontScale + + + + + + + + + + +
+ +

Module: fontScale

+ + + + + + +
+ +
+ +
+ +
+
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + +

Methods

+ + + + + + + +

(static) fontScale(element, width) → {Promise.<Int>}

+ + + + + + +
+

Scale an element's font size to fit inside the given width.

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
element + + +HTMLElement + + + +
width + + +Int + + + +

defaults to window.innerWidth

+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+

resulting font size in px

+
+ + + +
+
+ Type +
+
+ +Promise.<Int> + + +
+
+ + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/fontScale.js b/fontScale.js new file mode 100644 index 0000000..1aa3f60 --- /dev/null +++ b/fontScale.js @@ -0,0 +1,36 @@ +/** @module fontScale */ + +/** + * Scale an element's font size to fit inside the given width. + * @param {HTMLElement} element + * @param {Int} width defaults to `window.innerWidth` + * @returns {Promise} resulting font size in px + */ +export function fontScale(element,width=innerWidth){ + return new Promise(function(resolve){ + let fontSize=0 + element.style.fontSize=fontSize+'px' + width-=element.clientWidth + function down(){ + if(element.clientWidth>=width){ + fontSize-=1 + element.style.fontSize=fontSize+'px' + requestAnimationFrame(down) + }else{ + resolve(fontSize) + } + } + function up(){ + if(element.clientWidth