particle-by-resize
Advanced tools
Comparing version 0.0.1 to 1.0.2
133
index.js
'use strict'; | ||
var _ = require('lodash'); | ||
var format = require('format'); | ||
var Canvas = require('canvas'); | ||
function ParticleByResize(options) { | ||
this.options = _.assign({ | ||
scale: 0.25 | ||
scale: 1 | ||
}, options); | ||
@@ -15,13 +13,13 @@ this.data = []; | ||
ParticleByResize.prototype.sampling = function (canvas) { | ||
ParticleByResize.prototype.sampling = function (imageSource) { | ||
var scale = this.options.scale; | ||
var cw = canvas.width * scale, // context width | ||
ch = canvas.height * scale; // context height | ||
var resizedCanvas = new Canvas(); | ||
var resizedCanvas = document.createElement('canvas'); | ||
var cw = Math.round(imageSource.width * scale); | ||
var ch = Math.round(imageSource.height * scale); | ||
resizedCanvas.width = cw; | ||
resizedCanvas.height = ch; | ||
var c = resizedCanvas.getContext('2d'); | ||
c.drawImage(canvas, 0, 0); | ||
c.drawImage(canvas, 0, 0, cw, ch); | ||
c.drawImage(imageSource, 0, 0, cw, ch); | ||
@@ -48,84 +46,67 @@ var data = c.getImageData(0, 0, cw, ch).data; | ||
ParticleByResize.prototype.collidedWith = function collidedWith(change, x, y) { | ||
var base = this; | ||
// @arg fn takes two data and a result as arguments. The result includes | ||
// key "done" to indicator the end of iterator and | ||
// key "data" for the return data | ||
function createIterator(fn) { | ||
var result = { | ||
done: false, | ||
data: null | ||
}; | ||
if (x < 0 || x + change.width > base.width || y < 0 || y + change.height > base.height) { | ||
throw new Error('out of bound'); | ||
} | ||
function createIndexInBg(background, particles, x, y) { | ||
return function indexInBg(i) { | ||
var xRel = i % particles.width; | ||
var yRel = i / particles.height; | ||
var indexInBg = (function (bg, pt, x, y) { | ||
return function (i) { | ||
var xRel = i % pt.width; | ||
var yRel = Math.floor(i / pt.width); | ||
var xBg = x + xRel; | ||
var yBg = y + yRel; | ||
if (xBg > background.width || xBg < 0 || yBg > background.height || yBg < 0) { | ||
throw new Error(format('out of bound: %d, %d', xBg, yBg)); | ||
if (xBg >= bg.width || xBg < 0 || yBg >= bg.height || yBg < 0) { | ||
return null; | ||
} | ||
return xBg + yBg * background.width; | ||
return Math.round(xBg + yBg * bg.width); | ||
}; | ||
} | ||
})(base, change, Math.floor(x), Math.floor(y)); | ||
return function(particles, x, y) { | ||
// reset the result when no argument given | ||
if (arguments.length === 0) { | ||
result.done = false; | ||
result.data = null; | ||
return result.data; | ||
var baseData = base.data; | ||
var chngData = change.data; | ||
for (var i = 0; i < chngData.length; i++) { | ||
var j = indexInBg(i); | ||
if (j !== null) { | ||
if (baseData[j] === 1 && chngData[i] === 1) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
if (isNaN(x) || isNaN(y)) { | ||
throw new Error(format('invalid offset: %d, %d', x, y)); | ||
} | ||
ParticleByResize.prototype.composite = function composite(change, x, y) { | ||
var base = this; | ||
var background = this; | ||
if (particles.width + x > background.width || particles.height + y > background.height) { | ||
throw new Error('placed out of bound'); | ||
} | ||
var bgData = background.data; | ||
var ptData = particles.data; | ||
var indexInBg = createIndexInBg(background, particles, x, y); | ||
for (var i = 0; i < ptData.length; i++) { | ||
var j = indexInBg(i); | ||
try { | ||
fn(bgData[j], ptData[i], result); | ||
} catch(e) { | ||
throw new Error(format('invalid data bg[%d], pt[%d]', j, i)); | ||
if (x < 0 || x + change.width > base.width || y < 0 || y + change.height > base.height) { | ||
throw new Error('out of bound'); | ||
} | ||
var indexInPt = (function (bg, pt, x, y) { | ||
return function (i) { | ||
var xRel = i % bg.width; | ||
var yRel = Math.floor(i / bg.width); | ||
var xPt = xRel - x; | ||
var yPt = yRel - y; | ||
if (xPt < 0 || xPt >= pt.width || yPt < 0 || yPt >= pt.height) { | ||
return null; | ||
} | ||
if (result.done) { | ||
return result.data; | ||
return Math.round(xPt + yPt * pt.width); | ||
}; | ||
})(base, change, Math.floor(x), Math.floor(y)); | ||
var baseData = base.data; | ||
var chngData = change.data; | ||
for (var i = 0; i < baseData.length; i ++) { | ||
var j = indexInPt(i); | ||
if (j !== null) { | ||
if (chngData[j] === 1) { | ||
baseData[i] = 1; | ||
} | ||
} | ||
result.done = true; | ||
return result.data; | ||
}; | ||
} | ||
ParticleByResize.prototype.collidedWith = createIterator(function (datumInBg, datumInPt, result) { | ||
if (datumInBg === 0 || datumInPt === 0) { | ||
result.done = false; | ||
result.data = false; | ||
} else if (datumInBg === 1 && datumInPt === 1) { | ||
result.done = true; | ||
result.data = true; | ||
} else { | ||
throw new Error(format('invalid data: %s or %s', datumInBg, datumInPt)); | ||
} | ||
}); | ||
}; | ||
ParticleByResize.prototype.merge = createIterator(function (datumInBg, datumInPt, result) { | ||
if (result.data === null) { | ||
result.data = []; | ||
} | ||
if (datumInBg === 0 && datumInPt === 0) { | ||
result.data.push(0); | ||
} else { | ||
result.data.push(1); | ||
} | ||
result.done = false; | ||
}); | ||
module.exports = ParticleByResize; |
{ | ||
"name": "particle-by-resize", | ||
"version": "0.0.1", | ||
"description": "Particlize an image by resizing", | ||
"version": "1.0.2", | ||
"description": "create image particles", | ||
"main": "index.js", | ||
@@ -10,10 +10,10 @@ "directories": { | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "browserify test/index.js > test/bundle.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/rogerz/particle-by-resize.git" | ||
"url": "git+https://github.com/rogerz/particle-by-resize.git" | ||
}, | ||
"keywords": [ | ||
"canvas", | ||
"image", | ||
"particle" | ||
@@ -26,11 +26,10 @@ ], | ||
}, | ||
"homepage": "https://github.com/rogerz/particle-by-resize", | ||
"homepage": "https://github.com/rogerz/particle-by-resize#readme", | ||
"dependencies": { | ||
"canvas": "^1.2.2", | ||
"format": "^0.2.1", | ||
"lodash": "^3.8.0" | ||
"lodash": "^3.9.3" | ||
}, | ||
"devDependencies": { | ||
"chai": "^2.3.0" | ||
"chai": "^3.0.0", | ||
"mocha": "^2.2.5" | ||
} | ||
} |
@@ -6,3 +6,2 @@ 'use strict'; | ||
var Particle = require('..'); | ||
var Canvas = require('canvas'); | ||
@@ -14,3 +13,5 @@ describe('particle', function() { | ||
before(function () { | ||
particle = new Particle(); | ||
particle = new Particle({ | ||
scale: 0.25 | ||
}); | ||
}); | ||
@@ -21,3 +22,3 @@ | ||
expect(particle.sampling).to.be.a('function'); | ||
expect(particle.merge).to.be.a('function'); | ||
expect(particle.composite).to.be.a('function'); | ||
expect(particle.collidedWith).to.be.a('function'); | ||
@@ -33,3 +34,3 @@ }); | ||
it('should sample the canvas to particle', function() { | ||
var canvas = new Canvas(); | ||
var canvas = document.createElement('canvas'); | ||
canvas.width = canvas.height = 4; | ||
@@ -49,27 +50,59 @@ | ||
describe('behavior', function () { | ||
var background, particle; | ||
before(function () { | ||
background = new Particle(); | ||
particle = new Particle(); | ||
var base, change22, change33; | ||
beforeEach(function init() { | ||
base = new Particle(); | ||
change22 = new Particle(); | ||
change33 = new Particle(); | ||
// fake data since resizing result is not easy to predict | ||
background.width = background.height = 2; | ||
background.data = [1, 0, 0, 1]; | ||
base.width = base.height = 3; | ||
base.data = [1, 0, 0, 0, 1, 0, 0, 0, 0]; | ||
particle.width = particle.height = 2; | ||
particle.data = [0, 1, 1, 0]; | ||
}); | ||
change22.width = change22.height = 2; | ||
change22.data = [0, 1, 1, 0]; | ||
it('should detect collision', function () { | ||
expect(background.collidedWith(background, 0, 0)).to.be.true; | ||
change33.width = change33.height = 3; | ||
change33.data = [0, 1, 1, 1, 0, 1, 1, 1, 1]; | ||
}); | ||
it('should detect no collision', function () { | ||
expect(background.collidedWith(particle, 0, 0)).to.be.false; | ||
describe('collision', function () { | ||
it('should detect collision', function () { | ||
expect(base.collidedWith(base, 0, 0)).to.be.true; | ||
}); | ||
it('should detect no collision', function () { | ||
expect(base.collidedWith(change22, 0, 0)).to.be.false; | ||
}); | ||
it('should detect no collision', function () { | ||
expect(base.collidedWith(change33, 0, 0)).to.be.false; | ||
}); | ||
it('should throw exception when out of bound', function () { | ||
expect(function () { | ||
base.collidedWith(change22, -1, 0); | ||
}).to.throw(Error); | ||
expect(function () { | ||
base.collidedWith(change22, 0, -1); | ||
}).to.throw(Error); | ||
expect(function () { | ||
base.collidedWith(change22, base.width, 0); | ||
}).to.throw(Error); | ||
expect(function () { | ||
base.collidedWith(change22, 0, base.height); | ||
}).to.throw(Error); | ||
}); | ||
}); | ||
it('should merge data', function () { | ||
var data = background.merge(particle, 0, 0); | ||
expect(data).to.eql([1, 1, 1, 1]); | ||
describe('composite', function () { | ||
it('should composite smaller particle', function () { | ||
base.composite(change22, 0, 0); | ||
expect(base.data).to.eql([1, 1, 0, 1, 1, 0, 0, 0, 0]); | ||
}); | ||
it('should composite same size particle', function () { | ||
base.composite(change33, 0, 0); | ||
expect(base.data).to.eql([1, 1, 1, 1, 1, 1, 1, 1, 1]); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7496
1
8
177
0
2
1
- Removedcanvas@^1.2.2
- Removedformat@^0.2.1
- Removedcanvas@1.6.13(transitive)
- Removedformat@0.2.2(transitive)
- Removednan@2.22.0(transitive)
Updatedlodash@^3.9.3