JSFiddle - React, Tailwind, and code Playground
HTML
<img src="http://4.bp.blogspot.com/_3acdvEQmam8/TOQGl4Rt1SI/AAAAAAAAAFs/edj0DwqAvBs/s1600/dali-illusion.jpg" />
CSS
body { background: wheat; }
JavaScript
(function (exports) {
'use strict';
exports.mask || (exports.mask = init);
function init(selector, bgColor, steps) {
if (hasCanvas) {
Array.prototype.forEach.call(
document.querySelectorAll(selector),
function (img) {
createMask(img, bgColor, steps);
}
);
}
}
function hasCanvas() {
var canvas = document.createElement('canvas');
return 'function' === typeof canvas.getContext;
}
function rand() {
return Math.random();
}
function createMask(img, bgColor, steps) {
var w = img.clientWidth,
h = img.clientHeight,
cl = img.className;
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');
canvas.width = w;
canvas.height = h;
cl && (canvas.className = cl);
var container = img.parentNode;
container.insertBefore(canvas, img);
container.removeChild(img);
ctx.drawImage(img, 0, 0, w, h);
drawSaw(ctx, w, h, bgColor, steps);
}
function drawSaw(ctx, w, h, bgColor, steps) {
bgColor || (bgColor = '#fff');
steps || (steps = 50);
var stepY = Math.ceil(Math.max(w, h) / steps),
stepX = stepY * 2,
i;
ctx.beginPath();
var x = 0, y = 0;
ctx.moveTo(x, y);
for (i = 0; i < steps; i++) {
y = y ? 0 : stepY * rand();
x += stepX
ctx.lineTo(x, y);
}
y = 0, x = 0;
ctx.moveTo(x, y);
for (i = 0; i < steps; i++) {
x = x ? 0 : stepY * rand();
y += stepX;
ctx.lineTo(x, y);
}
x = 0, y = h;
ctx.moveTo(x, y)
for (i = 0; i < steps; i++) {
y = y == h ? h - stepY * rand() : h;
x += stepX;
ctx.lineTo(x, y);
}
x = w, y...