Edan Kwan @edankwan
http://www.edankwan.com
Hong Kong 2015
get
initial value
update
new value
redraw
finish
if not finished, on next frame...
function loop() {
// clamp at maximum 60fps in browser
requestAnimationFrame(loop);
render();
}
function render() {
// draw something
}
function lerp(from, to, ratio) {
return from + (to - from) * ratio;
}
ease-in-out
ease-in / ease-out
Further Reading: Google Material Design
var x = 0;
function loop() {
// x = lerp( x, newX, 0.1 );
x += ( newX - x ) * 0.1;
}
var tmX = 0;
var tmY = 0;
var mX = 0;
var mY = 0;
var easeRatio = 0.1;
var ball = document.querySelector('.ball');
window.addEventListener('mousemove', onMouseMove);
function onMouseMove(evt) {
tmX = evt.pageX;
tmY = evt.pageY;
}
function loop() {
requestAnimationFrame(loop);
mX += (tmX - mX) * easeRatio;
mY += (tmY - mY) * easeRatio;
ball.style.left = mX + 'px';
ball.style.top = mY + 'px';
}
loop();
Further Reading: Useful Math Snippets by Howling Moon Software
WebGL != 3D
WebGL != ThreeJS
Vertex Shader
Fragment Shader
JS with WebGL API
var vertices = [x0, y0, x1, y1, x2, y2...]
gl.drawArrays(gl.TRIANGLES, first, count);
// OR draw elements with Indices
gl.drawElements(gl.TRIANGLES, count, type, offset);
attribute vec2 aPosition;
void main(void) {
gl_Position = vec4(aPosition, 0.0, 1.0);
}
GLSL
JS with WebGL API
var texture = gl.createTexture();
texture.image = image;
gl.bindTexture(gl.TEXTURE_2D, texture);
uniform sampler2D uTexture;
varying vec2 vTexCoord;
void main() {
gl_FragColor = texture2D(uTexture, vTexCoord);
}
GLSL
GLSL Qualifiers
GLSL data types