Skip to content

Instantly share code, notes, and snippets.

View LiamChapman's full-sized avatar

Liam Chapman LiamChapman

View GitHub Profile
@LiamChapman
LiamChapman / webgl_light_mousemove_target_position.js
Created January 9, 2018 13:57
WebGL (ThreeJS) mouse move light x and y position (target position) / pan
var l = {
colour: 0xffffff,
intensity: 1,
distance: 0,
angle: 10 * (Math.PI/180),
penumbra: 0.8,
decay: 1
};
var light = new THREE.SpotLight( l.colour, l.intensity, l.distance, l.angle, l.penumbra, l.decay);
@LiamChapman
LiamChapman / webgl_light_mousemove.js
Last active January 8, 2018 10:43
WebGL (ThreeJS) mouse move light x and y position
var l = {
colour: 0xffffff,
intensity: 1,
distance: 0,
angle: 10 * RAD,
penumbra: 0.8,
decay: 1
};
var light = new THREE.SpotLight( l.colour, l.intensity, l.distance, l.angle, l.penumbra, l.decay);
@LiamChapman
LiamChapman / webgl_camera_mousemove.js
Last active January 8, 2018 10:44
WebGL (ThreeJS) mouse move camera x and y
var m = { x: 0, y: 0 };
window.addEventListener('mousemove', function (e) {
m.x = e.clientX;
m.y = e.clientY;
camera.position.x = (((m.x / window.innerWidth) * 2) - 1) * -1;
camera.position.y = ((m.y / window.innerHeight) * 2) - 1;
});
@LiamChapman
LiamChapman / double_tap_touch.js
Created January 5, 2018 10:17
Double click for touch devices
var tapedTwice = false;
function tapHandler (e) {
if(!tapedTwice) {
tapedTwice = true;
setTimeout( function() { tapedTwice = false; }, 300 );
return false;
}
e.preventDefault();
// DO FUNCTIONALITY HERE!
}
@LiamChapman
LiamChapman / simple_router.js
Created November 8, 2017 14:34
Simple function router with js
var router = function (routes) {
for (var i in routes) {
if (i == window.location.pathname) {
window[routes[i]]();
}
}
};
/**
* Example
@LiamChapman
LiamChapman / frame_rate_control.js
Created October 25, 2017 15:20
Control canvas frame rate (fps) with requestAnimationFrame
var stop = false;
var frameCount = 0;
var fps, fpsInterval, startTime, now, then, elapsed;
function startAnimating(fps) {
fpsInterval = 1000 / fps;
then = Date.now();
startTime = then;
render();
}
@LiamChapman
LiamChapman / _README.md
Created October 24, 2017 13:23 — forked from remy/_README.md
requestAnimationFrame helper

raf.js

A simple script with a few niceties that allows for multiple requestAnimationFrame calls, and FPS pinning.

How it works

The script polyfills rAF if required, then overloads requestAnimationFrame and cancelAnimationFrame with a process that allows multiple frames to be queued up for rAF to run.

This is useful if there are multiple animations running on the page, you want all the callbacks to happen at once, and not on multiple rAF calls. This script is meant as a drop-in solution to that problem.

@LiamChapman
LiamChapman / rAF.js
Created October 23, 2017 20:05 — forked from paulirish/rAF.js
requestAnimationFrame polyfill
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
@LiamChapman
LiamChapman / range.js
Created September 29, 2017 13:33
range function to create array range of number
function range (start, stop, step){
if(!stop){stop=start;start=0;}
return Array.from(new Array(parseInt((stop-start)/step)), (x,i) => start+ i*step)
}
@LiamChapman
LiamChapman / isMobile.js
Created September 25, 2017 16:08
mobile user agent check
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)