More pretty things
[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) {
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 = (height - distance) / height; // 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.punchImage = function(sx, sy, sw, sh, force, spread) {
129 for(var i = -(sw / 2); i < (sw/2); ++i) {
130 for(var j = (-(sh/2)); j < (sh/2); ++j) {
131 var x = sx + i,
132 y = sy + j;
133
134 var intensity = Math.sqrt( (i*i) + (j*j) );
135 intensity = Math.pow(2, -intensity / spread);
136 this.punchPoint(x, y, intensity * force);
137 }
138 }
139 }
140
141 // opposite of point pulling; works on a single point only
142
143 DynImage.prototype.punchPoint = function(x, y, force) {
144 var color = this.getColor(x, y);
145 color[0] -= force;
146 color[1] -= force;
147 color[2] -= force;
148
149 color[0] = color[0] > 0 ? color[0] < 256 ? color[0] : 255 : 0;
150 color[1] = color[1] > 0 ? color[0] < 256 ? color[1] : 255 : 0;
151 color[2] = color[2] > 0 ? color[0] < 256 ? color[2] : 255 : 0;
152
153 this.setColor(x, y, color[0], color[1], color[2], color[3]);
154 }
155
156 module.exports = DynImage;
This page took 0.100289 seconds and 4 git commands to generate.