measure-text-width.js 839 B

123456789101112131415161718192021
  1. import { __spreadArrays } from "tslib";
  2. import values from './values';
  3. import memoize from './memoize';
  4. import isString from './is-string';
  5. var ctx;
  6. /**
  7. * 计算文本的宽度
  8. */
  9. export 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