Book Image

jQuery Mobile Cookbook

By : Chetan Jain
Book Image

jQuery Mobile Cookbook

By: Chetan Jain

Overview of this book

jQuery Mobile is an award winning, HTML5/CSS3 based open source cross-platform UI framework. It offers a very cool and highly customizable UX. It is built on the popular jQuery library and uses declarative coding making it easy to use and learn. It is the market leader today considering the numerous browsers and platforms that it supports."jQuery Mobile Cookbook" presents over a hundred recipes written in a simple and easy manner. You can quickly learn and start writing code immediately. Advanced topics such as using scripts to manipulate, customize, and extend the framework are also covered. These tips address your common everyday problems. The book is very handy for both beginner and experienced jQuery Mobile developers.You start by developing simple apps using various controls and learn to customize them. Later you explore using advanced aspects like configurations, events, and methods.Develop single and multi-page applications. Use caching to boost performance. Use custom transitions, icon sprites, styles, and themes. Learn advanced features like configurations, events, and methods. Explore future trends by using HTML5 new features and semantics with jQuery Mobile."jQuery Mobile Cookbook" is an easy read and is packed with practical tips and screenshots.
Table of Contents (19 chapters)
jQuery Mobile Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

2D drawing with Canvas


Canvas is a rectangular region in your web page where you can draw 2D shapes using JavaScript, and also render bitmap images. It is used for graphs, animations, images, photo compositions, real-time video processing, and games. This recipe shows you how to use the Canvas in your jQuery Mobile app.

Getting ready

Copy the full code of this recipe from the code/11/canvas folder. You can launch this code using the URL: http://localhost:8080/11/canvas/main.html.

How to do it...

The steps to be followed are:

  1. Create main.html with its page content having a canvas element:

    <div data-role='content'>
      <canvas id="myCanvas" width="500" height="500">
        Canvas is not supported on your browser
      </canvas>
    </div>
  2. Add the following script to get the 2D context, and then draw two rectangles:

    $('#main').live('pageinit', function(event) {
      var cxt = $('#myCanvas')[0].getContext("2d");
      cxt.fillStyle = '#5f98c5';
      cxt.fillRect(20,20, 100, 100);
      cxt.strokeRect...