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

Drawing a circle


To draw a circle in the canvas, you need to add the following code in your <script></script> tags:

<script type="text/javascript">
  var c = document.getElementById("canvasTest");
  var canvasElement = c.getContext("2d");
  canvasElement.beginPath();
  canvasElement.arc(95,50,40,0,2*Math.PI);
  canvasElement.stroke();
</script>

Here, we used canvasElement.beginPath(); to start drawing the circle, canvasElement.arc(95,50,40,0,2*Math.PI); for the shape of the circle, and canvasElement.stroke(); to set the circle visible.

Note

The canvasElement.arc(95,50,40,0,2*Math.PI); statement is similar to canvasElement.arc(x, y, r, sA, eA, ac);,

where x is the starting coordinate from x axis, y is the starting coordinate from y axis, r is the radius of the circle, sA is the starting angle of the circle, eA is the ending angle of the circle, and ac is the direction of the circle. Here, ac denotes anticlockwise.

The output of our code will be the following image: