Skip to content

Instantly share code, notes, and snippets.

@digitalicarus
digitalicarus / calcFrameTime.js
Created January 18, 2015 19:52
average frame time
function flashFrameTime (counts, cb) {
if (!typeof counts === 'number' || !counts > 0) { throw "no!"; }
var accum = []
, reqFrame = window.requestAnimationFrame
;
function calcFramerate () {
return accum
.slice(0, accum.length-1)
@digitalicarus
digitalicarus / fiddle.html
Last active August 30, 2015 19:12 — forked from RyanAtViceSoftware/fiddle.html
Hello React - composite components - how to componse simple components. JsFiddle: http://jsfiddle.net/gh/gist/library/pure/27ec782a76990860929b/
<!-- Required for using JSX in JsFiddle -->
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script>
<div id="view"/>
@digitalicarus
digitalicarus / wee.js
Created May 4, 2012 21:00
JavaScript Game Loop
//http://www.gamedev.net/topic/579738-web-javascript-game-loop/
//http://dev.opera.com/articles/view/framerate-control-system-for-javascript/
//http://jsfiddle.net/digitalicarus/4kshN/3/
var WEE = {
FPS: 60,
rate: 60,
dt: 1/60,
accrued: 0.0,
last: Date.now()/1000,
@digitalicarus
digitalicarus / canvas_round_rect.js
Created June 29, 2012 03:01
canvas rounded rectangle
var roundRect = function(ctx,o) {
if(!ctx || !o.x || !o.y || !o.w || !o.h || !o.r) {
return;
}
var x = o.x,
y = o.y,
w = o.w,
h = o.h,
r = o.r,
@digitalicarus
digitalicarus / midpointDisplacement
Created July 5, 2012 02:37
2d midpoint displacement
/* http://www.gameprogrammer.com/fractal.html */
// Also, as expected, shifting is waaay faster than division, even in JS :) http://jsperf.com/division-vs-shift unfortunately it also truncates to the mantissa of floating points
// http://jsfiddle.net/digitalicarus/YUDgH/24/
// TODO: abs option, obj args, exception reason
var calcTerrain = function(conf) {
var height = conf.height,
swing = conf.swing,
h = conf.decay || conf.smooth || conf.smoothness,
i = conf.degree,
lb = (height-swing < 0) ? conf.lowerBound : null,
@digitalicarus
digitalicarus / smartReveal
Created September 17, 2012 18:52
fadey height
(function($){
$.fn.voila = function(content,cb) {
return this.each(function() {
var $this = $(this);
if($this.is(":visible")) {
//-- http://stackoverflow.com/questions/4085800/jquery-animate-changes-in-div-height-on-ajax-l
console.log("voila: origHeight="+$this.height());
$this
.animate({height: $this.height(), opacity: 'hide'}, 'fast', function(){
@digitalicarus
digitalicarus / callAsyncAndJoin
Created September 24, 2012 20:58
calls array of async functions and invokes callback when all done - shindig
/**
* Calls an array of asynchronous functions and calls the continuation
* function when all are done.
* @param {Array} functions Array of asynchronous functions, each taking
* one argument that is the continuation function that handles the result
* That is, each function is something like the following:
* function(continuation) {
* // compute result asynchronously
* continuation(result);
@digitalicarus
digitalicarus / regexen
Last active October 11, 2015 12:08
regexen
//-- JS number
[-+]?(?:(?:\d+)|(?:\d+\.\d+)|(?:\.\d+))(?:e[-+]?\d+)?
//-- vim non-greedy multiline http://www.fumbling-in-the-dark.com/2011/08/non-greedy-multi-line-search-and.html
%s/text1\_.\{-}text2/text3/
@digitalicarus
digitalicarus / faithplusone.js
Last active October 12, 2015 05:24
Faith+1
(function(){var t=function(r){var n,a=[],w=document.createTreeWalker(r,NodeFilter.SHOW_TEXT,null,false);while(n=w.nextNode()) a.push(n);return a;},i,n=t(document.body); for(i=0;i<n.length;i++) n[i].nodeValue=n[i].nodeValue.replace(/baby|girl/ig,'Jesus').replace(/ her /ig, ' Him ').replace(/ she /ig, ' He ');})()
@digitalicarus
digitalicarus / loader.js
Created December 1, 2012 05:02
load js and css
function asyncLoader (s, cb) {
var d = document
, h = document.getElementsByTagName('head')[0]
, b = (Math.random()*1e8>>0)
, i = 'loader_'+b
, t = (/\.js$/.test(s)) ? "js" : "css"
, j = (t === 'js') ? d.createElement('script') : d.createElement('link');
j.id = i;