/
index.js
106 lines (90 loc) · 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
var _ = require('lodash');
function ParticleByResize(options) {
this.options = _.assign({
scale: 1
}, options);
this.data = [];
this.width = this.height = 0;
}
ParticleByResize.prototype.sampling = function (imageSource) {
var scale = this.options.scale;
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(imageSource, 0, 0, cw, ch);
var data = c.getImageData(0, 0, cw, ch).data;
this.data = [];
function occupied(pixelRgba) {
// imageData = [r,g,b,a]
if (pixelRgba[3] > 0) {
return 1;
} else {
return 0;
}
}
for (var i = 0; i < data.length; i = i + 4) {
this.data.push(occupied(Array.prototype.slice.call(data, i, i+4)));
}
this.width = cw;
this.height = ch;
return this;
};
ParticleByResize.prototype.collidedWith = function collidedWith(change, x, y) {
var base = this;
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 >= bg.width || xBg < 0 || yBg >= bg.height || yBg < 0) {
throw new Error('out of bound');
}
return Math.round(xBg + yBg * bg.width);
};
})(base, change, Math.floor(x), Math.floor(y));
var baseData = base.data;
var chngData = change.data;
for (var i = 0; i < chngData.length; i++) {
if (chngData[i] === 1) {
var j = indexInBg(i);
if (baseData[j] === 1) {
return true;
}
}
}
return false;
};
ParticleByResize.prototype.composite = function composite(change, x, y) {
var base = this;
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;
}
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;
}
}
}
};
module.exports = ParticleByResize;