Book Image

HTML5 Web Application Development By Example : Beginner's guide

By : Jody Gustafson
Book Image

HTML5 Web Application Development By Example : Beginner's guide

By: Jody Gustafson

Overview of this book

HTML5's new features have made it a real application development platform with widespread adoption throughout the industry for this purpose. Being able to create one application that can run on virtually any device from phone to desktop has made it the first choice among developers. Although JavaScript has been around for a while now, it wasn't until the introduction of HTML5 that we have been able to create dynamic, feature-rich applications rivaling those written for the desktop. HTML5 Web Application Development By Example will give you the knowledge you need to build rich, interactive web applications from the ground up, incorporating the most popular HTML5 and CSS3 features available right now. This book is full of tips, tools, and example applications that will get you started writing your own applications today. HTML5 Web Application Development By Example shows you how to write web applications using the most popular HTML5 and CSS3 features. This book is a practical, hands-on guide with numerous real-world and relevant examples. You will learn how to use local storage to save an application's state and incorporate CSS3 to make it look great. You will also learn how to use custom data attributes to implement data binding. We'll use the new Canvas API to create a drawing application, then use the Audio API to create a virtual piano, before turning it all into a game. The time to start using HTML5 is now. And HTML5 Web Application Development by Example will give you the tips and know-how to get started.
Table of Contents (18 chapters)
HTML5 Web Application Development By Example Beginner's guide
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

The time to start using HTML5 is now. HTML5 provides a complete application development framework for writing full featured applications that run in the web browser. Even though the HTML5 specification hasn't been fully completed yet, the most popular features are already widely supported by nearly every modern browser running on devices, from desktops to tablets to smartphones. That means you can write an application once and have it run on nearly any device.

If you are looking to start writing HTML5 web applications but don't know where to start, then this book is for you. We will start with the basics of building a web application and then learn about HTML5, CSS3, and JavaScript by building real working applications. This is not a reference book. We will keep the theory to a minimum and hands-on coding to a maximum.

Just a few years ago, writing full-featured applications in the browser required other technologies such as Flash or Java Applets that ran as browser plugins. Like most people, I had only used JavaScript to write simple client validation scripts. I didn't even think it was possible to write real applications using JavaScript. That all started to change when a couple of things happened.

Firstly, I discovered jQuery. Here was a library that made writing JavaScript a whole lot easier by abstracting away browser idiosyncrasies and making it very easy to manipulate the elements of a web page. Plus it could help us perform some cool actions, such as animating elements. Then about three years ago I found out about HTML5 while looking for a way to draw graphics primitives directly onto a web page. Since then I've watched HTML5 develop into a complete framework, capable of being used to write real applications without plugins.

This book is the culmination of those past three years of writing JavaScript nearly every day, learning what works and what doesn't. A technical brain dump, if you will. The objective was to write the book that I would have liked to read when I started out.

The future of HTML5 web application development looks bright. All of the big hitters in the world of web browser development are putting their full support behind HTML5 and JavaScript. HTML5 is the future of web application development!

What this book covers

Chapter 1, The Task at Hand, will teach you the basic components of a JavaScript application by building a template that can be used to start writing new applications. Then we will create a tasklist application where we will learn how to manipulate the DOM and how to use HTML5 Web Storage to save the state of the application.

Chapter 2, Let's Get Stylish, will show how to use the new CSS3 features to add professional-looking styles to your web applications including rounded corners, shadows, and gradients. We will also learn how to use CSS sprites to make loading images more efficient.

Chapter 3, The Devil is in the Details, will teach you about the new HTML5 form input types by adding a details section to the tasklist application. We will also learn about custom data attributes and learn how to use them to bind data in the view to the model.

Chapter 4, A Blank Canvas, will show how to use the new Canvas element and API to draw directly onto a web page by creating a drawing application. We will also learn how to handle touch events from touch-screen devices.

Chapter 5, Not So Blank Canvas, will continue teaching about the canvas by showing you how to export images from a canvas and load images into a canvas using the new File API. Then we will get down to the pixel level and learn how to directly manipulate canvas image data.

Chapter 6, Piano Man, will teach you how to use the Audio element and API to play sounds in a web page. We will create a virtual piano that plays sounds when the keys are clicked.

Chapter 7, Piano Hero, will take the virtual piano from the previous chapter and turn it into a game where the player must play the correct notes of a song at the correct time to get points. In the process we will learn about using JavaScript timers and animating elements.

Chapter 8, A Change in the Weather, will show you how to get data from servers and talk to web services using Ajax. We will build a weather widget that gets the user's location using the Geolocation API and displays their local weather report with data from a web service.

Chapter 9, Web Workers Unite, will teach you how to use HTML5 web workers to perform long running processes in a separate thread to make your applications more responsive. We will create an application that draws Mandelbrot fractals onto a canvas using a web worker.

Chapter 10, Releasing an App into the Wild, will teach you how to use a JavaScript compressor to combine and compress your application's JavaScript files before releasing it to the world. We will also learn how to create applications that can be used offline by using the HTML5 Application Cache.

What you need for this book

The great thing about HTML5 is that there is no cost to use it. You don't need any special tools or licenses to develop web applications. However, using a good code editor will help you a lot, especially as you get started. There's nothing like autosuggest to help you remember JavaScript functions, element names, and styling options. And syntax highlighting is essential for making it easier to read code.

That said, there are a few source code editors that I can suggest if you don't already have one. Notepad++ is a free editor with JavaScript, HTML, and CSS syntax highlighting and some basic autosuggest, without a lot of overhead. I used it to write all of the code in this book. On the higher end, Microsoft Visual Studio provides very good autosuggest features but with more overhead than a basic text editor. Another great option is NetBeans, an open source IDE written in Java with good web development support.

You will also need a web browser with good HTML5 support and developer tools. The latest versions of most browsers support the HTML5 features used in this book. The browser you use should depend on which has the developer tools you like most. I use Chrome because it has great developer tools built-in. Firefox with the Firebug plugin installed is also very good. For this book I use Chrome as the browser of choice. Internet Explorer 9 doesn't completely support all of the HTML5 features we will be learning and the developer tools are not as good as the others, so I would stay away from using it for development.

The one other thing you may need is a web server such as IIS or Apache. Most of the time you can open your web application right from the filesystem when in development. However, some HTML5 features will only work through a web server. I have noted the places in this book where that is the case.

Who this book is for

This book is for the programmer who has experience in other languages and wants to start writing HTML5 web applications. You should have some basic knowledge of HTML, CSS, and JavaScript. For example, you should know how to write simple HTML documents. You should also know the basics of using CSS selectors as they are important to using jQuery. You do not need to know how to use jQuery as this book will briefly cover the basics, but it would be helpful. As long as you can understand and write simple JavaScript code, that should be sufficient to get you going. We will start off with the basics and work our way up using lots of examples.

Conventions

In this book, you will find several headings appearing frequently.

To give clear instructions of how to complete a procedure or task, we use:

Time for action – heading

  1. Action 1

  2. Action 2

  3. Action 3

Instructions often need some extra explanation so that they make sense, so they are followed with:

What just happened?

This heading explains the working of tasks or instructions that you have just completed.

You will also find some other learning aids in the book, including:

Pop quiz – heading

These are short multiple-choice questions intended to help you test your own understanding.

Have a go hero – heading

These practical challenges give you ideas for experimenting with what you have learned.

You will also find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Next, we'll add a drawText() method to the Canvas2D object."

A block of code is set as follows:

this.drawText = function(text, point, fill)
{
    if (fill)
    {
        context.fillText(text, point.x, point.y);
    }
    else
    {
        context.strokeText(text, point.x, point.y);
    }
};

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

switch (action.tool)
{
    // code not shown...
    case "text":
        canvas2d.drawText(action.text, action.points[0],
            action.fill);
        break;
}

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "When the Save button is clicked, it will get the data URL and then open it."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.