Skip to content

Instantly share code, notes, and snippets.

@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:4997956
Last active May 20, 2019 14:14
Javascript Math: get degree between two points
/**
* return the angle between two points.
*
* @param {number} x1 x position of first point
* @param {number} y1 y position of first point
* @param {number} x2 x position of second point
* @param {number} y2 y position of second point
* @return {number} angle between two points (in radian)
*/
Math.getAngle = function( x1, y1, x2, y2 ) {
@timohausmann
timohausmann / gist:5003280
Created February 21, 2013 08:43
Javascript Math: get the distance between two points.
/**
* return the distance between two points.
*
* @param {number} x1 x position of first point
* @param {number} y1 y position of first point
* @param {number} x2 x position of second point
* @param {number} y2 y position of second point
* @return {number} distance between given points
*/
Math.getDistance = function( x1, y1, x2, y2 ) {
@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;
}