lorumipsum glitch
by edwardsharp
HTML
<div id="example5" class="example">
<h1>Glitch Transition (different size elements)</h1>
<div class="source">
<h2>Page 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="middle"><button class="btn">Glitch</button></div>
<div class="target"></div>
</div>
JavaScript
/**
@license glitch.js v0.1 <http://www.github.com/sjhewitt/glitch.js>
Released under MIT License
Copyright (c) 2012 Simon Hewitt.
http://www.twitter.com/sjhewitt
*/
(function($){
/*global html2canvas */
var noop = function(){},
/**
* Set default properties on an object
* @param {Object} obj The target object
* @param {Object} defaults The default properties
* @return {Object} The target obj
*/
defaults = function(obj, defaults) {
for (var prop in defaults) {
if (obj[prop] == null)
obj[prop] = defaults[prop];
}
return obj;
},
/**
* Generates an integer between min and max
*
* @param {Number} min The lower bound
* @param {Number} max The upper bound
* @return {Number} A random number
*/
getRandInt = function(min, max) {
return (Math.floor(Math.random() * (max - min) + min));
};
/**
* Apply the glitch effect to a canvas object
*
* @param {HTMLCanvasElement} canvas The canvas (or HTMLImageElement) to apply the glitch to
* @param {number} amount The amount to glitch the canvas (default: 6)
* @return {HTMLCanvasElement} A canvas containing a glitched version
* of the original canvas
*/
var _glitch = function(canvas, amount) {
var
// cache the width and height of the canvas locally
x, y, w = canvas.width, h = canvas.height,
// _len is an iterator limit, initially storing the number of slices
// to create
i, _len = amount || 6,
// pick a random amount to offset the color channel
channelOffset = (getRandInt(-_len*2, _len*2) * w * + getRandInt(-_len, _len)) * 4,
// the maximum amount to offset a chunk of the image is a function of its width
maxOffset = _len * _len / 100 * w,
// vars for the width and height of the chunk that gets offset
chunkWidth, chunkHeight,
// create a temporary canvas to hold the image we're working on
tempCanvas =...