【发布时间】:2018-05-27 14:44:40
【问题描述】:
我有一个在白色背景上绘制的 SVG 文件。
如果用户选择在黑色背景上显示,则所有呈现为黑色的路径都会消失。
是否可以将 SVG 文件中所有呈现为黑色的元素更改为呈现为白色?包括路径、文字等。
SVG 是在浏览器中查看的,所以如果这可以在 CSS 和 javascript 中完成,我想这样做。
抱歉:SVG 中有 256 种颜色。
我已经制作了一个测试svg如下:
<svg
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="fgc">
<stop stop-color="blue"/>
</linearGradient>
</defs>
<g id="layer1" >
<rect
style="stroke:url(#fgc);fill: none;"
id="rect10"
width="110"
height="75"
x="50"
y="55" />
</g>
</svg>
我从 HTML 页面调用它:
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="application/x-javascript">
var drg="";
var svgDoc="";
var svgItem="";
window.onload=function() {
drg = document.getElementById("drawing");
svgDoc = drg.contentDocument;
svgItem = svgDoc.getElementById("fgc");
console.log(svgItem.getAttribute("stop-color"));
svgItem.setAttribute("stop-color", "red");
console.log(svgItem.getAttribute("stop-color"));
};
</script>
</head>
<body>
<object id="drawing" data="drawing.svg" type="image/svg+xml" width="200" height="150"></object>
</body>
</html>
console.log(svgItem.getAttribute("stop-color")) 在 svgItem.setAttribute("stop-color", "red") 之前在浏览器控制台中打印“null”,之后打印“red” .
矩形呈现为蓝色。
显然在 window.onload 中将 'stop-color' 属性更改为红色将不起作用,因为 svg 图形已经加载。如何将我的图形设置为在加载前呈现红色?
另外,我希望 console.log(svgItem.getAttribute("stop-color")) 在更改之前给我“蓝色”,但它给了我“null”,这一定意味着我正在更改错误的属性。
【问题讨论】:
-
您能否进一步详细说明 SVG 要求?具体来说,调色板是仅限于白色还是存在其他颜色?