Misc changes; punch point
[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 // opposite of point pulling; works on a single point only
129
130 DynImage.prototype.punchPoint = function(x, y, force) {
131 var color = this.getColor(x, y);
132 color[0] -= force;
133 color[1] -= force;
134 color[2] -= force;
135
136 color[0] = color[0] > 0 ? color[0] < 256 ? color[0] : 255 : 0;
137 color[1] = color[1] > 0 ? color[0] < 256 ? color[1] : 255 : 0;
138 color[2] = color[2] > 0 ? color[0] < 256 ? color[2] : 255 : 0;
139
140 this.setColor(x, y, color[0], color[1], color[2], color[3]);
141 }
142
143 module.exports = DynImage;
This page took 0.095496 seconds and 5 git commands to generate.