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 / ogl.d.ts
Last active October 9, 2020 15:37
Typescript for OGL
/**
* Last updated to work with version 0.60
* Library Repo: https://github.com/oframe/ogl
* Definitions extracted from https://github.com/nshen/ogl-typescript/
*
*/
declare module 'ogl/src/math/functions/ColorFunc' {
const NAMES: {
black: string;
@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]';
@vanrez-nez
vanrez-nez / wait-for-global.js
Last active January 3, 2018 18:19
Wait for globals to be present
// Micro utility to execute a function when all globals are present
var waitForGlobal = function(params) {
var pathExists = function( arr, obj ) {
var target = obj[ arr.shift() ];
var exists = target !== undefined;
return arr.length == 0 ? exists : exists && pathExists(arr, target);
};
var predicate = function() {
return params.globals.every(function(path) {
@vanrez-nez
vanrez-nez / brunch-config.js
Created December 15, 2017 00:56
Brunch Setup
module.exports = {
paths: {
watched: ['resources']
},
files: {
javascripts: { joinTo: {
'scripts/app.js': /^resources/,
'scripts/vendor.js': /^node_modules/
} },
stylesheets: { joinTo: {
@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 / 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 / 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 / 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 / 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