Introduction
In this section, the shape's position changes and then the layer is redrawn with each animation frame using the onFrame() method. The onFrame() method is passed a frame object which contains two properties of interest. The number of milliseconds that the animation has been running is in frame.time.
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 CanvasAnimatedPostion application can be created. To do so use the following steps.
Step 1 : Open a HTML editor or Visual Studio.


- 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 animatedpostioning.aspx

CSS Script
- <style>
- body
- {
- margin: 0px;
- padding: 0px;
- }
- Canvas
- {
- border: 2px solid #009999;
- margin-top: 50px;
- margin-left: 50px;
- background-color:#9C9898 ;
- 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 JavaScript works, download the attached .rar file and run the CanvasAnimatedPostion application.
The whole JavaScript looks as in the following.
- <script>
- window.onload = function ()
- {
- var stage = new Kinetic.Stage({
- container: "container",
- width: 578,
- height: 200
- });
- var layer = new Kinetic.Layer();
- var hexagon = new Kinetic.RegularPolygon({
- x: stage.width / 2,
- y: stage.height / 2,
- sides: 6,
- radius: 70,
- fill: "yellow",
- stroke: "black",
- strokeWidth: 4
- });
- layer.add(hexagon);
- stage.add(layer);
- var amplitude = 150;
- var period = 2000;
- // in ms
- var centerX = stage.width / 2;
- stage.onFrame(function (frame) {
- hexagon.x = amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerX;
- layer.draw();
- });
- stage.start();
- };
- </script>



Vinay SinghPosted Jun 20, 2016, 5:16 AM
Good one. Nice animation
Gowtham RajamanickamPosted Apr 11, 2015, 1:33 PM
good one
Manish SinghPosted Mar 28, 2012, 12:31 AM
Hi Deepak, It's really help for beginners. Thanks for sharing.....