Book Image

JavaScript Projects for Kids

By : Syed Omar Faruk Towaha
Book Image

JavaScript Projects for Kids

By: Syed Omar Faruk Towaha

Overview of this book

JavaScript is the most widely-used programming language for web development and that's not all! It has evolved over the years and is now being implemented in an array of environments from websites to robotics. Learning JavaScript will help you see the broader picture of web development. This book will take your imagination to new heights by teaching you how to work with JavaScript from scratch. It will introduce you to HTML and CSS to enhance the appearance of your applications. You’ll then use your skills to build on a cool Battleship game! From there, the book will introduce you to jQuery and show you how you can manipulate the DOM. You’ll get to play with some cool stuff using Canvas and will learn how to make use of Canvas to build a game on the lines of Pacman, only a whole lot cooler! Finally, it will show you a few tricks with OOP to make your code clean and will end with a few road maps on areas you can explore further.
Table of Contents (17 chapters)
JavaScript Projects for Kids
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Let's make a clock!


We are going to draw an analog clock and make it work as a real clock. In your body part of the HTML document, type the following code:

<canvas id="myclock" height="500" width="500"></canvas>
In your <script></script> tags, take the following variables:
Var canvas; // the clock canvas
var canvasElement; // canvas's elements

// clock settings
var cX = 0; 
var cY = 0;
var radius = 150;

Here, cX and cY are the center coordinates of our clock. We took 150 px as the clock's radius. You can increase or decrease it.

Then, we need to initialize the variables. Make an init(); function after defining the preceding variables.

The function should look similar to the following:

function init() {

  canvas = document.getElementById("myclock");
  //Called the element to work on. 
  canvasElement = canvas.getContext("2d");
  //Made the context 2d. 

  cX = canvas.width / 2;
  // we divided by two to get the middle point of X-axis
  cY = canvas.height / 2;
  // we...