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.
Open File menu ->select new ->Choose Website then.
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
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;
- font-family: Comic Sans MS;
- outline-color: Yellow;
- }
- 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. 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.
- <script>
- function buildAnchor(curveLayer, anchorsLayer, x, y, anchor)
- {
- var anchor = new Kinetic.Circle({
- x: x,
- y: y,
- radius: 5,
- stroke: "#666",
- fill: "#ddd",
- strokeWidth: 2,
- draggable: true
- });
-
- anchor.on("dragmove", function ()
- {
- drawCurves(curveLayer);
- });
-
- anchor.on("mouseover", function ()
- {
- document.body.style.cursor = "pointer";
- this.setStrokeWidth(4);
- anchorsLayer.draw();
- });
- anchor.on("mouseout", function ()
- {
- document.body.style.cursor = "default";
- this.setStrokeWidth(2);
- anchorsLayer.draw();
- });
- anchorsLayer.add(anchor);
- return anchor;
- }
- function drawCurves(curveLayer)
- {
- var context = curveLayer.getContext();
- curveLayer.clear();
- var quad = curveLayer.quad;
-
- context.beginPath();
- context.moveTo(quad.start.x, quad.start.y);
- context.lineTo(quad.control.x, quad.control.y);
- context.lineTo(quad.end.x, quad.end.y);
- context.strokeStyle = "#ccc";
- context.lineWidth = 2;
- context.stroke();
- context.closePath();
-
- context.beginPath();
- context.moveTo(quad.start.x, quad.start.y);
- context.quadraticCurveTo(quad.control.x, quad.control.y, quad.end.x, quad.end.y);
- context.strokeStyle = "red";
- context.lineWidth = 4;
- context.stroke();
- var bezier = curveLayer.bezier;
-
- context.beginPath();
- context.moveTo(bezier.start.x, bezier.start.y);
- context.lineTo(bezier.control1.x, bezier.control1.y);
- context.lineTo(bezier.control2.x, bezier.control2.y);
- context.lineTo(bezier.end.x, bezier.end.y);
- context.strokeStyle = "#ccc";
- context.lineWidth = 2;
- context.stroke();
- context.closePath();
-
- context.beginPath();
- context.moveTo(bezier.start.x, bezier.start.y);
- context.bezierCurveTo(bezier.control1.x, bezier.control1.y, bezier.control2.x, bezier.control2.y, bezier.end.x, bezier.end.y);
- context.strokeStyle = "blue";
- context.lineWidth = 4;
- context.stroke();
- }
- window.onload = function ()
- {
- var stage = new Kinetic.Stage("container", 578, 200);
- var curveLayer = new Kinetic.Layer();
- var anchorsLayer = new Kinetic.Layer();
- curveLayer.quad = {
- start: buildAnchor(curveLayer, anchorsLayer, 60, 30),
- control: buildAnchor(curveLayer, anchorsLayer, 240, 110),
- end: buildAnchor(curveLayer, anchorsLayer, 80, 160)
- };
- curveLayer.bezier = {
- start: buildAnchor(curveLayer, anchorsLayer, 280, 20),
- control1: buildAnchor(curveLayer, anchorsLayer, 530, 40),
- control2: buildAnchor(curveLayer, anchorsLayer, 480, 150),
- end: buildAnchor(curveLayer, anchorsLayer, 300, 150)
- };
- stage.on("mouseout", function ()
- {
- drawCurves(curveLayer);
- anchorsLayer.draw();
- });
- stage.add(curveLayer);
- stage.add(anchorsLayer);
- drawCurves(curveLayer);
- };
- </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.
- <body style="background-color: #00CED1">
- <center>
- <h2>
- Anchor Points
- </h2>
- </center>
- <hr />
- <div id="container">
- </div>
- </body>
Step 5: The complete code for the AnchorPoints application.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AnchorPoints.aspx.cs" Inherits="_Default" %>
- <!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 src="jscript.js">
- </script>
- <script>
- </script>
- </head>
- <body style="background-color: #00CED1">
- <center>
- <h2>
- Anchor Points
- </h2>
- </center>
- <hr />
- <div id="container">
- </div>
- </body>
- </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.
Here are some useful resources.