HTML Canvas scale() mirror canvas

Introduction

To create a mirror transform, assign a negative value to sx or sy for scale() method of the canvas context:

context.scale(-sx,-sy); 

View in separate window

<html>
    <head>
        <script> 
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
                // w  w  w . j  av a  2 s .  c o  m
                // translate context to center of canvas
                context.translate(canvas.width / 2, canvas.height / 2);
                
                // flip context horizontally
                context.scale(-1, 1);
                
                context.font = "30pt Calibri";
                context.textAlign = "center";
                context.fillStyle = "blue";
                context.fillText("Hello World!", 0, 0);
            };
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
        </canvas>
    </body>
</html>



PreviousNext

Related