Canvas How to - Draw text on black background








Question

We would like to know how to draw text on black background.

Answer


<!--from   www.  ja  va  2 s  .co  m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        ctx.save();
        ctx.beginPath();
        ctx.fillStyle = "black";
        ctx.rect(0, 0, canvas.width, canvas.height);
        ctx.fill();
        ctx.font = "72pt Verdana";
        ctx.globalCompositeOperation = "xor";
        ctx.beginPath();
        ctx.fillText("java2s.com", 30, 200);
        ctx.fill();
        ctx.restore();
}//]]>  
</script>
</head>
<body>
  <canvas id="canvas" width=400 height=300></canvas>
</body>
</html>

The code above is rendered as follows: