Skip to content

Commit df3b3f3

Browse files
committed
fix(svg): parse opacity of empty/'none'/'transparent' color as 0 and add warning for development environment
1 parent 247e119 commit df3b3f3

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/svg/helper.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import env from '../core/env';
1515
const mathRound = Math.round;
1616

1717
export function normalizeColor(color: string): { color: string; opacity: number; } {
18+
if (process.env.NODE_ENV !== 'production') {
19+
if (!color || color === 'none' || color === 'transparent') {
20+
console.warn(`'${color}' is an illegal value for transparency in SVG, please use 'rgba(r,g,b,0)' instead.`);
21+
}
22+
}
23+
1824
let opacity;
1925
if (!color || color === 'transparent') {
2026
color = 'none';
@@ -29,7 +35,9 @@ export function normalizeColor(color: string): { color: string; opacity: number;
2935
}
3036
return {
3137
color,
32-
opacity: opacity == null ? 1 : opacity
38+
opacity: opacity == null
39+
? color === 'none' ? 0 : 1
40+
: opacity
3341
};
3442
}
3543
const EPSILON = 1e-4;

test/svg-gradient.html

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>SVG Gradient</title>
6+
<script src="./lib/config.js"></script>
7+
<script src="../dist/zrender.js"></script>
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
</head>
10+
<body>
11+
<div id="main"></div>
12+
<script type="text/javascript">
13+
var zr = zrender.init(document.getElementById('main'), {
14+
renderer: 'svg',
15+
width: 200,
16+
height: 200
17+
});
18+
zr.setBackgroundColor('navajowhite');
19+
20+
var linearGradient1 = new zrender.LinearGradient(0, 0, 0, 1);
21+
linearGradient1.addColorStop(0, 'transparent');
22+
linearGradient1.addColorStop(1, 'orange');
23+
24+
var linearGradient2 = new zrender.LinearGradient(0, 0, 0, 1);
25+
linearGradient2.addColorStop(0, 'none');
26+
linearGradient2.addColorStop(1, 'orange');
27+
28+
var linearGradient3 = new zrender.LinearGradient(0, 0, 0, 1);
29+
linearGradient3.addColorStop(0, '');
30+
linearGradient3.addColorStop(1, 'orange');
31+
32+
zr.add(
33+
new zrender.Rect({
34+
shape: {
35+
x: 20,
36+
y: 50,
37+
width: 20,
38+
height: 100,
39+
},
40+
style: {
41+
fill: linearGradient1
42+
}
43+
})
44+
);
45+
46+
zr.add(
47+
new zrender.Rect({
48+
shape: {
49+
x: 60,
50+
y: 50,
51+
width: 20,
52+
height: 100,
53+
},
54+
style: {
55+
fill: linearGradient2
56+
}
57+
})
58+
);
59+
60+
zr.add(
61+
new zrender.Rect({
62+
shape: {
63+
x: 100,
64+
y: 50,
65+
width: 20,
66+
height: 100,
67+
},
68+
style: {
69+
fill: linearGradient3
70+
}
71+
})
72+
);
73+
</script>
74+
</body>
75+
</html>

0 commit comments

Comments
 (0)