Skip to content

Instantly share code, notes, and snippets.

Avatar

Iván Juárez Núñez vanrez-nez

View GitHub Profile
@vanrez-nez
vanrez-nez / lerp.js
Last active Aug 29, 2015
linearly interpolate between two values
View lerp.js
//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;
}
View clampNumber.js
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
@vanrez-nez
vanrez-nez / idGenerator.js
Last active Apr 28, 2016
Generate string ID's of a fixed length
View idGenerator.js
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 Aug 29, 2015
Conversion from RGB values to Hex notation
View rgb2Hex.js
function rgb2Hex(r, g, b) {
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).substr(1);
}
@vanrez-nez
vanrez-nez / bitwise.js
Created Mar 24, 2015
Bitwise Operations
View bitwise.js
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 Jan 15, 2016
Random Utils
View random.js
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 Aug 29, 2015
Function that oscillates between two values
View oscillator.js
// @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 Dec 1, 2015
Traverse all object properties recursively.
View traverse_object_props.ts
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 Dec 18, 2015
Image resizing utils
View image_resize.js
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 Mar 9, 2018
Common useful accessories for javascript
View min.accessories.js
//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]';