我为 SVG 图像创建了一个投影,使用纯 SVG 效果很好(参见现场演示):
<!DOCTYPE html>
<html>
<body>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="drop">
<feComponenttransfer result="offOut" in="SourceAlpha">
<feFuncA type="linear" />
<feFuncR type="discrete" tablevalues="1" />
<feFuncG type="discrete" />
<feFuncB type="discrete" />
</feComponentTransfer>
<feOffset result="shadow" dx="67" dy="0" in="offOut" />
<feGaussianBlur stdDeviation="8" result="blurOut" in="shadow" />
<feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
</filter>
</defs>
<g>
<g filter="url(#drop)">
<image x="10" y="100" width="440" height="60" xlink:href="/images/pulpit.jpg"
preserveAspectRatio="defer"></image>
</g>
</g>
</svg>
</body>
</html>
正如预期的那样,这里的图像是用红色阴影创建的,但我尝试使用以下代码通过 JavaScript 创建和修改 SVG(参见现场演示):
<!DOCTYPE html>
<html>
<body>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="sv">
<rect width="90" height="90" stroke="green" stroke-width="3" fill="rgba(255,25,8,1)"
/>
<g id="gr">
<image x="10" y="100" width="440" height="60" xlink:href="/images/pulpit.jpg"
preserveAspectRatio="defer"></image>
</g>
</svg>
</body>
<script>
var feoffset = document.createElementNS("http://www.w3.org/2000/svg", "feOffset");
var feGaussianBlur = document.createElementNS("http://www.w3.org/2000/svg", "feGaussianBlur");
var filter = document.createElementNS("http://www.w3.org/2000/svg", "filter");
var definition = document.createElementNS("http://www.w3.org/2000/svg", "defs");
filter.setAttribute("id", "fPicture5");
filter.setAttribute("filterUnits", "userSpaceOnUse");
feoffset.setAttribute("result", "shadow");
feoffset.setAttribute("in", "offOut");
feoffset.setAttribute("dx", "67");
feoffset.setAttribute("dy", "0");
feGaussianBlur.setAttribute("result", "blurOut");
feGaussianBlur.setAttribute("in", "shadow");
feGaussianBlur.setAttribute("stdDeviation", "8");
var feBlend = document.createElementNS("http://www.w3.org/2000/svg", "feBlend");
var feCTransfer = document.createElementNS("http://www.w3.org/2000/svg", "feComponentTransfer");
var feFuncR = document.createElementNS("http://www.w3.org/2000/svg", "feFuncR");
var feFuncG = document.createElementNS("http://www.w3.org/2000/svg", "feFuncG");
var feFuncB = document.createElementNS("http://www.w3.org/2000/svg", "feFuncB");
feCTransfer.setAttribute("in", "SourceAlpha");
feCTransfer.setAttribute("result", "offOut");
feBlend.setAttribute("in", "SourceGraphic");
feBlend.setAttribute("in2", "blurOut");
feBlend.setAttribute("mode", "normal");
feFuncR.setAttribute("type", "discrete");
feFuncG.setAttribute("type", "discrete");
feFuncB.setAttribute("type", "discrete");
feFuncR.setAttribute("tablevalues", "1");
feFuncG.setAttribute("tablevalues", "0");
feFuncB.setAttribute("tablevalues", "0");
feCTransfer.appendChild(feFuncR);
feCTransfer.appendChild(feFuncG);
feCTransfer.appendChild(feFuncB);
filter.appendChild(feCTransfer);
filter.appendChild(feoffset);
filter.appendChild(feGaussianBlur);
filter.appendChild(feBlend);
definition.appendChild(filter);
document.getElementById("sv").appendChild(definition);
document.getElementById("gr").setAttribute("filter", "url(#fPicture5)");
</script>
</html>
结果是图像创建成功,但是阴影是黑色的。
我找不到办法让它按预期变红,有什么想法吗?