Book Image

WebGL Game Development

By : Sumeet Arora
Book Image

WebGL Game Development

By: Sumeet Arora

Overview of this book

<p>WebGL, the web implementation of Open GL, is a JavaScript API used to render interactive 3D graphics within any compatible web browser, without the need for plugins. It helps you create detailed, high-quality graphical 3D objects easily. WebGL elements can be mixed with other HTML elements and composites to create high-quality, interactive, creative, innovative graphical 3D objects.</p> <p>This book begins with collecting coins in Super Mario, killing soldiers in Contra, and then quickly evolves to working out strategies in World of Warcraft. You will be guided through creating animated characters, image processing, and adding effects as part of the web page canvas to the 2D/3D graphics. Pour life into your gaming characters and learn how to create special effects seen in the most powerful 3D games. Each chapter begins by showing you the underlying mathematics and its programmatic implementation, ending with the creation of a complete game scene to build a wonderful virtual world.</p>
Table of Contents (17 chapters)
WebGL Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Understanding canvas 2D basics and the drawing API


The 2D context provides objects, methods, and properties to draw and manipulate graphics on the drawing surface of a canvas. Refer to the link http://www.w3schools.com/html/html5_canvas.asp to learn more about canvas 2D. The following is some example code for the canvas element:

<html>
<canvas id="canvasElement"></canvas>
</html>
<script>
this.canvas = document.getElementById("canvasElement");
this.ctx = this.canvas.getContext('2d');
</script>

The canvas element has no drawing properties in itself and we use a script to draw graphics. The getContext method returns the object, which provides methods to draw text, lines, shapes, and graphics on the canvas.

The following code walks you through the methods of the 2D context object returned from the getContext method:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,10,10);

The fillstyle property will...