UNPKG

839 BJavaScriptView Raw
1import { __spreadArrays } from "tslib";
2import values from './values';
3import memoize from './memoize';
4import isString from './is-string';
5var ctx;
6/**
7 * 计算文本的宽度
8 */
9export default memoize(function (text, font) {
10 if (font === void 0) { font = {}; }
11 var fontSize = font.fontSize, fontFamily = font.fontFamily, fontWeight = font.fontWeight, fontStyle = font.fontStyle, fontVariant = font.fontVariant;
12 if (!ctx) {
13 ctx = document.createElement('canvas').getContext('2d');
14 }
15 ctx.font = [fontStyle, fontVariant, fontWeight, fontSize + "px", fontFamily].join(' ');
16 return ctx.measureText(isString(text) ? text : '').width;
17}, function (text, font) {
18 if (font === void 0) { font = {}; }
19 return __spreadArrays([text], values(font)).join('');
20});
21//# sourceMappingURL=measure-text-width.js.map
\No newline at end of file