UNPKG

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