Skip to content

Instantly share code, notes, and snippets.

View rvighne's full-sized avatar

Rohit Vighne rvighne

View GitHub Profile
@rvighne
rvighne / pixel8.js
Last active April 28, 2020 13:29
Pixel8 is as easy way to get the raw pixel data of an image, canvas, or video from the same domain. It uses the HTML5 Canvas's getImageData method, so Internet Explorer is only supported through FlashCanvas (http://flashcanvas.net). Simply call something like `var data = pixel8(image)` and then use `data.pixelAt(x, y)` to get a specific pixel's …
/*
Copyright (c) 2013 Rohit Vighne
License: The MIT License (MIT)
*/
function pixel8(image, x, y, w, h) {
"use strict";
// Image must be an image, canvas, or video
// For videos: Pixel data of the currently displayed frame will be extracted
@rvighne
rvighne / bugbot.js
Last active December 20, 2015 22:39
BugBot is an automatic JavaScript error logger. I hacked it up to record errors in an alpha version of a game I'm working on, so I don't have to test it to death before releasing. Use it by instantiating a BugBot object with a url to a server-side logging script like: `var bot = new BugBot("logger.php");`, and then calling `bot.start` whenever y…
/*
Copyright (c) 2013 Rohit Vighne
License: The MIT License (MIT)
*/
(function (window, document, Date, undefined) {
"use strict";
function createAJAXObject () {
if (window.XMLHttpRequest) return new XMLHttpRequest();
@rvighne
rvighne / exec2.js
Last active December 21, 2015 13:48
The default RegExp.exec method is terribly designed, what with having to loop for global patterns, the full regex coming before capture groups, etc. I have devised a solution: the (imaginatively named) exec2. Start by doing something like "/ab/g.exec2('abcdefabuf')"Example usage included at the bottom.THIS IS AN ALPHA VERSION! Please report bugs…
/*
Copyright (c) 2013 Rohit Vighne
License: The MIT License (MIT)
*/
RegExp.prototype.exec2 = function(str) {
if (this.global) {
var result = [];
var tm;
@rvighne
rvighne / Array.cycle.js
Last active December 21, 2015 15:58
This quick Array extension simply moves each element in the array to the left by one, then moves the displaced first element to the end, returning the new first element. Use cases may be quick-and-dirty cycling through background colors.
/*
Copyright (c) 2013 Rohit Vighne
License: The MIT License (MIT)
*/
Array.prototype.cycle = function() {
return this[this.push(this.shift()), 0];
};
/*
@rvighne
rvighne / parseQuery.js
Last active December 23, 2015 16:39
Gets the query string of a page from JavaScript as a easy-to-use object, with Javascript-style values already parsed. It's useful for adding some quick configurability to a webapp.
/*
Copyright (c) 2013 Rohit Vighne
License: The MIT License (MIT)
*/
(function(window, location) {
var re = /(&|\?)([^&?=]+)=([^&?=]+)/g;
var fre = /^function\s*\w*\(([^)]*)\)\s*\{([^}]*)\}$/;
@rvighne
rvighne / Array.shuffle.js
Last active December 25, 2015 08:19
Shuffles an array in-place, then returns the new array.
/*
Copyright (c) 2013 Rohit Vighne
License: The MIT License (MIT)
*/
Array.shuffle = function (array) {
var top, i, temp;
top = array.length;
while (top !== 0) {
@rvighne
rvighne / addEventListeners.js
Last active January 1, 2016 08:09
Adds the same event listener to multiple elements, by CSS selector or by array.
/*
Copyright (c) 2013 Rohit Vighne
License: The MIT License (MIT)
*/
function addEventListeners (sel, evt, fun, cap) {
var els;
if (sel.length) els = sel;
else document.querySelectorAll(sel);
@rvighne
rvighne / drawHTML.js
Last active January 3, 2016 17:49
Draw valid XHTML or a DOM element into a 2D canvas. Works on all modern browsers. Asynchronous but callback supported. Requires well-formed XHTML (circumventable by passing a DOM element or otherwise cleansing the source).
/*
Copyright (c) 2014 Rohit Vighne
License: The MIT License (MIT)
*/
CanvasRenderingContext2D.prototype.drawHTML = function(src, x, y, cb) {
if (src.nodeType) src = src.outerText;
var svg = new Blob(["\
<svg xmlns='http://www.w3.org/2000/svg' width='" + this.canvas.width + "' height='" + this.canvas.height + "'>\
@rvighne
rvighne / pwd.js
Last active January 4, 2016 10:48
Get the URL of the directory of the page that included this JavaScript, including the last "/".
/*
Copyright (c) 2014 Rohit Vighne
License: The MIT License (MIT)
*/
function pwd () {
return location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1);
}
var getCurrentDirectory = pwd;
@rvighne
rvighne / prefixProp.js
Last active January 4, 2016 14:49
Get the appropriate CSS property to use, for example, in setting styles dynamically. Takes an unprefixed property as argument. Extremely fast and minifies easily.
/*
Copyright (c) 2014 Rohit Vighne
License: The MIT License (MIT)
*/
var prefixProp = function(document, undefined) {
var test = document.createElement('div');
var prefixes = ["-webkit-", "-moz-", "-ms-", "-o-"];
var length = prefixes.length;