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

Converting canvas to image and back to canvas


The output of this recipe may not be attractive, but the recipe is definitely useful. Whatever you draw on the canvas can be saved as an image:

How to do it

The recipe code is as follows:

The HTML code:

<html>
<head>
<title>Image</title>
<style>
img {
border: 1px solid black;
}
</style>
<script src="CanvasToImageAndBackToCanvas.js"></script>
</head>
<body onload="init();">
<center>
<canvas id="MyCanvasArea1" width="100" height="100" style="border:2px solid blue;" >
your browser does not support canvas
</canvas>
<h1>Drawing in canvas 1.</h1>
<br><br>
<imgsrc="" id="myimg" alt="here goes the image"><br><br>
<h1>Image saved from canvas 1 and assigned to image element</h1>
<canvas id="MyCanvasArea2" width="100" height="100" style="border:2px solid green;" >
your browser does not support canvas
</canvas>
<h1...