helper.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. function isSafeNum(num) {
  2. return isFinite(num);
  3. }
  4. export function createLinearGradient(ctx, obj, rect) {
  5. var x = obj.x == null ? 0 : obj.x;
  6. var x2 = obj.x2 == null ? 1 : obj.x2;
  7. var y = obj.y == null ? 0 : obj.y;
  8. var y2 = obj.y2 == null ? 0 : obj.y2;
  9. if (!obj.global) {
  10. x = x * rect.width + rect.x;
  11. x2 = x2 * rect.width + rect.x;
  12. y = y * rect.height + rect.y;
  13. y2 = y2 * rect.height + rect.y;
  14. }
  15. x = isSafeNum(x) ? x : 0;
  16. x2 = isSafeNum(x2) ? x2 : 1;
  17. y = isSafeNum(y) ? y : 0;
  18. y2 = isSafeNum(y2) ? y2 : 0;
  19. var canvasGradient = ctx.createLinearGradient(x, y, x2, y2);
  20. return canvasGradient;
  21. }
  22. export function createRadialGradient(ctx, obj, rect) {
  23. var width = rect.width;
  24. var height = rect.height;
  25. var min = Math.min(width, height);
  26. var x = obj.x == null ? 0.5 : obj.x;
  27. var y = obj.y == null ? 0.5 : obj.y;
  28. var r = obj.r == null ? 0.5 : obj.r;
  29. if (!obj.global) {
  30. x = x * width + rect.x;
  31. y = y * height + rect.y;
  32. r = r * min;
  33. }
  34. x = isSafeNum(x) ? x : 0.5;
  35. y = isSafeNum(y) ? y : 0.5;
  36. r = r >= 0 && isSafeNum(r) ? r : 0.5;
  37. var canvasGradient = ctx.createRadialGradient(x, y, 0, x, y, r);
  38. return canvasGradient;
  39. }
  40. export function getCanvasGradient(ctx, obj, rect) {
  41. var canvasGradient = obj.type === 'radial'
  42. ? createRadialGradient(ctx, obj, rect)
  43. : createLinearGradient(ctx, obj, rect);
  44. var colorStops = obj.colorStops;
  45. for (var i = 0; i < colorStops.length; i++) {
  46. canvasGradient.addColorStop(colorStops[i].offset, colorStops[i].color);
  47. }
  48. return canvasGradient;
  49. }
  50. export function isClipPathChanged(clipPaths, prevClipPaths) {
  51. if (clipPaths === prevClipPaths || (!clipPaths && !prevClipPaths)) {
  52. return false;
  53. }
  54. if (!clipPaths || !prevClipPaths || (clipPaths.length !== prevClipPaths.length)) {
  55. return true;
  56. }
  57. for (var i = 0; i < clipPaths.length; i++) {
  58. if (clipPaths[i] !== prevClipPaths[i]) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. function parseInt10(val) {
  65. return parseInt(val, 10);
  66. }
  67. export function getSize(root, whIdx, opts) {
  68. var wh = ['width', 'height'][whIdx];
  69. var cwh = ['clientWidth', 'clientHeight'][whIdx];
  70. var plt = ['paddingLeft', 'paddingTop'][whIdx];
  71. var prb = ['paddingRight', 'paddingBottom'][whIdx];
  72. if (opts[wh] != null && opts[wh] !== 'auto') {
  73. return parseFloat(opts[wh]);
  74. }
  75. var stl = document.defaultView.getComputedStyle(root);
  76. return ((root[cwh] || parseInt10(stl[wh]) || parseInt10(root.style[wh]))
  77. - (parseInt10(stl[plt]) || 0)
  78. - (parseInt10(stl[prb]) || 0)) | 0;
  79. }