Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriopvl committed Sep 24, 2015
0 parents commit 54987a9
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
46 changes: 46 additions & 0 deletions 8bit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 8bit
*
* A function that converts an image into a pixelated version (just like
* 8bit artwork).
*
* @author rogeriopvl <https://github.com/rogeriopvl>
* @license MIT
*/

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.eightBit = factory();
}
} (this, function () {
/**
* Draws a pixelated version of an image in a given canvas
* @param {object} canvas - a canvas object
* @param {object} image - an image HTMLElement object
* @param {number} scale - the scale factor: between 0 and 100
*/
var eightBit = function (canvas, image, scale) {
scale *= 0.01;

canvas.width = image.width;
canvas.height = image.height;

var scaledW = canvas.width * scale;
var scaledH = canvas.height * scale;

var ctx = canvas.getContext('2d');

ctx.mozImageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;

ctx.drawImage(image, 0, 0, scaledW, scaledH);
ctx.drawImage(canvas, 0, 0, scaledW, scaledH, 0, 0, image.width, image.height);
};

return eightBit;
}));
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 8bit
*
* A module that converts an image into a pixelated version (just like
* 8bit artwork).
*
* @author rogeriopvl <https://github.com/rogeriopvl>
* @license MIT
*/

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.eightBit = factory();
}
} (this, function () {
/**
* Draws a pixelated version of an image in a given canvas
* @param {object} canvas - a canvas object
* @param {object} image - an image HTMLElement object
* @param {number} scale - the scale factor: between 0 and 100
*/
var eightBit = function (canvas, image, scale) {
scale *= 0.01;

canvas.width = image.width;
canvas.height = image.height;

var scaledW = canvas.width * scale;
var scaledH = canvas.height * scale;

var ctx = canvas.getContext('2d');

ctx.mozImageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;

ctx.drawImage(image, 0, 0, scaledW, scaledH);
ctx.drawImage(canvas, 0, 0, scaledW, scaledH, 0, 0, image.width, image.height);
};

return eightBit;
}));
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "8bit",
"version": "1.0.0",
"description": "Transform images into pixelated versions",
"main": "8bit.js",
"scripts": {
"test": "tape test/*.js"
},
"keywords": [
"eightbit",
"8bit",
"pixelize",
"pixelate",
"pixels",
"image",
"canvas"
],
"homepage": "https://rogeriopvl.github.io/8bit",
"repository": {
"type": "git",
"url": "https://github.com/rogeriopvl/8bit"
},
"author": "rogeriopvl",
"license": "MIT",
"devDependencies": {
"tape": "^4.2.0"
}
}
19 changes: 19 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>8bit test</title>
</head>
<body>
<canvas id="canvas"></canvas>

<script src="../8bit.js"></script>
<script>
var img = new Image();
img.onload = function () {
eightBit(document.getElementById('canvas'), img, 10);
};
img.src = 'test.png';
</script>
</body>
</html>
45 changes: 45 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var test = require('tape');

var eightBit = require('../8bit.js');

test('exists as a function', function (t) {
t.plan(1);
t.equals(typeof eightBit, 'function');
});

test('passed canvas is resized to the image size', function (t) {
t.plan(2);

var fakeCanvas = {
getContext: function () {
return {
drawImage: function () {}
};
}
};
var fakeImage = { width: 300, height: 200 };

eightBit(fakeCanvas, fakeImage, 30);

t.equals(fakeCanvas.width, 300);
t.equals(fakeCanvas.height, 200);
});

test('should call context.drawImage two times', function (t) {
t.plan(1);

var fakeCanvas = {
drawImageCalls: 0,
getContext: function () {
var self = this;
return {
drawImage: function () { self.drawImageCalls++; }
};
}
};
var fakeImage = { width: 300, height: 200 };

eightBit(fakeCanvas, fakeImage, 30);

t.equals(fakeCanvas.drawImageCalls, 2);
});
Binary file added test/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 54987a9

Please sign in to comment.