tQuery API
three.js
power
+
jQuery
API Usability
Jerome Etienne
Who Am I
-
CTO of Beloola.com
-
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
What is tQuery API ?
tQuery API in a few word
Thin layer on top of three.js
-
three.js : 100k min+gzip
-
tQuery.js : 6k min+gzip
Dumb library
-
Different API
-
Different default values
-
Same Semantic
tQuery API Started as a itch to scratch
Itch: Bring 3D to non specialists
-
may be seen as WebGL for jQuery enthousiasts
-
If you know jQuery, you learn tQuery in no time
Working hard to make it true !! :)
Freeze! Show, dont tell.
Flying to the moon Demo - 10h of code
-
Demo
Minecraft visits Mount Olympus - 6h of code
-
Demo
a Sport Car - 40 lines of js
-
Demo
a minecraft guy - 20 lines of js
-
Demo
head tracking - 20 lines of js
-
Demo
light saber - 30 lines of js
-
Demo
Realistic physics - 80 lines of js
-
Demo
a 3D Pong - 100 lines of js
-
Demo
Multiplayer Minecraft Chat - 1-day workshop
-
Demo
Not bad ... Why the code is so short ?
Why the code is so short ?
Mimicking jQuery API makes code shorter
-
chained API tends to produce short code
-
Plugins avoids recoding repeatitive elements
Plugins makes code shorter
Short code is a by product of tQuery principles
Wanna try ?
Live editor to the rescue
Coolness
-
no install needed
-
live experimentation
Examples cuteness
Show us the code.
Here is a Minimal Page
-
playground
<!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>
Fun Facts
- Full HTML5 page
- Valid HTML5
- Only 6-lines !
Here is a Minimal Page
-
Demo
What is the recipe of tQuery ?
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
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 :)
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
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
Key aspects of jQuery - All Present in tQuery too!
- 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);
ID and Class
var cube = tQuery().createCube();
// set the id of this element
cube.id("myId");
// add 'fooKlass' class to this cube
cube.addClass('fooKlass');
// set data attribute
cube.data('goom', 'baa');
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
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
Bla bla bla ... all talk.
Show something!
Dom Events
-
Demo
a sound visualisation - 40 lines of js
-
Demo
Play with Doom Character - 50 lines of js
-
Demo
What about a game ?
Here is a mini-game
-
Demo
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!
Examples Plugins
tQuery.text: Display Text in 3D
- example
tQuery.Shapes: Build 3D shape with canvas-like path
- example
- Valentine card
tQuery.csg: add/sub objects
- example
tQuery.domevents: Emulation of DOM event in 3D space
- example
More Examples Plugins
tQuery.fireball: pure shader material
- example
tQuery.md2Character: Animate doom character
- example
- can play in console too.
character.setSkin(2); // to change the skin (5 of them)
character.setWeapon(4); // to change the weapons (11 of them)
// some randomness
setInterval(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 50+ plugins in incubation
Anything else ?
Conclusion
three.js power + jQuery API Usability
The Code
That's all folks.
Question ?
@JeromeEtienne