HTML Canvas transform() create custom transform

Description

HTML Canvas transform() create custom transform

View in separate window

<html>
    <head>
        <script> 
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
                /*  w ww.  ja  v  a 2  s. c o m*/
                var rectWidth = 150;
                var rectHeight = 75;
                
                // translation matrix:
                //  1  0  tx              
                //  0  1  ty
                //  0  0  1  
                var tx = canvas.width / 2;
                var ty = canvas.height / 2;
                
                // apply custom transform
                context.transform(1, 0, 0, 1, tx, ty);
                
                context.fillStyle = "blue";
                context.fillRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight);
            };
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
        </canvas>
    </body>
</html>



PreviousNext

Related