helper.js 4.4 KB

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