Skip to content

Instantly share code, notes, and snippets.

@raydog
Last active March 24, 2016 23:06
Show Gist options
  • Save raydog/f2e3ced54eb680b8ec8e to your computer and use it in GitHub Desktop.
Save raydog/f2e3ced54eb680b8ec8e to your computer and use it in GitHub Desktop.
Creates one fucked up gif. Requires imagemagick with liblqr enabled.
#!/usr/bin/env node
var im = require('imagemagick');
var async = require('async');
var temp = require('temp').track();
var ProgressBar = require('progress');
// Constants:
var FRAMES = 100;
var MIN_DISTORT = 90; // 1-100 amount of distortion for "least distorted". Higher numbers distort less.
var MAX_DISTORT = 20; // 1-100 amount of distortion for "most distorted". Lower numbers distort more.
var RESULT_SCALE = "x400"; // Size to scale the result picture to. (x400 == 400 pixels high)
var DELAY = "15"; // Time (in 1/100ths of a sec) between frames
var FUZZ_FACTOR = "1%"; // The greater this number, the more close colors distort in the result.
// Calculated from the above:
FRAMES = Math.floor(FRAMES / 2);
var DISTORT_DIFF = MAX_DISTORT - MIN_DISTORT;
var FRAMES_2 = FRAMES / 2;
var args = process.argv;
if (args.length !== 4) {
console.error("Usage: distortpulse <source> <dest>");
process.exit(1);
}
var infile = args[2];
var outfile = args[3];
console.log("IN ", infile);
console.log("OUT", outfile);
var scales = [];
for (var i=0; i<FRAMES; i++) {
var scaled = _cubicEasing(i);
scales.push(scaled.toFixed(3) + "%");
}
var bar = new ProgressBar('Generating [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 50,
total: FRAMES
});
var paths = [];
async.eachSeries(
scales,
function forEachFrame(scale, next) {
temp.open({prefix: 'distortpulse', suffix: '.png'}, function (err, temp_file) {
if (err) { throw err; }
im.convert([infile, "-liquid-rescale", scale, '-resize', RESULT_SCALE, temp_file.path], function (err) {
if (err) { throw err; }
bar.tick();
paths.push(temp_file.path);
next();
});
});
},
function allDone(err) {
if (err) { throw err; }
var final_args = ['-delay', DELAY, '-loop', 0]
.concat(paths)
.concat(paths.slice().reverse())
.concat(['-fuzz', '1%', '-layers', 'remove-dups', '-layers', 'optimize'])
.concat([outfile]);
im.convert(final_args, function (err) {
if (err) { throw err; }
console.log("\n\nDone:", outfile);
});
}
);
function _cubicEasing(t) {
if ((t/=FRAMES_2) < 1) return DISTORT_DIFF/2*t*t*t + MIN_DISTORT;
return DISTORT_DIFF/2*((t-=2)*t*t + 2) + MIN_DISTORT;
}
{
"name": "distortpulse",
"version": "1.0.0",
"description": "WTF",
"main": "distortpulse.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ray Myers",
"license": "MIT",
"dependencies": {
"async": "1.5.0",
"imagemagick": "0.1.3",
"progress": "1.1.8",
"temp": "0.8.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment