Skip to Content
WebGL: Up and Running
book

WebGL: Up and Running

by Tony Parisi
August 2012
Beginner
230 pages
6h 4m
English
O'Reilly Media, Inc.
Content preview from WebGL: Up and Running

Chapter 5. Interaction

Like any online medium, WebGL is a tripod that stands on three legs: graphics, animation, and interaction. Our examples thus far have made very limited use of interaction. We handled mouse clicks in the canvas to toggle animation states; we used controls on the web page to change behaviors in the WebGL scene. But we can go way beyond this. We can click and drag on items in the scene itself, triggering other behaviors. We can also navigate within the scene by manipulating the camera. Furthermore, we can combine these capabilities with animated tweens to give our interactivity a rich, organic feel.

As an experienced web developer, you probably take interaction for granted. Browsers automatically perform hit detection—the process of finding out which element the mouse pointer is over—for you; you simply need to register mouse event handlers for your DOM elements and controls. However, WebGL is only a drawing system and, as such, has no built-in support for hit detection. You need to build that yourself. Thankfully, Three.js gives us the support we need so that we can tell when the mouse is over an object. We will lean on that to implement rollovers, clicks, and dragging, as well as camera navigation. We will also use Tween.js to combine interaction with animation for some nice UI effects. Let’s get into it.

Hit Detection, Picking, and Projection

Hit detection is a special case of the more general 3D graphics concept of picking, the process of determining if two ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

WebGL Beginner's Guide

WebGL Beginner's Guide

Diego Cantor, Brandon Jones
WebGL Insights

WebGL Insights

Patrick Cozzi

Publisher Resources

ISBN: 9781449326487Errata