4

我们怎样才能做到这一点?

我在函数中得到了 SVG,我怎样才能使它在画布上透明?目前我的所有功能都在画布上工作。但我发现SVG可以做添加和删除功能。我该怎么办?

function Add() {
    var id = Math.floor(Math.random()*101+1);
        x = Math.random() * 550;
        y = Math.random() * 250;
    if (document.getElementById('amount').value < 50){
        document.getElementById('amount').value++;
        svg = document.getElementById("main");


        // construct uniqueid for the images
        uniqueid = "frog" + document.getElementById('amount').value;

        //namespaces for SVG
        svgNS="http://www.w3.org/2000/svg";
        xlinkNS="http://www.w3.org/1999/xlink";

        // create a image element
        image = document.createElementNS(svgNS, 'image');

        // set id and other attributes
        image.setAttributeNS(null, "id", uniqueid);
        image.setAttributeNS(xlinkNS, "href","jef-frog.gif");
        image.setAttributeNS(null, "x", x);
        image.setAttributeNS(null, "y", y);
        image.setAttributeNS(null, "width", "50");
        image.setAttributeNS(null, "height", "50");

        // append to svg
        svg.appendChild(image);

    } else {
        alert("we got 50");
    }
}
4

1 回答 1

3

假设您正在询问 SVG<image>元素的透明度,我很高兴地说它工作得很好:

演示:http: //jsfiddle.net/XBCEK/

演示截图

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xl="http://www.w3.org/1999/xlink">
  <image xl:href="http://phrogz.net/tmp/alphaball.png"
         x="20" y="30" width="128" height="128" />
  <image xl:href="http://phrogz.net/tmp/hand.gif"
         x="220" y="30" width="32" height="32" />
</svg>​

如果您将该 SVG 与以下 CSS 一起嵌入到页面中:

body { background:url(http://phrogz.net/tmp/grid.gif) }
svg  { background:rgba(255,0,0,0.3) /*…*/ }

…然后你会看到:

  • SVG 的背景默认是透明的。我们甚至可以提供低不透明度的颜色背景,让页面(网格)的背景显示出来。
  • 8 位透明度 PNG(球)和 1 位透明度 GIF(手)的背景允许 SVG/页面的背景正确地发光。​</li>
于 2012-05-17T22:38:46.043 回答