Advertisement
here2share

HTML5 Canvas Transparency

Apr 30th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.94 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML5 Canvas Transparency</title>
  5. </head>
  6. <body>
  7. <canvas id="MyCanvas" width="650" height="600"></canvas>
  8. <script>
  9. var canvas = document.getElementById("MyCanvas");
  10. if (canvas.getContext) // Test for support.
  11.   {          
  12.     var ctx = canvas.getContext("2d"); // Get the context to draw on.
  13.       for (var g = 0,x=0, y=25; g <= 10; g+=1, x+=25, y+=25)
  14.       {
  15.         ctx.globalAlpha = g/10;  
  16.         ctx.fillStyle = "red";
  17.         ctx.strokeStyle = "black";
  18.         ctx.fillRect(x, y, 200, 200);  // Create a filled rectangle.
  19.         ctx.strokeRect(x, y, 200, 200); // Put an outline around it
  20.         ctx.globalAlpha = 1;  
  21.         ctx.fillStyle = 'blue';
  22.         ctx.font = "14px Unknown Font, sans-serif";  
  23.         ctx.fillText(" Transparency Value "+g, 202+x, y+10);
  24.         ctx.globalAlpha = g/10;                        
  25.       }
  26.  }      
  27. </script>
  28. </body>
  29. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement