Creating 2-D Animations on Canvas

In this tutorial, you will:

  • Learn to create and amazingly simple, yet powerful and exciting game.
  • Play it as a challenge with your loved ones.
Finding a good book to master HTML5 can be very challenging: there are so many around - most with eye-catching titles and very complex substance.
Therefore, Vivax Solutions strongly recommends Core HTML5 Canvas for those who really want to delve into HTML5.
Please click the image to access Amazon:

Interactive Coin Game

In this game, you can drag and drop a coin at three different places to invert a triangle. The movement is restricted to just three moves.

The element, img, is made draggable by making the property true.

Then onDrag(), onDropOver() and onDrop() events are manipulated to produce the desired effect.

Since the game allows a user to make three attempts, after dragging a coin three times, the programme holts - until the page is refreshed to make another attempt.





The Source Code for Interactive Coin Game is as follows:

<script>
// global variable to store the number of dragging
h = 0;
// invoked above the place where coin is dropped - just before the event
function allowDrop(ev) {
// blocking the default event - inability to drop
ev.preventDefault();
}
// invoked when dragging starts
function drag(ev) {
// stores the data of the object being dragged
ev.dataTransfer.setData("Text", ev.target.id);
}
// invoked when dropping the image
function drop(ev) {
h = h + 1;
if (h <= 3) {
ev.preventDefault();
// getting the data of the object to be dropped
var data = ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
else {
alert('Only 3 moves allowed; refresh the page for a new attempt.');
}
}
</script>

 

All Canvas Animations

 

 

 

Recommended Reading

Amazon Best Seller

 

Advertisements

Amazon Best Seller

Advertisement