dashStyle.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import { isArray, isNumber, map } from '../core/util';
  2. import Path from '../graphic/Path';
  3. import TSpan from '../graphic/TSpan';
  4. export function normalizeLineDash(lineType: any, lineWidth?: number): number[] | false {
  5. if (!lineType || lineType === 'solid' || !(lineWidth > 0)) {
  6. return null;
  7. }
  8. return lineType === 'dashed'
  9. ? [4 * lineWidth, 2 * lineWidth]
  10. : lineType === 'dotted'
  11. ? [lineWidth]
  12. : isNumber(lineType)
  13. ? [lineType] : isArray(lineType) ? lineType : null;
  14. }
  15. export function getLineDash(el: Path | TSpan): [number[] | false, number] {
  16. const style = el.style;
  17. let lineDash = style.lineDash && style.lineWidth > 0 && normalizeLineDash(style.lineDash, style.lineWidth);
  18. let lineDashOffset = style.lineDashOffset;
  19. if (lineDash) {
  20. const lineScale = (style.strokeNoScale && el.getLineScale) ? el.getLineScale() : 1;
  21. if (lineScale && lineScale !== 1) {
  22. lineDash = map(lineDash, function (rawVal) {
  23. return rawVal / lineScale;
  24. });
  25. lineDashOffset /= lineScale;
  26. }
  27. }
  28. return [lineDash, lineDashOffset];
  29. }