Book Image

Canvas Cookbook

Book Image

Canvas Cookbook

Overview of this book

With the growing popularity of HTML5 Canvas, this book offers tailored recipes to help you develop portable applications, presentations, and games. The recipes are simple yet creative and build on each other. At every step, the book inspires the reader to develop his/her own recipe. From basic to advanced, every aspect of Canvas API has been covered to guide readers to develop their own application, presentation, or game.
Table of Contents (16 chapters)
Canvas Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Drawing a pie chart


A share can be easily represented in form of a pie chart. This recipe demonstrates a pie chart:

How to do it…

The HTML code is as follows:

<html>
<head>
  <title>A simple Pie chart</title>
  <script src="piechart.js"></script>
</head>
<body onload=init()>
  <h1>Your WhatsApp Usage</h1>
  <canvas width="600" height="500" id="MyCanvasArea" style="border:2px solid blue;" tabindex="0">
    Canvas tag is not supported by your browser
  </canvas>
</body>
</html>

The JavaScript code is as follows:

function init()
{
  var can = document.getElementById('MyCanvasArea');
  var ctx = can.getContext('2d');
  var data = [130,140,150,140,170,250,340];
  var colors = ["crimson", "blue", "yellow", "navy", "aqua", "purple","red"];
  var names=["MON","TUE","WED","THU","FRI","SAT","SUN"];
  var centerX=can.width/2;
  var centerY=can.height/2;
  //var center = [can.width/2,can.height / 2];
  var radius = (Math...