Skip to content

Commit

Permalink
update readme + add api
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-mouland committed Nov 23, 2016
1 parent d248e81 commit c55eb10
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
trim_trailing_whitespace = true

[*.js]
indent_size = 2
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.idea
node_modules
*.log
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# node-resemble-v2
node-resemble-v2
=============

A new Node port of [Resemble.js v2](https://github.com/Huddle/Resemble.js).

##Installation

```bash
$ npm i node-resemble-v2
```

##Prerequisites

[Cairo](http://cairographics.org/) is required for [node-canvas](https://github.com/Automattic/node-canvas), installation instructions can be found node-canvas [Wiki](https://github.com/Automattic/node-canvas/wiki/_pages).

##Example

```js
var fs = require('fs');

var resemble = require('node-resemble-v2');

var github_img1 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC/xhBQAAAJlQTFRFJiYmJiYmJSUlJiYmKysrJiYmJSUlJiYmJSUlJCQkJiYmJycnJiYmJiYmJSUlJiYmJSUlJiYmJSUlJCQkJycnJCQkJycnJiYmJSUlJiYmJiYmJycnJiYmJiYmJSUlJycnJycnJCQkJSUlJiYmJiYmJycnJiYmJycnJiYmJSUlJiYmJiYmJycnJiYmEhISISEhJycnAAAAJiYmQmWwmAAAADJ0Uk5T9JNotgyiTK2KR0P7Zij+PYni/StBMdmBpKnq5tbw1VXsKu9RUM3O7cl8V5ql3w42+gB0ejcrAAAApElEQVQY012PRRIEMQwDs8zMOAzhyP9/3CYDl9XJ7irZEoOXldo5LW2YGWAUlVwIXpIyAZi6ytAor2rjgao2pgXjpFJglvLLdrbez4eT1TIny2QJS50WKCXTHLceDMA1cwLXHsQQjrkIhx4wRC5YRj2og0UW2Ll2P05RSP82g72/iJ6PMzL/FipOgIJIA6cQzEdPBT7eAZE20UO5b7jy7sr91f8BZR4qkBlEJUYAAAAASUVORK5CYII=';

var github_img2 = fs.readFileSync('./example.png');

resemble(github_img1).onComplete(function(data){
console.log(data);
});

resemble(github_img1).compareTo(github_img2).onComplete(function(data){
console.log(data);
});
```
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const canvas = require('canvas');
const imageType = require('image-type');

function overrideWindow() {
const doc = {};
doc.createElement = function createElement(tag) {
return (tag !== 'canvas')
? false
: new canvas();
};
return {
document: doc
};
}

function overrideFileReader() {
const FileReader = function FileReader() { };
FileReader.prototype.readAsDataURL = function readAsDataURL(buffer) {
this.onload({
target: {
result: "data:image/" + imageType(buffer) + ";base64," + buffer.toString('base64') + ""
}
});
};
return FileReader;
}

function overrideImage() {
const Image = canvas.Image;
Image.prototype.setAttribute = function setAttribute() { };
return Image;
}

module.exports = function exports() {
if (typeof window === 'undefined') {
global.window = overrideWindow();
}
if (typeof FileReader === 'undefined') {
global.FileReader = overrideFileReader();
}
if (typeof Image === 'undefined') {
global.Image = overrideImage();
}
const resemblejs = require('resemblejs'); // eslint-disable-line global-require
return resemblejs.apply(this, arguments); // eslint-disable-line prefer-rest-params
};
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "node-resemble-v2",
"version": "1.0.0",
"description": "Node API for ResembleJS (v2)",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/peter-mouland/node-resemble-v2.git"
},
"keywords": [
"resemblejs"
],
"author": "Peter Mouland",
"license": "ISC",
"bugs": {
"url": "https://github.com/peter-mouland/node-resemble-v2/issues"
},
"homepage": "https://github.com/peter-mouland/node-resemble-v2#readme",
"dependencies": {
"canvas": "^1.6.2",
"image-type": "^2.1.0",
"resemblejs": "^2.2.2"
}
}

0 comments on commit c55eb10

Please sign in to comment.