Skip to content

Instantly share code, notes, and snippets.

View IceCreamYou's full-sized avatar

Isaac Sukin IceCreamYou

View GitHub Profile
@IceCreamYou
IceCreamYou / pcrbetasearch.js
Created November 1, 2011 00:01
Penn Course Review Beta - Keyboard Shortcut to Search
// Basically copied directly from http://www.catswhocode.com/blog/using-keyboard-shortcuts-in-javascript
var isCtrl = false;
$(document).keyup(function (e) {
if (e.which == 17)
isCtrl = false;
}).keydown(function (e) {
if (e.which == 17)
isCtrl = true;
if (e.which == 83 && isCtrl == true) {
$('#searchbox').focus().select();
@IceCreamYou
IceCreamYou / smiley-slider-snippet.php
Created November 13, 2011 10:18
Put this in a Drupal node and it creates a "smiley slider" as a Drupal form that degrades gracefully.
<!--break-->
<!-- script from https://github.com/dglittle/smiley-slider -->
<script type="text/javascript" src="https://raw.github.com/dglittle/smiley-slider/master/smiley-slider.js"></script>
<script>
jQuery(document).ready(function() {
var s = new SmileySlider(document.getElementById("slider"), 'https://github.com/dglittle/smiley-slider/raw/master/smiley-slider.png');
s.position(0.5);
s.position(function (p) {
jQuery('.smiley-slider').val(Math.round(p*10));
});
@IceCreamYou
IceCreamYou / JS Log Periodically
Created December 15, 2012 02:35
A JavaScript function to log messages to the console periodically.
/**
* Periodically log a message to the JavaScript console.
*
* This is useful for logging things in loops; it avoids being overwhelmed by
* an unstoppable barrage of similar log messages. Example calls:
*
* # Log "message" to the console no more than every 500ms.
* logPeriodically('message', 500);
* # Log "message" as an error no more than every 500ms.
* logPeriodically('message', 500, console.error);
@IceCreamYou
IceCreamYou / gist:5131253
Created March 11, 2013 00:59
Experimentally verify that if you generate 9 numbers at a time between 0 and 1 and sort them, the first number will average out to 0.1, the second to 0.2, etc.
// e.g. round(3.456, 1) -> 3.5
Number.prototype.round = function(v, a) {
if (typeof a === 'undefined') {
a = v;
v = this;
}
if (!a) a = 0;
var m = Math.pow(10,a|0);
return Math.round(v*m)/m;
};
/**
* @param lo The closest previous input below the current input
* @param hi The closest previous input above the current input
* @param loPos The position of lo
* @param hiPos The position of hi
* @param input The current input
* @return The optimal position for input
*/
function optimalGuess(lo, hi, loPos, hiPos, input) {
// Track the best values
@IceCreamYou
IceCreamYou / Inc 500 TSV
Created March 20, 2013 09:35
This script scrapes the Inc 500 data and turns it into a tab-separated values (TSV) string which can be easily imported into Excel or a database. To run it, go to http://www.inc.com/inc5000/list/2012 and execute the code in your browser's JavaScript console. Be nice! This hits the Inc site 50 times in quick succession. I don't know who technical…
/**
* Get an array containing the text nodes within a DOM node.
*
* Modified from http://stackoverflow.com/a/4399718/843621
*
* @param node Any DOM node.
* @param [includeWhitespaceNodes=false] Whether to include whitespace-only nodes.
* @param [recurse=false] Whether to get all nodes (true) or only the immediate child nodes (false).
* @return An array containing TextNodes.
*/
@IceCreamYou
IceCreamYou / Batch.js
Created June 24, 2013 05:01
Execute a collection of tasks together at a later time.
/**
* Execute a collection of tasks together at a later time.
*
* This is occasionally useful when animating a lot of primitives on a Canvas
* since batching certain operations that change the canvas state can
* significantly improve drawing time (especially in older browsers).
*/
function Batch(before, after) {
var tasks = [];
this.addTask = function(task) {
@IceCreamYou
IceCreamYou / Stopwatch.js
Created July 22, 2013 01:40
A timer that can be started and stopped and will tell you how long something took. Adapted from https://github.com/IceCreamYou/HTML5-Canvas-Game-Boilerplate/blob/gh-pages/js/boilerplate/core.js#L774
// performance.now() shim
window.performance = window.performance || {};
performance.now = (function() {
return performance.now ||
performance.mozNow ||
performance.msNow ||
performance.oNow ||
performance.webkitNow ||
function() { return Date.now(); };
})();
@IceCreamYou
IceCreamYou / Layer.js
Created July 22, 2013 06:00
A Layer object (basically a utility canvas, useful for caching intermediate graphics buffers). Adapted from https://github.com/IceCreamYou/HTML5-Canvas-Game-Boilerplate/blob/gh-pages/js/boilerplate/drawing.js#L7
/**
* The Layer object (basically a new, utility canvas).
*
* Layers allow efficient rendering of complex scenes by acting as caches for
* parts of the scene that are grouped together. For example, it is recommended
* to create a Layer for your canvas's background so that you can render the
* background once and then draw the completely rendered background onto the
* main canvas in each frame instead of re-computing the background for each
* frame. This can significantly speed up animation.
*
/**
* Calculates the Damerau-Levenshtein distance between two strings.
*/
function distance(source, target) {
if (!source) return target ? target.length : 0;
else if (!target) return source.length;
var m = source.length, n = target.length, INF = m+n, score = new Array(m+2), sd = {};
for (var i = 0; i < m+2; i++) score[i] = new Array(n+2);
score[0][0] = INF;