Powerful Apis for Richer Internet Application Development
Total Page:16
File Type:pdf, Size:1020Kb
THE EXPERT’S VOICE® IN WEB DEVELOPMENT Pro Pro HTML5 Programming Powerful APIs for Richer Internet Application Development Use HTML5 to create cutting-edge web applications SOURCE CODE ONLINE Peter Lubbers, Brian Albers, and Frank Salim Foreword by Paul Irish, Google Pro HTML5 Programming Powerful APIs for Richer Internet Application Development ■ ■ ■ PETER LUBBERS BRIAN ALBERS FRANK SALIM Pro HTML5 Programming: Powerful APIs for Richer Internet Application Development Copyright © 2010 by Peter Lubbers, Brian Albers, Frank Salim All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2790-8 ISBN-13 (electronic): 978-1-4302-2791-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Clay Andres Development Editor: Matthew Moodie Technical Reviewer: Paul Haine Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Laurin Becker Copy Editors: Heather Lang, Andy Rosenthal, Nancy Sixsmith Compositor: Kimberly Burton Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.prohtml5.com and also at www.apress.com. ii For my beautiful wife, Vittoria, and for my sons—Sean and Rocky. I am so proud of you! —Peter Lubbers For John. You make it all worthwhile. —Brian Albers For people who still read books. —Frank Salim Contents at a Glance ■Foreword ..................................................................................................................xiii ■About the Authors..................................................................................................... xiv ■About the Technical Reviewer................................................................................... xv ■Acknowledgements .................................................................................................. xvi ■Introduction............................................................................................................. xvii ■Chapter 1: Overview of HTML5 ....................................................................................1 ■Chapter 2: Using the HTML5 Canvas API ...................................................................25 ■Chapter 3: Working with HTML5 Audio and Video....................................................65 ■Chapter 4: Using the HTML5 Geolocation API ...........................................................87 ■Chapter 5: Using the Communication APIs..............................................................115 ■Chapter 6: Using the HTML5 WebSocket API ..........................................................137 ■Chapter 7: Using the HTML5 Forms API...................................................................169 ■Chapter 8: Using the HTML5 Web Workers API.......................................................193 ■Chapter 9: Using the HTML5 Web Storage API........................................................213 ■Chapter 10: Creating HTML5 Offline Web Applications............................................243 ■Chapter 11: The Future of HTML5 ............................................................................259 ■Index........................................................................................................................269 iv Contents ■Foreword ..................................................................................................................xiii ■About the Authors..................................................................................................... xiv ■About the Technical Reviewer................................................................................... xv ■Acknowledgements .................................................................................................. xvi ■Introduction............................................................................................................. xvii ■Chapter 1: Overview of HTML5 ....................................................................................1 The Story So Far—The History of HTML5..........................................................................1 The Myth of 2022 and Why It Doesn’t Matter ....................................................................2 Who Is Developing HTML5? ...............................................................................................3 A New Vision......................................................................................................................3 Compatibility and Paving the Cow Paths.................................................................................................. 3 Utility and the Priority of Constituencies .................................................................................................. 4 Interoperability Simplification .................................................................................................................. 5 Universal Access ...................................................................................................................................... 5 A Plugin–Free Paradigm ....................................................................................................5 What’s In and What’s Out? ....................................................................................................................... 6 What’s New in HTML5?......................................................................................................8 New DOCTYPE and Character Set............................................................................................................. 8 New and Deprecated Elements ................................................................................................................ 9 Semantic Markup ................................................................................................................................... 10 Simplifying Selection Using the Selectors API........................................................................................ 17 JavaScript Logging and Debugging........................................................................................................ 20 window.JSON ......................................................................................................................................... 21 v ■ CONTENTS DOM Level 3 ........................................................................................................................................... 22 Monkeys, Squirrelfish, and Other Speedy Oddities ................................................................................ 22 Summary .........................................................................................................................23 ■Chapter 2: Using the HTML5 Canvas API ...............................................................25 Overview of HTML5 Canvas .............................................................................................25 History .................................................................................................................................................... 25 What Is a Canvas? .................................................................................................................................. 26 Canvas Coordinates................................................................................................................................ 26 When Not to Use Canvas .......................................................................................................................