helper.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { RADIAN_TO_DEGREE, retrieve2, logError, isFunction } from '../core/util.js';
  2. import { parse } from '../tool/color.js';
  3. import env from '../core/env.js';
  4. var mathRound = Math.round;
  5. export function normalizeColor(color) {
  6. var opacity;
  7. if (!color || color === 'transparent') {
  8. color = 'none';
  9. }
  10. else if (typeof color === 'string' && color.indexOf('rgba') > -1) {
  11. var arr = parse(color);
  12. if (arr) {
  13. color = 'rgb(' + arr[0] + ',' + arr[1] + ',' + arr[2] + ')';
  14. opacity = arr[3];
  15. }
  16. }
  17. return {
  18. color: color,
  19. opacity: opacity == null ? 1 : opacity
  20. };
  21. }
  22. var EPSILON = 1e-4;
  23. export function isAroundZero(transform) {
  24. return transform < EPSILON && transform > -EPSILON;
  25. }
  26. export function round3(transform) {
  27. return mathRound(transform * 1e3) / 1e3;
  28. }
  29. export function round4(transform) {
  30. return mathRound(transform * 1e4) / 1e4;
  31. }
  32. export function round1(transform) {
  33. return mathRound(transform * 10) / 10;
  34. }
  35. export function getMatrixStr(m) {
  36. return 'matrix('
  37. + round3(m[0]) + ','
  38. + round3(m[1]) + ','
  39. + round3(m[2]) + ','
  40. + round3(m[3]) + ','
  41. + round4(m[4]) + ','
  42. + round4(m[5])
  43. + ')';
  44. }
  45. export var TEXT_ALIGN_TO_ANCHOR = {
  46. left: 'start',
  47. right: 'end',
  48. center: 'middle',
  49. middle: 'middle'
  50. };
  51. export function adjustTextY(y, lineHeight, textBaseline) {
  52. if (textBaseline === 'top') {
  53. y += lineHeight / 2;
  54. }
  55. else if (textBaseline === 'bottom') {
  56. y -= lineHeight / 2;
  57. }
  58. return y;
  59. }
  60. export function hasShadow(style) {
  61. return style
  62. && (style.shadowBlur || style.shadowOffsetX || style.shadowOffsetY);
  63. }
  64. export function getShadowKey(displayable) {
  65. var style = displayable.style;
  66. var globalScale = displayable.getGlobalScale();
  67. return [
  68. style.shadowColor,
  69. (style.shadowBlur || 0).toFixed(2),
  70. (style.shadowOffsetX || 0).toFixed(2),
  71. (style.shadowOffsetY || 0).toFixed(2),
  72. globalScale[0],
  73. globalScale[1]
  74. ].join(',');
  75. }
  76. export function getClipPathsKey(clipPaths) {
  77. var key = [];
  78. if (clipPaths) {
  79. for (var i = 0; i < clipPaths.length; i++) {
  80. var clipPath = clipPaths[i];
  81. key.push(clipPath.id);
  82. }
  83. }
  84. return key.join(',');
  85. }
  86. export function isImagePattern(val) {
  87. return val && (!!val.image);
  88. }
  89. export function isSVGPattern(val) {
  90. return val && (!!val.svgElement);
  91. }
  92. export function isPattern(val) {
  93. return isImagePattern(val) || isSVGPattern(val);
  94. }
  95. export function isLinearGradient(val) {
  96. return val.type === 'linear';
  97. }
  98. export function isRadialGradient(val) {
  99. return val.type === 'radial';
  100. }
  101. export function isGradient(val) {
  102. return val && (val.type === 'linear'
  103. || val.type === 'radial');
  104. }
  105. export function getIdURL(id) {
  106. return "url(#" + id + ")";
  107. }
  108. export function getPathPrecision(el) {
  109. var scale = el.getGlobalScale();
  110. var size = Math.max(scale[0], scale[1]);
  111. return Math.max(Math.ceil(Math.log(size) / Math.log(10)), 1);
  112. }
  113. export function getSRTTransformString(transform) {
  114. var x = transform.x || 0;
  115. var y = transform.y || 0;
  116. var rotation = (transform.rotation || 0) * RADIAN_TO_DEGREE;
  117. var scaleX = retrieve2(transform.scaleX, 1);
  118. var scaleY = retrieve2(transform.scaleY, 1);
  119. var skewX = transform.skewX || 0;
  120. var skewY = transform.skewY || 0;
  121. var res = [];
  122. if (x || y) {
  123. res.push("translate(" + x + "px," + y + "px)");
  124. }
  125. if (rotation) {
  126. res.push("rotate(" + rotation + ")");
  127. }
  128. if (scaleX !== 1 || scaleY !== 1) {
  129. res.push("scale(" + scaleX + "," + scaleY + ")");
  130. }
  131. if (skewX || skewY) {
  132. res.push("skew(" + mathRound(skewX * RADIAN_TO_DEGREE) + "deg, " + mathRound(skewY * RADIAN_TO_DEGREE) + "deg)");
  133. }
  134. return res.join(' ');
  135. }
  136. export var encodeBase64 = (function () {
  137. if (env.hasGlobalWindow && isFunction(window.btoa)) {
  138. return function (str) {
  139. return window.btoa(unescape(encodeURIComponent(str)));
  140. };
  141. }
  142. if (typeof Buffer !== 'undefined') {
  143. return function (str) {
  144. return Buffer.from(str).toString('base64');
  145. };
  146. }
  147. return function (str) {
  148. if (process.env.NODE_ENV !== 'production') {
  149. logError('Base64 isn\'t natively supported in the current environment.');
  150. }
  151. return null;
  152. };
  153. })();