Canvas Anchor Points Using HTML 5

Introduction

 
In this article, we are going to understand the concept of Canvas Anchor Points Using HTML 5. In this section, you will see the anchor points of the quadratic curve and the Bezier curve in your browser.
 
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 AnchorPoints application can be created. To do so use the following steps.
 
Step 1 : Open a HTML editor or Visual Studio.
 
sd.gif
Open File menu ->select new ->Choose Website then.
 
0000.jpg
 
This is where we will create an HTML5 application.
  • 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 a new Webform
  • Rename it to AnchorPoints.aspx
anchor1.gif
 
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
  1. <style>  
  2. body  
  3. {  
  4.   margin0px;  
  5.   padding0px;  
  6.   font-family: Comic Sans MS;  
  7.   outline-color: Yellow;  
  8. }  
  9. Canvas  
  10. {  
  11.    border2px solid #9C9898;  
  12.    margin-top50px;  
  13.    margin-left60px;  
  14.    background-color#00B2EE;  
  15.    box-shadow: 5px 5px 8px #222;  
  16.  }  
  17. .title  
  18. {  
  19.    text-aligncenter;  
  20.    font-family: Segoe UI Light, ArialHelvetica;  
  21.    font-size2.2em;  
  22.    margin1em;  
  23. }  
  24. .info  
  25. {  
  26.    text-aligncenter;  
  27.    font-family: Segoe UI Light, ArialHelvetica;  
  28.    font-size1.2em;  
  29.    margin0.25em;  
  30. }  
  31. </style> 
Step 3 : In this part we need to work on some JavaScript. For fully understanding how the JavaScript works, download the attached .rar file and run the AnchorPoints application. The whole JavaScript looks as in the following.
  1. <script>  
  2.         function buildAnchor(curveLayer, anchorsLayer, x, y, anchor)  
  3.          {  
  4.            var anchor = new Kinetic.Circle({  
  5.                 x: x,  
  6.                 y: y,  
  7.                 radius: 5,  
  8.                 stroke: "#666",  
  9.               fill: "#ddd",  
  10.                 strokeWidth: 2,  
  11.                draggable: true  
  12.             });  
  13.             // set curve points on dragmove  
  14.             anchor.on("dragmove"function ()  
  15.             {  
  16.                 drawCurves(curveLayer);  
  17.             });  
  18.             // add hover styling  
  19.             anchor.on("mouseover"function ()  
  20.             {  
  21.                 document.body.style.cursor = "pointer";  
  22.                 this.setStrokeWidth(4);  
  23.                 anchorsLayer.draw();  
  24.             });  
  25.             anchor.on("mouseout"function ()  
  26.             {  
  27.                 document.body.style.cursor = "default";  
  28.                 this.setStrokeWidth(2);  
  29.                 anchorsLayer.draw();  
  30.             });  
  31.            anchorsLayer.add(anchor);  
  32.             return anchor;  
  33.         }  
  34.         function drawCurves(curveLayer)  
  35.          {  
  36.             var context = curveLayer.getContext();  
  37.             curveLayer.clear();     
  38.             var quad = curveLayer.quad;  
  39.             // draw curve  
  40.             context.beginPath();  
  41.             context.moveTo(quad.start.x, quad.start.y);  
  42.             context.lineTo(quad.control.x, quad.control.y);  
  43.             context.lineTo(quad.end.x, quad.end.y);  
  44.             context.strokeStyle = "#ccc";  
  45.             context.lineWidth = 2;  
  46.             context.stroke();  
  47.             context.closePath();  
  48.             // draw connectors  
  49.             context.beginPath();  
  50.             context.moveTo(quad.start.x, quad.start.y);  
  51.             context.quadraticCurveTo(quad.control.x, quad.control.y, quad.end.x, quad.end.y);  
  52.             context.strokeStyle = "red";  
  53.             context.lineWidth = 4;  
  54.             context.stroke();  
  55.             var bezier = curveLayer.bezier;  
  56.             // draw curve  
  57.             context.beginPath();  
  58.             context.moveTo(bezier.start.x, bezier.start.y);  
  59.             context.lineTo(bezier.control1.x, bezier.control1.y);  
  60.             context.lineTo(bezier.control2.x, bezier.control2.y);  
  61.             context.lineTo(bezier.end.x, bezier.end.y);  
  62.             context.strokeStyle = "#ccc";  
  63.             context.lineWidth = 2;  
  64.             context.stroke();  
  65.             context.closePath();  
  66.             // draw connectors  
  67.             context.beginPath();  
  68.             context.moveTo(bezier.start.x, bezier.start.y);  
  69.             context.bezierCurveTo(bezier.control1.x, bezier.control1.y, bezier.control2.x, bezier.control2.y, bezier.end.x, bezier.end.y);  
  70.             context.strokeStyle = "blue";  
  71.             context.lineWidth = 4;  
  72.             context.stroke();  
  73.         }  
  74.         window.onload = function ()  
  75.         {  
  76.             var stage = new Kinetic.Stage("container", 578, 200);  
  77.             var curveLayer = new Kinetic.Layer();  
  78.             var anchorsLayer = new Kinetic.Layer();  
  79.             curveLayer.quad = {  
  80.                 start: buildAnchor(curveLayer, anchorsLayer, 60, 30),  
  81.               control: buildAnchor(curveLayer, anchorsLayer, 240, 110),  
  82.                 end: buildAnchor(curveLayer, anchorsLayer, 80, 160)  
  83.             };  
  84.             curveLayer.bezier = {  
  85.                 start: buildAnchor(curveLayer, anchorsLayer, 280, 20),  
  86.                 control1: buildAnchor(curveLayer, anchorsLayer, 530, 40),  
  87.                 control2: buildAnchor(curveLayer, anchorsLayer, 480, 150),  
  88.                 end: buildAnchor(curveLayer, anchorsLayer, 300, 150)  
  89.             };  
  90.             stage.on("mouseout"function ()  
  91.             {  
  92.                 drawCurves(curveLayer);  
  93.                 anchorsLayer.draw();  
  94.             });  
  95.             stage.add(curveLayer);  
  96.             stage.add(anchorsLayer);  
  97.             drawCurves(curveLayer);  
  98.         };  
  99. </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 AnchorPoints.aspx page. Here we pass a Canvas in the canvas tag.
  1. <body style="background-color: #00CED1">  
  2.     <center>  
  3.         <h2>  
  4.             Anchor Points  
  5.         </h2>  
  6.     </center>  
  7.     <hr />  
  8.     <div id="container">  
  9.     </div>  
  10. </body> 
Step 5: The complete code for the AnchorPoints application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AnchorPoints.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <style>  
  6.     </style>  
  7.     <script src="jscript.js">  
  8.     </script>  
  9.     <script>  
  10.     </script>  
  11. </head>  
  12. <body style="background-color: #00CED1">  
  13.     <center>  
  14.         <h2>  
  15.             Anchor Points  
  16.         </h2>  
  17.     </center>  
  18.     <hr />  
  19.     <div id="container">  
  20.     </div>  
  21. </body>  
  22. </html> 
Step 6: Output Press F5 Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC. You will see the anchor points of the quadratic curve and the Bezier curve in your browser.
 
anchor2 
 
Here are some useful resources.