JavaScript Creativity - Shane Hudson.pdf

(2952 KB) Pobierz
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
About the Author ...............................................................................................................xiii
About the Technical Reviewer ............................................................................................
xv
Acknowledgments ............................................................................................................
xvii
Foreword ...........................................................................................................................
xix
Chapter 1: Introduction .....................................................................................................1
Chapter 2: Canvas and Animation Basics ........................................................................11
Chapter 3: Audio and Video Basics..................................................................................35
Chapter 4: Beginning 3D ..................................................................................................55
Chapter 5: Creating Music in the Browser ......................................................................77
Chapter 6: The Music Player............................................................................................89
Chapter 7: Real-time Collaboration with Node.js ..........................................................105
Chapter 8: Video-to-Video Using WebRTC......................................................................113
Chapter 9: Motion Detection ..........................................................................................125
Chapter 10: Interacting with the Browser Using Gestures ............................................137
Chapter 11: Appendix ....................................................................................................155
Index .................................................................................................................................159
v
CHAPTER 1
Introduction
In this book, we will go on a journey down the rabbit hole of HTML5 and modern JavaScript, exploring a lot of
different subjects as we go. We will start with canvas, which many of you will already be familiar with, and use it for
two examples: a flocking animation and a coloring book app. Each chapter shows you how each topic can be used in
the real world; how HTML5 and JavaScript can be used together to create useful applications and not just for making
games or cool demos (although it will also help with those too). The book is split into three main projects, with some
additional examples along the way. Every example (especially the main projects) aims to be a good starting point from
which to play around and explore, because as useful as books are with their structured approach to learning, there is
nothing better than just diving into the code yourself.
After canvas we will, in Chapter 3, delve into using audio and video on the web; this chapter primarily focuses
on the Web Audio API, as well as both the audio and video HTML elements. The API is extremely powerful, so the
chapter is only a brief introduction and is mostly about visualization of audio, which ties in nicely with Chapter 2,
which uses canvas as the base for the graphics. We then move onto 3D graphics, exploring the basics of 3D on the web.
I’ve decided to use the popular library Three.js for handling the WebGL, since pure WebGL “shader code” is rather
complicated and very similar to the C language. There are multiple examples along the way in Chapter 4 but the
main example is a 3D node graph that shows relationships between films and actors/actresses. I chose this example
because data visualization is an extremely useful technique and one of the areas that I have a strong interest in, it is
also a good way to show what these new technologies have allowed us to do natively on the web.
In Chapter 5, we will be going on a bit of a tangent into the realm of music theory because the first main project
is going to be a music player that includes 3D visualization and music creation; this will nicely tie together all the
chapters up until this point. The chapter builds heavily on the introduction to the Web Audio API in Chapter 3,
however instead of visualization it generates sound (which can hopefully be used as a starting point for music).
Of course, as with all the chapters, it is only a peek into a large discipline so I aim for it to be a good starting point
rather than a comprehensive guide. The music theory aspect of Chapter 3 is one of the reasons why this book is called
JavaScript Creativity;
it is not about design or even focused on what you can do with canvas—it is about the creativity
of applying web technologies to more than just a blog or an online store.
The music player project is put together in Chapter 6. I use Backbone as a way to bind together all the data,
although you are welcome to convert it and instead use something else such as Ember or even just ES6.
The next project is comprised of Chapters 7 and 8. The project is a video chat application using WebRTC and
Node.js. Chapter 7 will be a brief introduction to using Node.js for a text-based chat room and Chapter 8 will be a
fairly in-depth look into how WebRTC works and how to use it. This will be a useful chapter for many because video
chat is a common project that people want to make, especially within some businesses. The project also provides the
introduction to getUserMedia that will be needed for the final project (Chapters 9 and 10) involving motion and object
detection using the webcam.
Object detection is not a field that is particularly common within the web industry, but it has always been an
interest of mine and we now have native capabilities for accessing the webcam (and even more complex techniques
such as Web Workers, although I will not be using them in this book) so it makes sense that I would be doing
computer vision on the web! The final project, involving object detection, is possibly less useful than the others but
more interesting and hopefully it will be the perfect starting place for anybody interested in the subject as well as
1
CHAPTER 1
INTRODUCTION
showing how powerful the web platform really is. The majority of computer vision algorithms are math based and
confusing, so I’ve gone for naïve algorithms (simple to understand but have many edge cases that are not catered
for), as well as using a library for more advanced object detection so that you can have an introduction without being
bombarded with academia.
The final project ties together all the chapters. It is a real time multi-user, computer-generated, gesture-controlled
musical band. This is an ambitious project and is by no means perfect. It is a great way to practice everything in the
book and is (I hope) a fun and unique project. The music generated will not be great, it will probably lag and not
sound very good but I think it will be the perfect end to the book; after all, my main purpose of the book is to get you
involved with projects that you may have not even thought about before!
What You Need to Know
Now that you know the journey we will be going on with this book, it is important to be able to start it. The book
is being sold as requiring a working knowledge of JavaScript. This is quite true, but it is written in such a way that,
although some of the code is quite complicated, it should be fairly accessible to anybody with some programming
background.
Note
This chapter will focus on the basics, namely debugging, so most of you will probably want to continue to
chapter 2 for the introduction to canvas.
CSS
I just wanted to say that knowledge of CSS is expected, but not particularly needed. I use CSS in some chapters to style
parts of a page but I tend to focus on the code throughout the book so nothing is polished design-wise and CSS use is
minimal. You can get by fine without knowing any, but just be aware that if I do use some it will not be explained.
Debugging
Debugging is, at least in my opinion, a programmer’s best skill because quite often code will not work as expected
(especially across browsers) and even if it does it can always be improved. Most of the code in this book relies on
cutting-edge technologies and many specs have changed even while I wrote the book—so if anything doesn’t work,
then it could be due to the implementation having changed since I wrote it or the browser you are using doesn’t
support it.
Browser Compatibility
The first thing to check is whether your browser is compatible with the features being used. This is something you
should already be familiar with if you’ve ever used any modern technologies in production because many users still
use browsers that do not support it. Of course, in this case you should be writing code in a ”progressive enhancement”
way so that the modern browsers get the modern features but it does not break the older browsers. However,
throughout this book (because it is about cutting-edge technologies and features) I do not support older browsers
and quite often at the time of writing the code only works in one or two browsers. Unless otherwise stated, I test all the
code using Google Chrome 31. Most of it should also work in Mozilla Firefox.
2
Zgłoś jeśli naruszono regulamin