Skip to content

Instantly share code, notes, and snippets.

@timohausmann
timohausmann / Vector.js
Last active October 13, 2015 15:56
Vector JS
/**
* Vector Lib
* @author Timo Hausmann <hello.timohausmann@gmail.com>
* @url https://gist.github.com/timohausmann/9603675
* @licence MIT
* @version 1.0
*/
function Vector(x, y) {
this.x = x,
@timohausmann
timohausmann / gist:4997781
Last active December 14, 2015 00:19
Javascript Math: factor for converting degree values into radian
/**
* Factor for converting degree values into radian.
*/
Math.TO_RAD = Math.PI/180;
@timohausmann
timohausmann / gist:4997846
Last active December 14, 2015 00:19
Javascript Math: get the signature of a number
/**
* return the signature of a number.
*
* @param {number} val the input value
* @return {number} 1 if positive, -1 if negative, else 0
*/
Math.sign = function( val ) {
return val > 0 ? 1 : val < 0 ? -1 : 0;
};
@timohausmann
timohausmann / gist:4997906
Last active December 14, 2015 00:19
Javascript Math: get random number within boundaries
/**
* return a random number within given boundaries.
*
* @param {number} min the lowest possible number
* @param {number} max the highest possible number
* @param {boolean} round if true, return integer
* @return {number} a random number
*/
Math.randMinMax = function(min, max, round) {
var val = min + (Math.random() * (max - min));
@timohausmann
timohausmann / gist:5055811
Last active December 14, 2015 08:09
HTML: HTML5 Template
<!doctype html>
<!--[if lt IE 7 ]> <html class="no-js ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]> <html class="no-js ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]> <html class="no-js ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<title>Seitentitel</title>
<meta charset="utf-8" />
<meta name="description" content="Kurzbeschreibung der Seite, idealerweise unter 160 Zeichen" />
@timohausmann
timohausmann / gist:5056007
Created February 28, 2013 11:11
CSS: Sexy Image Replacement
.ir {
border: 0;
font: 0/0 a;
text-shadow: none;
color: transparent;
background-color: transparent;
}
// 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
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
@timohausmann
timohausmann / gist:5097741
Created March 6, 2013 08:54
Javascript: Render To Canvas Buffer
var renderToCanvas = function (width, height, renderFunction) {
var buffer = document.createElement('canvas');
buffer.width = width;
buffer.height = height;
renderFunction(buffer.getContext('2d'));
return buffer;
};
@timohausmann
timohausmann / gist:5203276
Created March 20, 2013 09:01
Javascript: restrain maximum dimensions
var aspectRatio = width / height;
if( height > APP.height ) {
height = APP.height;
width = height * aspectRatio;
} else if( width > APP.width ) {
width = APP.width;
height = width / aspectRatio;
}
@timohausmann
timohausmann / gist:5364550
Created April 11, 2013 15:51
jQuery: reverse
$.fn.reverse = [].reverse;