3.12 kBJavaScriptView Raw
1import { DEFAULT_PATH_STYLE } from '../graphic/Path.js';
2import ZRImage from '../graphic/Image.js';
3import { getLineDash } from '../canvas/dashStyle.js';
4import { map } from '../core/util.js';
5import { normalizeColor } from './helper.js';
6var NONE = 'none';
7var mathRound = Math.round;
8function pathHasFill(style) {
9 var fill = style.fill;
10 return fill != null && fill !== NONE;
11}
12function pathHasStroke(style) {
13 var stroke = style.stroke;
14 return stroke != null && stroke !== NONE;
15}
16var strokeProps = ['lineCap', 'miterLimit', 'lineJoin'];
17var svgStrokeProps = map(strokeProps, function (prop) { return "stroke-" + prop.toLowerCase(); });
18export default function mapStyleToAttrs(updateAttr, style, el, forceUpdate) {
19 var opacity = style.opacity == null ? 1 : style.opacity;
20 if (el instanceof ZRImage) {
21 updateAttr('opacity', opacity);
22 return;
23 }
24 if (pathHasFill(style)) {
25 var fill = normalizeColor(style.fill);
26 updateAttr('fill', fill.color);
27 var fillOpacity = style.fillOpacity != null
28 ? style.fillOpacity * fill.opacity * opacity
29 : fill.opacity * opacity;
30 if (forceUpdate || fillOpacity < 1) {
31 updateAttr('fill-opacity', fillOpacity);
32 }
33 }
34 else {
35 updateAttr('fill', NONE);
36 }
37 if (pathHasStroke(style)) {
38 var stroke = normalizeColor(style.stroke);
39 updateAttr('stroke', stroke.color);
40 var strokeScale = style.strokeNoScale
41 ? el.getLineScale()
42 : 1;
43 var strokeWidth = (strokeScale ? (style.lineWidth || 0) / strokeScale : 0);
44 var strokeOpacity = style.strokeOpacity != null
45 ? style.strokeOpacity * stroke.opacity * opacity
46 : stroke.opacity * opacity;
47 var strokeFirst = style.strokeFirst;
48 if (forceUpdate || strokeWidth !== 1) {
49 updateAttr('stroke-width', strokeWidth);
50 }
51 if (forceUpdate || strokeFirst) {
52 updateAttr('paint-order', strokeFirst ? 'stroke' : 'fill');
53 }
54 if (forceUpdate || strokeOpacity < 1) {
55 updateAttr('stroke-opacity', strokeOpacity);
56 }
57 if (style.lineDash) {
58 var _a = getLineDash(el), lineDash = _a[0], lineDashOffset = _a[1];
59 if (lineDash) {
60 lineDashOffset = mathRound(lineDashOffset || 0);
61 updateAttr('stroke-dasharray', lineDash.join(','));
62 if (lineDashOffset || forceUpdate) {
63 updateAttr('stroke-dashoffset', lineDashOffset);
64 }
65 }
66 }
67 else if (forceUpdate) {
68 updateAttr('stroke-dasharray', NONE);
69 }
70 for (var i = 0; i < strokeProps.length; i++) {
71 var propName = strokeProps[i];
72 if (forceUpdate || style[propName] !== DEFAULT_PATH_STYLE[propName]) {
73 var val = style[propName] || DEFAULT_PATH_STYLE[propName];
74 val && updateAttr(svgStrokeProps[i], val);
75 }
76 }
77 }
78 else if (forceUpdate) {
79 updateAttr('stroke', NONE);
80 }
81}