threex-domevents

npm Package port of THREEx.DomEvents.js and THREEx.Linkify.js

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
threex-domevents
301.0.06 years ago6 years agoMinified + gzip package size for threex-domevents in KB

Readme

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
\[view source\ : It shows a demo showing up this extension. \[view source\ : It shows a simple usage of ```threex.. \[view source\ : It demonstrate a usage of ``threex.linkify.js``
How To Install It
You can install it via script tag
npm install --save threex.domevents
How To Use It
Let's start by binding dom events like on 3d objects.

First step initialize

var THREE = require('three') // require peer dependency
var initializeDomEvents = require('threex.domevents')
var THREEx = {}
initializeDomEvents(THREE, THREEx)

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
  • ``domEvents`: an instance of `THREEx.DomEvents`
  • `mesh`: an instance of `THREE.Mesh`
  • `url``: a string of the url
  • ``withBoundingBox``: true if you bind the bounding box and not the mesh itself. It
may be useful when your mesh is complex. useful because it is faster to test for simple mesh, usefull as it make the detections less flacky for the users for complex meshes.
Additionnaly you can use ``linkify.destroy()`` to stop listening on those events. The exposed properties are
  • ``linkify.underline`` is the mesh for the underline
  • ``linkify.eventTarget`` is the model on which the domEvents are bound

TODO

  • find a good demo
test all events in the demo why not the usual teapots ones ?