Introduction
In this article, we are going to understand canvas mirror transformations using HTML 5. In this section, we will describe the creation of a mirror transform using HTML5 Canvas; we can apply a negative scaling in either the x or y direction, depending on whether we want to mirror the context horizontally or vertically, respectively.
Here we will use some JavaScript and some styles along with HTML code. Just go through the steps to see how to create this application.
Let's see how the CanvasMirrorTransform application can be created. To do so use the following steps.
Step 1 : Open a HTML editor or Visual Studio.
Open File menu ->select new ->Choose Website then.


- Go to Solution Explorer
- Right-click on the Application name
- Select Add-->add new item
- Now in the window that opens, select an HTML page or new Webform
- Rename it to mirrortransform.aspx

CSS Script
- <style>
- body
- {
- margin: 0px;
- padding: 0px;
- }
- Canvas
- {
- border: 2px solid #9C9898;
- margin-top: 50px;
- margin-left: 60px;
- background-color: #00B2EE;
- box-shadow: 5px 5px 8px #222;
- }
- .title
- {
- text-align: center;
- font-family: Segoe UI Light, Arial, Helvetica;
- font-size: 2.2em;
- margin: 1em;
- }
- .info
- {
- text-align: center;
- font-family: Segoe UI Light, Arial, Helvetica;
- font-size: 1.2em;
- margin: 0.25em;
- }
- </style>
Step 3 : In this part we need to work on some JavaScript. To fully understand how the JavaScript works, download the attached .rar file and run the CanvasMirrorTransform application.
The whole JavaScript looks as in the following:
- <script>
- window.onload = function ()
- {
- var canvas = document.getElementById("myCanvas");
- var context = canvas.getContext("2d");
- // 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 = "GREEN";
- context.fillText("C-SHARP CORNER ROCKS !", 0, 0);
- };
- </script>
Step 4 : In this section, we are going to become familiar with the body part of HTML scripting. Replace this script from the body section of the mirrortransform.aspx page. Here we pass a Canvas in the canvas tag.
- <body style="background-color: #FFCCCC">
- <center>
- <h1>
- Canvas Mirror Transform
- </h1>
- </center>
- <hr />
- <canvas id="myCanvas" width="550" height="200">
- </canvas>
- </body>
Step 5 : The complete code for the CanvasMirrorTransform application:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="mirrortransform.aspx.cs" Inherits="CanvasMirrorTransform.WebForm1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <style>
- </style>
- <script>
- </script>
- </head>
- <body style="background-color: #FFCCCC">
- <center>
- <h1>
- Canvas Mirror Transform
- </h1>
- </center>
- <hr />
- <canvas id="myCanvas" width="550" height="200">
- </canvas>
- </body>
- </html>
Step 6: Output Press F5
Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. You will see the canvas mirror transform while displaying in the browser.


Santhakumar MunuswamyPosted Apr 11, 2015, 2:48 PM
thanks for nice article
Santhakumar MunuswamyPosted Apr 11, 2015, 2:48 PM
good work
Gowtham RajamanickamPosted Apr 11, 2015, 1:33 PM
great