jeromeetienne / threex.domevents

three.js extension which provide dom events inside your 3d scene.
http://jeromeetienne.github.io/threex.domevents/examples/demo.html
MIT License
246 stars 113 forks source link

threex.domevents

threex.domevents is a three.js extension which provide dom events inside your 3d scene. Always in a effort to stay close to usual pratices, the events name are the same as in DOM. The semantic is the same too, which makes it all very easy to learn. Currently, the available events are click, dblclick, mouseup, mousedown, mouseover and mouse out.

Show Don't Tell ===============

How To Install It

You can install it via script tag

<script src='threex.domevents.js'></script>

Or you can install with bower, as you wish.

bower install threex.domevents

How To Use It

Let's start by bindinf dom events like on 3d objects.

THREEx.DomEvent.js

First you need to instanciate the layer, like this.

var domEvents   = new THREEx.DomEvents(camera, renderer.domElement)

Always in a effort to stay close to usual pratices, the events name are the same as in DOM. The semantic is the same too. Currently, the available events are click, dblclick, mouseup, mousedown, mouseover and mouse out. Then you bind an event on a mesh like this

domEvents.addEventListener(mesh, 'click', function(event){
    console.log('you clicked on the mesh')
}, false)

You can remove an event listener too, here is the whole cycle

// define the callback
function callback(event){
    console.log('you clicked on the mesh')  
}

// add an event listener for this callback
domEvents.addEventListener(mesh, 'click', callback, false)

// remove an event listener for this callback
domEvents.removeEventListener(mesh, 'click', callback, false)

If the camera changes, you can use domEvents.camera(camera) to set the new one. Now that you got the

THREEx.Linkify.js

This helper will linkify any object3d you got. Here linkify is the fact to act as a <a> tag would behave. If you hover the mouse over it, the pointer is changed to become an hand, and you got text decoration. If you click on the object, it will open a new tab with the url your specified. It is that simple.

var url     = 'http://jeromeetienne.github.io/threex/'
var linkify = THREEx.Linkify(domEvents, mesh, url, true)

The parameters are

Additionnaly you can use linkify.destroy() to stop listening on those events. The exposed properties are

TODO