How can we make SVG transparent on canvas?

How can we achieve this?

I got SVG in a function, how can I make it transparent on top of the canvas ?? Currently, I have all of my functions working on canvas. But I found that SVG can perform the function of adding and removing. How can i do this?

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");
    }
}
+3
source share
1 answer

Assuming you ask a question about the transparency of SVG elements <image>, I'm glad to say that it works just fine:

Demo: http://jsfiddle.net/XBCEK/

Screenshot from demo

<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>

If you embed this SVG on the page along with the following CSS:

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

... then you will see that:

  • The SVG background is transparent by default. We can even provide a background color with low opacity, which allows the background of the page (grid) to show.
  • 8- PNG (), 1- GIF () SVG/.
+3

All Articles