HTML5 and JavaScript Projects
HTML5 and JavaScript Projects
HTML5 and JavaScript Projects
Projects
Build on your Basic Knowledge of
HTML5 and JavaScript to Create
Substantial HTML5 Applications
Second Edition
Jeanine Meyer
HTML5 and JavaScript Projects
Jeanine Meyer
New York, USA
Acknowledgments ���������������������������������������������������������������������������������������������������xv
Introduction �����������������������������������������������������������������������������������������������������������xvii
v
TABLE OF CONTENTS
Chapter 4: Map Maker: Combining Google Maps and the Canvas ����������������������� 119
Introduction ............................................................................................................................... 119
Latitude and Longitude and Other Critical Requirements ......................................................... 131
HTML5, CSS, and JavaScript Features ...................................................................................... 137
The Google Maps API ........................................................................................................... 137
Canvas Graphics .................................................................................................................. 140
Cursor .................................................................................................................................. 144
JavaScript Events ................................................................................................................ 145
Calculating Distance and Rounding Values for Display ....................................................... 150
Building the Application and Making It Your Own ..................................................................... 152
Testing and Uploading the Application ...................................................................................... 165
Summary................................................................................................................................... 165
vi
TABLE OF CONTENTS
Chapter 5: Map Portal: Using Google Maps to Access Your Media ����������������������� 167
Introduction ............................................................................................................................... 167
Project History and Critical Requirements ................................................................................ 175
HTML5, CSS, and JavaScript Features ...................................................................................... 176
Google Maps API for Map Access and Event Handling ........................................................ 176
Project Content in External File ........................................................................................... 179
Distances and Tolerances .................................................................................................... 181
Regular Expressions Used to Create the HTML ................................................................... 182
Dynamic Creation of HTML5 Markup and Positioning ......................................................... 183
Hint Button........................................................................................................................... 186
Building the Application and Making It Your Own ..................................................................... 187
The Quiz Application ............................................................................................................ 187
Testing and Uploading the Application ...................................................................................... 201
Summary................................................................................................................................... 201
vii
TABLE OF CONTENTS
viii
TABLE OF CONTENTS
ix
TABLE OF CONTENTS
x
About the Author
Jeanine Meyer is a full professor at Purchase College/State
University of New York. She teaches courses to students
majoring in mathematics/computer science and also enjoys
the frequent presence of others in her classes, including new
media, music, dance, economics, and chemistry majors. She
developed and teaches courses satisfying the mathematics
general education requirement, including one on math in
the news and one on origami. The website for her academic
activities is http://faculty.purchase.edu/jeanine.meyer.
Before coming to academia, she was a research staff member and manager at IBM
Research, focusing on robotics and manufacturing research, and she later worked on the
corporate manufacturing staff and as a research consultant at IBM for educational grant
programs.
She has enjoyed working with Apress, including updating the HTML5 books. She
continues with the practice of building programming examples using media featuring
her family and her activities and hopes that inspires readers to create work on topics that
are important to them. Her hobbies and interests include studying Spanish and piano,
playing computer games, doing origami, and volunteering for progressive candidates
and causes. She enjoys her daughter Aviva’s cooking while doing some baking herself
and looks forward to travel this year.
xi
About the Technical Reviewer
Takashi Mukoda is an international student at Purchase
College/State University of New York. He is currently taking
a semester off and back home in Japan. At Purchase College,
he majors in Mathematics/Computer Science and New
Media and has worked as a teaching assistant for computing
and mathematics courses.
Takashi likes playing the keyboard and going on hikes in
the mountains to take pictures. His interest in programming
and art motivates him to create multimedia art pieces. Some
of them are built with Processing and interact with human
motion and sounds.
(See his website at http://www.takashimukoda.com.)
xiii
Acknowledgments
Much appreciation to my students and colleagues at Purchase College/State University
of New York. In particular, for Chapter 5, which covers the map portal quiz, I want to
thank Jennifer Douglas, Jeremy Martinez, and Nik Dedvukaj, for the maze video clip
produced in my Robotics class in 2008, which I retained for the new edition. I want also
to thank Takashi Mukoda for his photograph of the Great Torii, in addition to everything
else he has contributed to this and other projects. I re-used an audio recording of my
mother playing piano. Thanks to all my family members (Aviva Meyer, Daniel Meyer,
Annika Meyer, Anne Kellerman, Palmer Agnew, Debbie Torres, and Joshua Torres) for
being the subjects of photographs and videos and for making the photos, video, and
audio. Thanks to Daniel Davis for his HTML5 logo and his technical assistance with the
first edition.
Thanks to the crew at Apress, including Nancy Chen, James Markham, and the
technical reviewer Takashi Mukoda, as well as others I do not know by name.
xv
Introduction
This book continues my exploration of HTML5. My approach in developing the projects
was to combine features such as canvas and video, attempt more intricate drawing by
using mathematics, and use standard programming techniques such as object-oriented
programming and separation of content and logic. I was also interested in building
applications combining HTML5 and JavaScript with other technologies, including
Google Maps.
Each chapter in the book is focused on an application or set of related applications.
This is because my experience as a teacher and a learner has shown that concepts
and mechanics are best understood in the context of actual use. The applications start
off with drawing the HTML5 official logo. As you will find out in Chapter 1, the way I
developed this application prompted use of coordinate transformations.
The project in Chapter 2, involving a family collage, was inspired by my growing
family and the desire to teach about object-oriented programming. It is a good
application for you to use as a foundation to create your own, with your own photos
and objects of your own invention. Chapter 3, which shows how to create a bouncing
video, was built on other two-dimensional applications I have created, and features two
different ways to combine canvas and video.
Chapters 4 and 5 demonstrate use of the Google Maps API (Application
Programming Interface), a powerful facility that allows you to incorporate access
to Google Maps as part of your own projects. Chapter 4 presents a user interface
combining map and canvas and includes a custom-designed cursor and the use of alpha
(transparency) in drawing paths. The map quiz in Chapter 5 demonstrates the use of
mapping as a portal to media. The application shows you how to separate content and
logic so you can scale up to various applications (e.g., a tour of a region or a geography
quiz with many locations).
Chapter 6 features a game called Add to 15, which turned out to be an excellent
example of arrays. It also demonstrated the necessity to prepare for bad behavior on the
part of players.
xvii
INTRODUCTION
In Chapter 7, I use the task of producing directions for origami to show how
to combine line drawings, often using mathematical expressions, and video and
photographs. You can use this as a model for your own set of directions for a task in
which drawings, video, or images would be most appropriate. Or you can let the reading
refresh your memory for topics in algebra and geometry. Chapter 8 features a jigsaw
puzzle that is transformed into a video when it’s completed. Chapter 9 is an educational
game with questions on the states of the USA, and it includes the challenge of a jigsaw
puzzle. The jigsaw puzzle includes the feature of saving the puzzle-in-progress using
localStorage.
For Chapter 10, I decided to address challenges of responsive design and
accessibility as being more appropriate for an HTML and JavaScript book than what I
had before. My examples demonstrate ways to incorporate touch in addition to mouse
actions, to respond to different screen dimensions, and to specify tab order to ease the
use of screen readers.
xviii
INTRODUCTION
to use them as a reference when writing your own programs. Each chapter ends
with instructions and tips for testing and uploading the application to a server, and a
summary of what you learned.
The code is included as downloads available from the publisher. Go to
https://github.com/Apress/html-js-projs. In addition, the figures are available as
full-color TIFF files. Of course, you will want to use your own media for the projects. My
media (video, audio, and images) is included with the code and this includes images for
the 50 states for the states game in Chapter 9. You can use the project as a model for a
different part of the world or a puzzle based on an image or diagram. There are extras:
a program for an origami frog included with the code in Chapter 7 and a version of the
jigsaw turning into a video from Chapter 8 adapted for use on devices requiring touch is
included with the source code for Chapter 10.
Let’s get started.
xix