Here I’m drawing a circle with a red fill and incrementing it’s x-axis value each time it is called.
JavaScript file: x-axis movement control myScript.js
1
2
3
4
5
var r = 50;
var x = -r;
if( x >= context.canvas.width + r)
x = -r;
var r = 50;
var x = -r;
if( x >= context.canvas.width + r) x = -r;
Here the x-axis value is tracked each time and is set back to -50 ( minus radius of the circle ) once the circle moves out of the x-axis or the canvas width.
window.onload = canvas;
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
if( myCanvas && myCanvas.getContext("2d") )
{
var context = myCanvas.getContext("2d");
var r = 50;
var x = -r;
setInterval(function(){
context.fillStyle = "#000";
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
context.fillStyle = "red";
context.arc(x++, context.canvas.height/2, r, 0, 2 * Math.PI);
context.fill();
if( x >= context.canvas.width + r)
x = -r;
}, 10);
}
}
window.onload = canvas;
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
if( myCanvas && myCanvas.getContext("2d") )
{
var context = myCanvas.getContext("2d");
var r = 50;
var x = -r;
setInterval(function(){
context.fillStyle = "#000";
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
context.fillStyle = "red";
context.arc(x++, context.canvas.height/2, r, 0, 2 * Math.PI);
context.fill();
if( x >= context.canvas.width + r) x = -r;
}, 10);
}
}
Here we use the setInterval() method to iterate/loop through the anonymous function and each time the background of the canvas is set to black(#000 or #000000) by drawing a black rectangle across the entire canvas width and height. For every call the value of x increments by 1, hence the circle moves 1px away from its previous position, until it reaches the end of canvas – after which it is reset back to -r value.
Today lets learn about rotate transformation. And also lets see how we can use translate transformation and rotate transformation to create simple animation.
Syntax rotate(angleInRadians); The rotate() transformation causes the subsequent drawing operations to be rotated by a given angle. Note that the angles are in radian and not in degrees. Also the rotation takes place around the current origin and not with respect to the center of the object or the element drawn.
Here we take 2 rectangles with same x and y axis and same width and height. For second rectangle we apply a rotation of 0.5 radians. With this you can see that the rotation takes place with respect to the origin of the canvas, which is (0, 0).
JavaScript file: Rotate Transformation On a Circle myScript.js
Here we draw a radialgradient and rotate it with 2 radians with a time interval of 50 milliseconds. Before we rotate the circle we change the origin of the canvas to 175px x-axis and 175px y-axis, so that the rotation takes place with respect to the point (175, 175).
Rotate Transformation and Animation in Canvas: HTML5
Note: We convert degree to radian using a simple mathematical formula: Radian = (PI / 180 ) * degree;
While developing moving objects in our applications(like online games applications), we need not remember lot of locations/points on the canvas to move our element, instead, we can change the origin of the canvas using translate transformation and move the elements or rotate them!
Video tutorial to illustrate animation of text and image using jQuery.
In this tutorial, we accomplish: Animation of text: using animate method. Display of hidden image: using fadeIn method. Toggle of image: using slideToggle method. Change the color of the text being animated: using jQuery’s css method.
Here we have 4 buttons, 1 div tag which contains some string in it, and 1 image tag. Each button has its own unique ID, which is referenced to track user click event.
Once the #top button is clicked, the string inside div tag(which has move as its id), moves upwards leaving 30px from top of the document and 200px from left.
Once the #down button is clicked, the string inside div tag(which has move as its id), moves down leaving 200px from top of the document and 200px from left.
Once the #blue button is clicked, the color of string inside div tag(which has move as its id), changes to blue. .css() method takes 2 parameters, first one being css property and the second one its value.
[If you are used to old stay table syntax, then it’s completely OK to use this CSS Hover over technique on it. It’s not mandatory to use HTML4. ] In HTML4, table mainly contains 3 parts. thead, tfoot and tbody.
thead represents the heading and the tfoot, the footer of the table. tbody represents the body except the head and the foot of the table. In thead, I have taken “Apple”, “Microsoft” and “Google”. In tfoot and tbody some random numbers for the purpose of illustration.
Just don’t try worry about these numbers, instead concentrate on the structure of the table.
Cascading Style Sheet Here we apply all the styling property to the HTML file.
Make sure you try this :hover pseudo class with other html elements too. You can create very good user interface with the help of this simple CSS hover over effect.