Skip to content

Instantly share code, notes, and snippets.

View victornpb's full-sized avatar
⌨️

Victor victornpb

⌨️
View GitHub Profile
var compileTemplate = function(html) {
var re = /{{([^}]*(?:}[^}]+)*}*)}}/g,
reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g,
code = 'var r=[];\n', cursor = 0, match;
var add = function(line, js) {
js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
(code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
return add;
}
@victornpb
victornpb / tag.js
Created April 23, 2014 03:32
Function to template DOM tags
//create element
function tag(tag, attributes, childs){
function isArray(o) {
return Object.prototype.toString.call(o) === "[object Array]";
}
function isElement(o) {
return !!(o && o.nodeType == 1);
}
function isTextNode(o) {
@victornpb
victornpb / addEvent.js
Last active August 29, 2015 14:00
function addEvent(element, trigger, action)
/**
* Add a event to a element;
* @param {Object} element Element or ID;
* @param {String} trigger Trigger to fire action eg:load,click,mouseover,etc;
* @param {Function} action A pointer to a function to be called on trigger;
*/
function addEvent(element, trigger, action){
if(typeof element==="string"){element=document.getElementById(element);}
if(element.addEventListener){
element.addEventListener(trigger,action,false);
@victornpb
victornpb / AsyncLoop.js
Last active August 29, 2015 14:00
Constructor that create a Loop object to run blocking loops, but allow it to be interrupted when requested to give a change to make DOM updates.
/** Constructor that create a Loop object to run blocking loops, but allow
* it to be interrupted when requested to give a change to make DOM updates
* @author Victor B - www.vitim.us
*/
function AsyncLoop(){
this.loop; //called on every loop
this.onUpdate; //called when the loop is interrupted
this.onComplete; //called when the loop is finished
this.requestInterruptToUpdateDOM = false;
@victornpb
victornpb / fpsMeter.js
Created April 23, 2014 04:22
FPS Meter - Returns a function that is used to compute the framerate without the overhead of updating the DOM every frame
/**
* FPS Meter - Returns a function that is used to compute the framerate without the overhead of updating the DOM every frame.
* @author Victor B - www.vitim.us
* @param {element} elm DOM Element to write the framerate
* @param {number} refresh Updates per second of the DOM
* @param {function} callback Function called on every DOM update
* @return {function} Returns a function that will be called inside the loop
*/
function fpsMeter(elm, refresh, callback){
//var elm; //element
@victornpb
victornpb / Stopwatch.js
Created April 23, 2014 04:24
Stopwatch constructor to measure the elapsed time
/** Stopwatch constructor to measure the elapsed time
* @author Victor B - www.vitim.us
*/
function Stopwatch(){
this.running = false;
this.startTimestamp;
this.endTimestamp;
}
Stopwatch.prototype.start = function(){
@victornpb
victornpb / MyConstructor.js
Created April 23, 2014 04:27
Javascript Constructor template
var MyConstructor = (function(){
function Constructor(options){
this.name = "foo";
}
//methods
Constructor.prototype = {
getName: function(){
@victornpb
victornpb / ajaxLoad.js
Created April 23, 2014 04:45
ajaxLoad(element, url, parameters)
function ajaxLoad(element, url, parameters){
if(typeof element=="string") element=document.querySelector(element);
var Ajax=(function(){
try{return new XMLHttpRequest()}
catch(e){try{return new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){try {return new ActiveXObject("Microsoft.XMLHTTP")}
catch(e){ return null; }}}
})();
if(Ajax){
@victornpb
victornpb / DomReady.js
Created April 23, 2014 04:55
DomReady
var DomReady = new function(){
var listeners = [];
var done = 0;
function onDomReady(e){
if(e.type=='readystatechange' && (document.readyState!='interactive' && document.readyState!='complete')) return;
if(!done++)
for(var i=0;i<listeners.length;i++){
@victornpb
victornpb / insaneLoop.js
Created June 27, 2014 05:26
insaneLoop
/**
* An alternative to setInterval and requestAnimationFrame that will call insanely fast for testing purposes.
* This will alternate between syncronous/blocking and assyncrounous non-blocking execution.
* Be very careful using this, this can freeze your browser.
*
* Your callback should return true in order to keep the loop running, return false to stop.
*/
function insaneLoop(callback, maxBlockingExecutionTime, assyncInterval){
maxBlockingExecutionTime = maxBlockingExecutionTime | 50;