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 / ogl.d.ts
Last active Oct 9, 2020
Typescript for OGL
View ogl.d.ts
/**
* 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 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]';
@vanrez-nez
vanrez-nez / wait-for-global.js
Last active Jan 3, 2018
Wait for globals to be present
View wait-for-global.js
// 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) {
View brunch-config.js
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 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 / 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 / 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 / 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 / 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