IntroductionIn this article we are going to understand the concept of canvas reset transformations using HTML 5. In this section, to reset the HTML5 Canvas transformation matrix, we can use the setTransform() method to set the transformation matrix.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 ResetTransform 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.This is where we will create the HTML5 application.
Step 2 : In this section we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or between the <head>--</head> tags. Here the CSS is used for design purposes.CSS Script <style>body{ margin: 0px; padding: 0px;}myCanvas{ border: 2px solid #9C9898; margin-top: 50px; margin-left: 50px; background-color: #009999; 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 ResetTransform application.The whole JavaScript looks as in the following.<script> window.onload = function () { var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); var rectWidth = 150; var rectHeight = 75; // translate context to center of canvas context.translate(canvas.width / 2, canvas.height / 2); context.fillStyle = "yellow"; context.fillRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight); // Reset Transform // 1 0 0 // 0 1 0 // 0 0 1 // apply custom transform context.setTransform(1, 0, 0, 1, 0, 0); context.fillStyle = "brown"; context.fillRect(0, 0, rectWidth, rectHeight); };</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 Canvasresettransform.aspx page. Here we pass a Canvas in the canvas tag.<body style="background-color: #FFFFCC"> <center> <h2> Canvas Reset Transform </h2> </center> <hr /> <canvas id="myCanvas" width="500" height="200"> </canvas></body>Step 5 : The complete code for the ResetTransform application:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Canvasresettransform.aspx.cs" Inherits="ResetTransform.Canvasresettransform" %><!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: #FFFFCC"> <center> <h2> Canvas Reset Transform </h2> </center> <hr /> <canvas id="myCanvas" width="500" height="200"> </canvas></body></html>Step 6 : Output Press F5Note : For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC. You will see the canvas reset transform while displaying in the browser. Here are the some useful resources.Canvas Mirror Transform Using HTML 5Canvas Shear Transform Using HTML 5Canvas Anchor Points Using HTML 5Canvas Oscillation Animation Using HTML 5Canvas Shape Layering Using HTML 5
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: