HTML5 Canvas and text-border - Javascript Canvas

Javascript examples for Canvas:Text

Description

HTML5 Canvas and text-border

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){/*from   www  . jav a  2s . c  o  m*/
        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        // Load background
        var bg = new Image();
        bg.onload = function () {
            canvas.width = this.width;
            canvas.height = this.height;
            ctx.drawImage(this, 0, 0);
            process();
        };
        bg.src = "http://upload.robrowser.com/bg-test-text.jpg";
        function process() {
            var x = canvas.width / 2;
            var y = canvas.height / 2;
            ctx.font = "12px Arial";
            try1(x, y);
        }
        function try1(x, y) {
            multiShadow("HappyEnd", x, y, 0, -1, 0);
            multiShadow("HappyEnd", x, y, 0, 1, 0);
            multiShadow("HappyEnd", x, y, -1, 0, 0);
            multiShadow("HappyEnd", x, y, 1, 0, 0);
            ctx.fillStyle = "white";
            ctx.strokeStyle = "black";
            ctx.strokeText("HappyEnd", x, y);
            ctx.fillText("HappyEnd", x, y);
        }
        function multiShadow(text, x, y, offsetX, offsetY, blur) {
            ctx.textBaseline = "top";
            ctx.lineWidth = 1;
            ctx.shadowColor = '#000';
            ctx.shadowBlur = blur;
            ctx.shadowOffsetX = offsetX;
            ctx.shadowOffsetY = offsetY;
            ctx.fillStyle = "black";
            ctx.fillText("HappyEnd", x, y);
        }
    });

      </script> 
   </head> 
   <body> 
      <canvas id="canvas" width="300" height="300"></canvas>  
   </body>
</html>

Related Tutorials