Created
December 5, 2016 23:42
-
-
Save ngscheurich/82fc932969d9b4a8d3df3a5d9d12e23d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const glitch = require('glitch-canvas'); | |
const canvasCover = require('./lib/canvas-cover.js'); | |
const canvas = document.getElementById('canvas'); | |
sizeCanvas(); | |
const ctx = canvas.getContext('2d'); | |
const image = new Image(); | |
image.src = '/assets/images/stock.jpg'; | |
image.onload = () => { glitchImage(); }; | |
window.onresize = sizeCanvas; | |
function sizeCanvas() { | |
canvas.width = window.innerWidth; | |
canvas.height = window.innerHeight; | |
} | |
function glitchImage() { | |
let params = { | |
seed: randomFromRange(0, 99), | |
quality: randomFromRange(0, 99), | |
amount: randomFromRange(0, 99), | |
iterations: randomFromRange(0, 99) | |
}; | |
let duration = randomFromRange(0, 500); | |
let countdown = randomFromRange(0, 5000); | |
glitch(params) | |
.fromImage(image) | |
.toImage() | |
.then((glitchedImage) => { | |
drawScaledImage(glitchedImage); | |
window.setTimeout(() => { drawScaledImage(image); }, duration); | |
window.setTimeout(() => { glitchImage(); }, countdown); | |
}); | |
} | |
function randomFromRange(min, max) { | |
return Math.random() * (max - min) + min; | |
} | |
function drawScaledImage(image) { | |
canvasCover.drawImageProp(ctx, image, 0, 0, canvas.width, canvas.height); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment