Skip to content

Instantly share code, notes, and snippets.

View rvighne's full-sized avatar

Rohit Vighne rvighne

View GitHub Profile
@rvighne
rvighne / extend.js
Last active January 4, 2016 17:19
The modern way to deep-merge objects. It cleanly attaches itself to all Objects without showing up in `for...in` loops and modifies the object being copied into.
/*
Copyright (c) 2014 Rohit Vighne
License: The MIT License (MIT)
*/
Object.defineProperty(Object.prototype, "extend", {
value: function() {
for (var i = arguments.length; i--;) {
var obj = arguments[i];
for (var j in obj) {
@rvighne
rvighne / codearea.js
Created January 28, 2014 18:03
Automatically set readonly textareas to select their contents on focus -- great for code copy-and-paste. Works on all modern browsers and IE 8+, but it's trivial to backport it.
/*
Copyright (c) 2014 Rohit Vighne
License: The MIT License (MIT)
*/
(function (window, document) {
function select () {
this.select();
}
@rvighne
rvighne / canvasnosmoothing.js
Last active January 4, 2016 20:59
Most browsers do not make it easy to draw perfect 1-pixel-wide lines; usually they are two pixels wide and weaker than the color you want. This little hack fixes that and some image smoothing problems.
function pixelPerfect(ctx) {
ctx.translate(0.5, 0.5); // so pixels with odd coordinates snap to nearest physical pixel
ctx.imageSmoothingEnabled = false; // may need polyfill for safari
}
// Call pixelPerfect with your canvas context (obtained by getContext) as argument, and happy whole-pixel drawing!
@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