Proper usage of character2
[Dynartic.git] / DynImage.js
1 /*
2 * DynImage.js
3 * node.js module to perform the bulk of the calculations used in Dynartic
4 * main.js is a light frontend to this data structure
5 */
6
7 function DynImage(width, height, buffer) {
8 this.width = width;
9 this.height = height;
10
11 this.lesserDimension = this.width > this.height ?
12 this.height :
13 this.width;
14
15 this.buffer = buffer;
16
17 // prefill with black as opposed to nonsense from memory
18 this.fillColor(0, 0, 0, 0);
19 }
20
21 // gets or sets the color; RGBA<->XY
22
23 DynImage.prototype.coordIndex = function(x, y) {
24 return 4 * ((y * this.width) + x);
25 }
26
27 DynImage.prototype.getColor = function(x, y) {
28 x = Math.floor(x);
29 y = Math.floor(y);
30
31 if(
32 (x < 0) ||
33 (y < 0) ||
34 (x >= this.width) ||
35 (y >= this.height)
36 ) {
37 return [0,0,0,0]; // out of bounds
38 }
39
40 var index = this.coordIndex(x, y);
41
42 return [
43 this.buffer[index],
44 this.buffer[index+1],
45 this.buffer[index+2],
46 this.buffer[index+3]
47 ];
48 }
49
50 DynImage.prototype.setColor = function(x, y, r, g, b, a) {
51 if(typeof a === 'undefined') a = 0; // A is optional; opaque if unspecified
52
53 x = Math.floor(x);
54 y = Math.floor(y);
55
56 if(
57 (x < 0) ||
58 (y < 0) ||
59 (x >= this.width) ||
60 (y >= this.height)
61 ) {
62 return; // out of bounds
63 }
64
65 var index = this.coordIndex(x, y);
66
67
68 this.buffer[index+0] = Math.floor(r);
69 this.buffer[index+1] = Math.floor(g);
70 this.buffer[index+2] = Math.floor(b);
71 this.buffer[index+3] = Math.floor(a);
72 }
73
74 // blindly fills the entirety of the buffer with a given color
75 // used for initialization, etc.
76 DynImage.prototype.fillColor = function(r, g, b, a) {
77 /*for(var i = 0; i < (this.width * this.height * 4); i += 4) {
78 this.buffer[i + 0] = r;
79 this.buffer[i + 1] = g;
80 this.buffer[i + 2] = b;
81 this.buffer[i + 3] = a;
82 }*/
83 for(var x = 0; x < this.width; ++x) {
84 for(var y = 0; y < this.height; ++y) {
85 this.setColor(x, y, r, g, b, a);
86 }
87 }
88 }
89
90 // "pulls" the image up
91 // starting with a point, that point is raised a certain, specified amount (this is of course an analogy)
92 // this causes the alpha value at that point to be raised to this value,
93 // and ripples the effect the nearby pixels as well
94 DynImage.prototype.pointRing = function(sx, sy, height, layerIntensity, intensityMultiplier) {
95 var rippleAmount = (255 / this.lesserDimension) * height;
96
97 var usedPoints = {};
98
99 for(var r = 0; r < height; r += 0.1) {
100 c: for(var theta = 0; theta < (2 * Math.PI); theta += 0.01) {
101 // tracing points around many concentric circles
102
103 var x = (r * Math.cos(theta)) + sx,
104 y = (r * Math.sin(theta)) + sy;
105
106 if(usedPoints[x+";"+y]) continue c;
107
108 usedPoints[x+";"+y] = 1;
109
110 var distance = Math.sqrt( ( (sx - x) * (sx - x) ) + ( (sy - y) * (sy - y) ) );
111 var intensity = Math.log(height - distance) * intensityMultiplier; // linear function.
112 // this outputs a value 0-1, where 1 is the full effect of pulling and 0 is no effect
113 // in the future, we may want a smoother curve
114 // TODO: find alternative that works better
115
116 //var intensity = 0.5;
117
118 var currentColor = this.getColor(x, y);
119 this.setColor(x, y,
120 currentColor[0] - (intensity * layerIntensity),
121 currentColor[1] - (intensity * layerIntensity),
122 currentColor[2] - (intensity * layerIntensity)
123 );
124 }
125 }
126 }
127
128 DynImage.prototype.character2 = function(width, height) {
129 for(var x = 0; x < width; ++x) {
130 for(var y = 0; y < height; ++y) {
131 var color = this.getColor(x, y);
132 var mod = 110 + Math.floor(Math.random() * 40);
133 var mult = Math.ceil(Math.random() * 120);
134
135 this.setColor(x, y,
136 Math.floor((color[0] * 0.5) + 128*Math.log(((x / width) * mod) + ((y / height) * mod))) & 255,
137 Math.floor((color[1] * 0.5) + 128*Math.log(((x / width) * mod) + ((y / height) * mod))) & 255,
138 Math.floor((color[2] * 0.5) + 128*Math.log(((x / width) * mod) + ((y / height) * mod))) & 255
139 );
140 }
141 }
142 }
143
144 DynImage.prototype.punchImage = function(sx, sy, sw, sh, force, spread) {
145 for(var i = -(sw / 2); i < (sw/2); ++i) {
146 for(var j = (-(sh/2)); j < (sh/2); ++j) {
147 var x = sx + i,
148 y = sy + j;
149
150 var intensity = Math.sqrt( (i*i) + (j*j) );
151 intensity = Math.pow(2, -intensity / spread);
152 if( Math.sqrt( ((sx - x) * (sx - x)) + ((sy - y) * (sy - y))) < (sw/2))
153 this.punchPoint(x, y, intensity * force);
154 }
155 }
156 }
157
158 DynImage.prototype.punchWave = function(sx, sy, sw, sh, force, spread, period) {
159 for(var i = -(sw / 2); i < (sw/2); ++i) {
160 for(var j = (-(sh/2)); j < (sh/2); ++j) {
161 var x = sx + i,
162 y = sy + j;
163
164 var intensity = Math.sqrt( (i*i) + (j*j) );
165 intensity = Math.pow(2, -intensity / spread);
166
167 this.punchPoint(x, y, intensity * force * Math.sin(intensity / period));
168 }
169 }
170 }
171
172 // opposite of point pulling; works on a single point only
173
174 DynImage.prototype.punchPoint = function(x, y, force) {
175 var color = this.getColor(x, y);
176 color[0] -= force;
177 color[1] -= force;
178 color[2] -= force;
179
180 color[0] = color[0] > 0 ? color[0] < 256 ? color[0] : 255 : 0;
181 color[1] = color[1] > 0 ? color[0] < 256 ? color[1] : 255 : 0;
182 color[2] = color[2] > 0 ? color[0] < 256 ? color[2] : 255 : 0;
183
184 this.setColor(x, y, color[0], color[1], color[2], color[3]);
185 }
186
187 // contrast changer
188 // any pixel below a threshold becomes darker,
189 // and any pixel above the threshold becomes lighter
190
191 DynImage.prototype.recontrast = function(threshold, amount) {
192 for(var i = 0; i < this.width; ++i) {
193 for(var j = 0; j < this.height; ++j) {
194 var pix = this.getColor(i, j);
195
196 // TODO: use HSL for proper value testing
197
198 var avg = (pix[0] + pix[1] + pix[2]) / 3;
199 if(avg > threshold) {
200 this.setColor(i, j, pix[0] + amount, pix[1] + amount, pix[2] + amount);
201 } else {
202 this.setColor(i, j, pix[0] - amount, pix[1] - amount, pix[2] - amount);
203 }
204 }
205 }
206 }
207
208 DynImage.prototype.borderPoint = function(x, y, bred, bgreen, bblue, percent) {
209 var color = this.getColor(x, y);
210
211 // weighted average
212
213 this.setColor( ((bred * percent) + (color[0] * (100 - percent))) / 200,
214 ((bgreen * percent) + (color[1] * (100 - percent))) / 200,
215 ((bblue * percent) + (color[2] * (100 - percent))) / 200);
216 }
217
218 DynImage.prototype.border = function(width, height, bred, bgreen, bblue, mul) {
219 var borderIterations = mul;
220
221 for(var i = 0; i < borderIterations; ++i) {
222 for(var x = i; x < (width - i); ++x) {
223 // top
224
225 var y = i;
226 this.borderPoint(x, y, bred, bgreen, bblue, 100 - (i * mul));
227
228 y = height - i - 1;
229 this.borderPoint(x, y, bred, bgreen, bblue, 100 - (i * mul));
230 }
231 }
232 }
233
234
235 module.exports = DynImage;
This page took 0.111061 seconds and 4 git commands to generate.