Skip to content

Instantly share code, notes, and snippets.

View vanrez-nez's full-sized avatar

Ivan Juarez vanrez-nez

View GitHub Profile
@vanrez-nez
vanrez-nez / lerp.js
Last active August 29, 2015 14:15
linearly interpolate between two values
//returns the interpolated value from a and b at the given weight (from 0 to 1)
var lerp = function(a, b, w) {
return (1 - w) * a + w * b;
}
@vanrez-nez
vanrez-nez / clampNumber.js
Last active August 29, 2015 14:15
Clamp number
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
@vanrez-nez
vanrez-nez / idGenerator.js
Last active April 28, 2016 23:23
Generate string ID's of a fixed length
function generateID() {
return ("000000" + (Math.random()*Math.pow(36,6) << 0).toString(36)).slice(-6));
}
function generateIDFromStr() {
var seed = str.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
return ( "000000" + ( seed << 0 ).toString( 36 ) ).slice( -6 );
}
@vanrez-nez
vanrez-nez / rgb2Hex.js
Last active August 29, 2015 14:17
Conversion from RGB values to Hex notation
function rgb2Hex(r, g, b) {
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).substr(1);
}
@vanrez-nez
vanrez-nez / bitwise.js
Created March 24, 2015 16:10
Bitwise Operations
function isOdd(number) {
return !!(number & 1);
}
// Floor Float aka cast to Int
var n = 1.9 | 0; // -> 1
@vanrez-nez
vanrez-nez / random.js
Last active January 15, 2016 19:54
Random Utils
function getRandomNumber(min, max) {
return Math.random() * (max - min) + min;
}
function getRandomColor() {
// http://stackoverflow.com/questions/1484506/random-color-generator-in-javascript
return "#"+((1<<24)*Math.random()|0).toString(16);
}
@vanrez-nez
vanrez-nez / oscillator.js
Last active August 29, 2015 14:27
Function that oscillates between two values
// @input: discrete counter
// @min: lower value
// @max: maximum value
function oscillate(input, min, max) {
var range = max - min;
return min + Math.abs(((input + range) % (range * 2)) - range);
}
@vanrez-nez
vanrez-nez / traverse_object_props.ts
Created December 1, 2015 20:30
Traverse all object properties recursively.
private traverse(obj: any, interatee: Funcion) {
for (var i in obj) {
func.apply(this, [i, obj[i]]);
if (obj[i] !== null && typeof (obj[i]) == "object") {
this.traverse(obj[i], iteratee);
}
}
}
@vanrez-nez
vanrez-nez / image_resize.js
Created December 18, 2015 19:37
Image resizing utils
function getSizeToCover(width, height, maxWidth, maxHeight) {
var ratio = Math.max(maxWidth / width, maxHeight / height);
return {
width: width * ratio,
height: height * ratio
};
}
function getSizeToContain(width, height, maxWidth, maxHeight) {
var ratio = Math.min(maxWidth / width, maxHeight / height);
@vanrez-nez
vanrez-nez / min.accessories.js
Last active March 9, 2018 19:37
Common useful accessories for javascript
//Some functions where taken from Jistin Windle's sketch.js at https://github.com/soulwire/sketch.js
// Modulus with support for negative integers
Mod = function(current, limit) {
return ( (current % limit) + limit ) % limit;
};
function isArray( object ) {
return Object.prototype.toString.call( object ) == '[object Array]';