Skip to content

Instantly share code, notes, and snippets.

View rvighne's full-sized avatar

Rohit Vighne rvighne

View GitHub Profile
@rvighne
rvighne / crop.js
Created February 8, 2014 06:30
Crop an image (or compatible object) to certain dimensions. Works in canvas-supporting browsers, works only on the same origin. It is asynchronous due to the nature of the JavaScript image loading method.
/*
Copyright (c) 2014 Rohit Vighne
License: The MIT License (MIT)
*/
var crop = (function(document) {
"use strict";
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
@rvighne
rvighne / shuffle.js
Created February 8, 2014 20:03
Most obfuscated function to shuffle an array, ever. Adapted from this code: http://jsfromhell.com/array/shuffle. There are no guarantees that it is a good approach; see https://gist.github.com/rohit-vighne/6946301 for something more practical.
function shuffle(v){for(var j,x,i=v.length;i;j=~~(Math.random()*i),x=v[--i],v[i]=v[j],v[j]=x);}
@rvighne
rvighne / deltaNow.js
Created February 17, 2014 02:47
Chooses the highest precision and fastest method to get a timestamp for computing a delta.
var now;
try {
now = performance.now.bind(performance);
} catch (e) {
if (Date.now) {
if (Date.now.bind) {
now = Date.now.bind(Date);
} else {
now = function() {
return Date.now();
@rvighne
rvighne / markthru.js
Last active August 29, 2015 14:00
Markthru interleaves the given character (specified by Unicode) between the characters of a string. It's especially useful for applying Unicode combining characters to a string, and in fact defaults to striking the text out.
function markthru (str, char) {
char = String.fromCharCode(char || 822);
return char + str.split('').join(char);
}
// A.K.A. "strikethrough" when second argument is omitted.
@rvighne
rvighne / barely-valid.html
Created May 31, 2014 05:15
How to make all your friends twitch in horror. This is a perfectly valid HTML5 document that defies all conventions about start and end tags. Feel free to fork it.
<!DOCTYPE html>
<title>A Perfectly Valid HTML Document</title>
<meta charset="utf-8">
<p>A paragraph
<p>Another paragraph
<p>The last paragraph
<ul>
<li>A list
<li>Green eggs
@rvighne
rvighne / string-extras.js
Created June 19, 2014 21:31
Highly performant ES6 String extras polyfills for modern browsers.
if (!('startsWith' in String.prototype))
Object.defineProperty(String.prototype, 'startsWith', {
value: function (searchString, position) {
if (Object.prototype.toString.call(searchString) === "[object RegExp]")
throw new TypeError("The search string cannot be a regular expression.");
if (position === undefined)
position = 0;
else
position = Math.min(Math.max(position, 0), this.length);
return this.lastIndexOf(searchString, position) === position;
@rvighne
rvighne / scrollend.js
Last active August 29, 2015 14:04
Fires an event "scrollend" on the window object when the user has stoppped scrolling -- very useful for saving resources in scroll-focused applications.
/*
The MIT License (MIT)
Copyright (c) 2014 Rohit Vighne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@rvighne
rvighne / isWeekday.js
Created July 23, 2014 22:39
Checks whether a Date object represents a weekday, where a weekday is any of the days Monday to Friday.
Object.defineProperty(Date.prototype, 'isWeekday', {
value: function(UTC) {
var day = (UTC ? this.getUTCDay : this.getDay)();
if (day === 0 || day === 6)
return false;
else
return true;
},
writable: true, configurable: true
@rvighne
rvighne / deepFreeze.js
Last active August 29, 2015 14:04
Deep-freeze an object (freeze the object and all objects inside it). See the native Object.freeze, which only does a shallow freeze. It is safe against circular structures.
Object.deepFreeze = function deepFreeze(obj) {
this.freeze(obj)
for (var key in obj) {
var prop = obj[key]
if (obj.hasOwnProperty(key) && prop instanceof this && !this.isFrozen(prop))
this.deepFreeze(prop)
}
return obj
@rvighne
rvighne / favicanvas.js
Created August 3, 2014 19:26
Exposes a global canvas context that renders to the page's favicon. Works in all browsers supporting Canvas 2D.
/*
The MIT License (MIT)
Copyright (c) 2014 Rohit Vighne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is