calculate-size
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -1,51 +0,42 @@ | ||
(function (dependencies, factory) { | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
var v = factory(require, exports); if (v !== undefined) module.exports = v; | ||
"use strict"; | ||
function createDummyElement(text, options) { | ||
var element = document.createElement('div'); | ||
var textNode = document.createTextNode(text); | ||
element.appendChild(textNode); | ||
element.style.fontFamily = options.font; | ||
element.style.fontSize = options.fontSize; | ||
element.style.fontWeight = options.fontWeight; | ||
element.style.position = 'absolute'; | ||
element.style.visibility = 'hidden'; | ||
element.style.left = '-999px'; | ||
element.style.top = '-999px'; | ||
element.style.width = options.width; | ||
element.style.height = 'auto'; | ||
document.body.appendChild(element); | ||
return element; | ||
} | ||
function destroyElement(element) { | ||
element.parentNode.removeChild(element); | ||
} | ||
var cache = {}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = function (text, options) { | ||
if (options === void 0) { options = {}; } | ||
var cacheKey = JSON.stringify({ text: text, options: options }); | ||
if (cache[cacheKey]) { | ||
return cache[cacheKey]; | ||
} | ||
else if (typeof define === 'function' && define.amd) { | ||
define(dependencies, factory); | ||
} | ||
})(["require", "exports"], function (require, exports) { | ||
"use strict"; | ||
function createDummyElement(text, options) { | ||
var element = document.createElement('div'); | ||
var textNode = document.createTextNode(text); | ||
element.appendChild(textNode); | ||
element.style.fontFamily = options.font; | ||
element.style.fontSize = options.fontSize; | ||
element.style.fontWeight = options.fontWeight; | ||
element.style.position = 'absolute'; | ||
element.style.visibility = 'hidden'; | ||
element.style.left = '-999px'; | ||
element.style.top = '-999px'; | ||
element.style.width = options.width; | ||
element.style.height = 'auto'; | ||
document.body.appendChild(element); | ||
return element; | ||
} | ||
function destroyElement(element) { | ||
element.parentNode.removeChild(element); | ||
} | ||
var cache = {}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = function (text, options) { | ||
if (options === void 0) { options = {}; } | ||
var cacheKey = JSON.stringify({ text: text, options: options }); | ||
if (cache[cacheKey]) { | ||
return cache[cacheKey]; | ||
} | ||
options.font = options.font || 'Times'; | ||
options.fontSize = options.fontSize || '16px'; | ||
options.fontWeight = options.fontWeight || 'normal'; | ||
options.width = options.width || 'auto'; | ||
var element = createDummyElement(text, options); | ||
var size = { | ||
width: element.offsetWidth, | ||
height: element.offsetHeight, | ||
}; | ||
destroyElement(element); | ||
cache[cacheKey] = size; | ||
return size; | ||
options.font = options.font || 'Times'; | ||
options.fontSize = options.fontSize || '16px'; | ||
options.fontWeight = options.fontWeight || 'normal'; | ||
options.width = options.width || 'auto'; | ||
var element = createDummyElement(text, options); | ||
var size = { | ||
width: element.offsetWidth, | ||
height: element.offsetHeight, | ||
}; | ||
}); | ||
destroyElement(element); | ||
cache[cacheKey] = size; | ||
return size; | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "calculate-size", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Calculate the pixel size of a string", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -22,4 +22,11 @@ calculate-size [![Build Status](https://travis-ci.org/schickling/calculate-size.svg?branch=master)](https://travis-ci.org/schickling/calculate-size) [![npm version](https://badge.fury.io/js/calculate-size.svg)](https://badge.fury.io/js/calculate-size) | ||
### Options | ||
* `font: string` | ||
* `fontSize: string` | ||
* `fontWeight: string` | ||
* `width: string` (Constraint width by a fixed value to calc height) | ||
console.log(size.width) // 140 | ||
console.log(size.height) // 20 | ||
``` |
Sorry, the diff of this file is not supported yet
32
6202
53