tQuery
three.js
power
+
jQuery
API Usability
Jerome Etienne
Who Am I
-
coder, speaker, trainer
-
Proud member of Web-5 association
-
Javascript, Node.js, WebGL and other HTML5 games stuff
Learning Three.js
-
Author of learningthreejs blog
-
Lists of three.js tutorials or "WebGL for dummies"
Contacts
tQuery in all that ?
tQuery Started as a Dream :)
Wishes
-
"You want to know webgl? Good news! You already know it, just replace
j by t!"
- you know jQuery, so you know tQuery
-
"WebGL should not be harder than playing with lego."
-
"WebGL for jQuery enthousiasts"
Would like them to be true one day...
and more preciselly ?
What is tQuery
Technical
-
Extension system for three.js
-
Mimic jQuery API
Direction
-
Technical: three.js power + jQuery API usability
-
Human: Bring 3D to non-specialists
Freeze! Show, dont tell.
Here is a Minimal Page - the SOURCE
<!doctype html><title>Minimal tQuery Page</title>
<script src="../../build/tquery-bundle.js"></script>
<body><script>
var world = tQuery.createWorld().boilerplate().start();
var object = tQuery.createTorus().addTo(world);
</script></body>
-
Super short (only 6-lines !)
-
jQuery-like syntax
-
Full HTML page. valid HTML5
-
See it running here
Here is a Minimal Page - the DEMO
OK, go on now.
tQuery aims to three.js power + jQuery API usability
three.js power:
-
actively developped by a team of skilled dev
-
significant community around three.js
jQuery API usability
-
widely recognized (85% market share)
-
successful plugin ecosystem
tQuery is an Extension System for three.js
-
Must make three.js easier to extend
Thus people can
- do plugins for three.js (like they do in jQuery)
- They share their work
- They build on top of each other
- Open Source spirit
All good in my book
What is your deal with jQuery ?
jQuery and its API usability
Always admired their plugins ecosystem
- varied, lively
- some impressive specimens
jQuery is popular
-
85% of marketshare (ymmv)
Match my goal: "Bring 3D to non-specialists"
-
people who know jQuery will learn tQuery in no time
How different is it from three.js ?
Relation with three.js
Three.js Library
What is it?
- 3D javascript library
- can render in WebGL, Canvas or dom
Why this library ?
- likely the most popular
- Actively developped by a community of experienced devs
- and i knew it already :)
tQuery and Three.js sitting in a tree
Principles
- tquery.js core got a single dependancy: three.js
- easy to go back and forth between the two
Coverage
- tQuery must provide Access to three.js
- no requirement to cover it all
Just another API on top of three.js
What remains the same ?
- no change in semantic
- nothing smart
What changes ?
- other functions
- other defaults
- easy plugins system
So you copy jQuery ?
Relation with jQuery
tQuery Mimics jQuery API
Motivation
-
It makes tQuery very easy to learn if you know jQuery
-
Very important. It got significant consequences.
Not Kidding
Principles
-
for api, copy jquery API whenever possible
-
helpers may be provided, but jquery is the auhority
hmm up to which point ?
Key aspects of jQuery
- DOM tree from webpages
- Chained API
- ID and Class
- CSS-like selector
- DOM events
Dom Tree
DOM and 3D Scene: both suprisingly similar
-
jQuery and DOM got a tree of elements from webpages
-
three.js got a hierachy of objects too
|
DOM |
Three.js |
Hierachy |
DOM tree
| scene
|
Element |
DOMElement
| THREE.Object3D
|
- On one hand, jquery got the dom and its tree of element.
- On the other hand, three.js got a scene with Object3D.
Chained API
tQuery('.fooKlass').scale(2).translate(1,0,0);
Well known feature jQuery
ID and Class
var cube = tQuery().createCube();
cube.id("myId"); // set the id of this element
cube.addClass('fooKlass'); // add 'fooKlass' class to this cube
cube.data('goom', 'baa');
- Playground example
- Notice css-like selector ?
- it works for class and id with jQuery
CSS-like Selector
for classes and id as we just saw in previous slide
Selector based on geometry, similar to a jQuery('span')
tQuery('sphere'); // select all objects with a sphere gemotry
tQuery('cube'); // same with a cube gemotry
CSS-like Selector
can compose them like with jQuery
tQuery('.bar.foo') // objects with class 'bar' and 'foo'
tQuery('.bar cube') // objects with class 'bar' and cube descandants
Dom Events
tQuery('cube').on('click', function(){
console.log("somebody put the mouse over a cube");
});
Supported Events
- click and dblclick
- mouseup and mousedown
- mouseover and mouse out
ok so it is for real.
Mimicing jQuery API for fun and profit
Taken very seriously!!
- Well-known for its usability
- Much easier to learn
jQuery API is authoritative!
Bla bla bla ... all talk.
Show something!
Here is a mini-game
click here
Ok not bad, what about the code?
Build the tube's walls
// HINT: a tunnel is a cylinder seen from inside.
// Once you realized that, most of the work is done.
// create the object
var tube = tQuery.createCylinder();
// now lets turn it inside out - flip the geometry
tube.geometry().flip();
Advancing into the tube
tubeTexture.material().textureScrolling({
// transform the texture on render
transform : function(tTexture){
// to go forward
tTexture.offset.y += 0.2;
// make it loop
tTexture.offset.y %= 1;
}
});
-
It is all an illusion!
-
"There is no spoon" - matrix
Making the tube wavy
tube.geometry().vertexAnimation({
// transform vertices on render
transform : function(origin, vector, present){
// compute displacement
var deltaX = Math.cos(present*0.4);
var deltaY = Math.sin(present*0.4);
// apply displacement
vector.x = origin.x + deltaX;
vector.y = origin.y + deltaY;
}
});
Move the player
// Move player left/right
var playerAngle = 0;
var keyboard = tQuery.keyboard();
// hook the rendering loop
world.loop().hook(function(){
// check the keyboard
if( keyboard.pressed('right') ) playerAngle += 0.01;
if( keyboard.pressed('left') ) playerAngle -= 0.01;
});
hmm tQuery is about extension ?
Show some!
tQuery.text
Display Text in 3D
Plugins layground examples
tQuery.Shapes
Build 3D shape with canvas-like path
tQuery.csg
Constructive Shape Geometry
Able to add/substract/join objects to build new ones
tQuery.domevents
Emulation of DOM event in 3D space
tQuery.fireball
Pure Shader Material
tQuery.md2character
Animate doom character without breaking a sweat
- MD2 is the format of the character
Play in javascript console
character.setSkin(2); // to change the skin (5 of them)
character.setWeapons(4); // to change the weapons (11 of them)
// some randomness
setTimeout(function(){
character.setSkin(Math.floor(Math.random()*5 % 5));
character.setWeapon(Math.floor(Math.random()*11 % 11));
}, 1000);
Just a few plugins in tQuery
- Plugins allows much easier reuse
- Written to see how tQuery reacts in practice
- Currently 30 plugins in incubation
Anything else ?
Conclusion
three.js power + jQuery API Usability
The Code
That's all folks.
Question ?
Jerome Etienne