1 | /** ---------------------------------------------------------------------------- |
---|
2 | * file : CanvasDraw.CommonClasses.js |
---|
3 | * file version : 1.0 |
---|
4 | * date : 2010-09-25 |
---|
5 | * ----------------------------------------------------------------------------- |
---|
6 | * author: grum at grum.fr |
---|
7 | * << May the Little SpaceFrog be with you >> |
---|
8 | * |
---|
9 | * This program is free software and is published under the terms of the GNU GPL |
---|
10 | * Please read CanvasDraw.ReadMe.txt file for more information |
---|
11 | * |
---|
12 | * ----------------------------------------------------------------------------- |
---|
13 | * |
---|
14 | * dependencies : none |
---|
15 | * |
---|
16 | * ----------------------------------------------------------------------------- |
---|
17 | * |
---|
18 | * provided classes : |
---|
19 | * - CDOrigin |
---|
20 | * . setX(value) |
---|
21 | * . setY(value) |
---|
22 | * . getX() |
---|
23 | * . getY() |
---|
24 | * |
---|
25 | * - CDPoint(x,y) |
---|
26 | * . x |
---|
27 | * . y |
---|
28 | * |
---|
29 | * - CDBoundBox() |
---|
30 | * . xMin |
---|
31 | * . yMin |
---|
32 | * . xMax |
---|
33 | * . yMax |
---|
34 | * . width |
---|
35 | * . height |
---|
36 | * |
---|
37 | * - CDRPoint(x,y,r) |
---|
38 | * . x |
---|
39 | * . y |
---|
40 | * . r |
---|
41 | * |
---|
42 | * - CDGradientStep(step, color) |
---|
43 | * . step |
---|
44 | * . color |
---|
45 | * |
---|
46 | * - CDTransformMatrix() |
---|
47 | * . get |
---|
48 | * . rotation(angle) |
---|
49 | * . scale(scaleX,scaleY) |
---|
50 | * . translation(x,y) |
---|
51 | * . identity |
---|
52 | * . apply |
---|
53 | * |
---|
54 | * ----------------------------------------------------------------------------- |
---|
55 | */ |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * The CDOrigin class give methods to manage origin values on abscissa an ordinate |
---|
61 | * It's used to determinate objects origin when positionning them |
---|
62 | |
---|
63 | * |
---|
64 | * |
---|
65 | * Object public properties |
---|
66 | * - x : String "left" | "middle" | "right" |
---|
67 | * origin on the abscissa |
---|
68 | * default "left" |
---|
69 | * |
---|
70 | * - y : String "top" | "middle" | "bottom" |
---|
71 | * origin on the ordinate |
---|
72 | * default "top" |
---|
73 | * |
---|
74 | * |
---|
75 | * Object public functions & methods |
---|
76 | * This object don't have any public functions & methods |
---|
77 | */ |
---|
78 | function CDOrigin () { |
---|
79 | |
---|
80 | this.properties = { |
---|
81 | x:"left", |
---|
82 | y:"top" |
---|
83 | } |
---|
84 | |
---|
85 | this.setX = function(value) |
---|
86 | { |
---|
87 | if((value=="left")||(value=="right")||(value=="middle")) this.properties.x = value; |
---|
88 | return(this.properties.x); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | this.setY = function (value) |
---|
93 | { |
---|
94 | if((value=="top")||(value=="bottom")||(value=="middle")) this.properties.y = value; |
---|
95 | return(this.properties.y); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * this CDPoint is used to define a coordinate |
---|
101 | */ |
---|
102 | function CDPoint (x,y) { |
---|
103 | this.x=x; |
---|
104 | this.y=y; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * this CDBoundBox is used to define a bound box |
---|
109 | */ |
---|
110 | function CDBoundBox () { |
---|
111 | this.properties = { |
---|
112 | min:new CDPoint(NaN,NaN), |
---|
113 | max:new CDPoint(NaN,NaN), |
---|
114 | size:new CDPoint(NaN,NaN) |
---|
115 | }; |
---|
116 | |
---|
117 | this.destroy = function () |
---|
118 | { |
---|
119 | delete this.properties.min; |
---|
120 | delete this.properties.max; |
---|
121 | delete this.properties.size; |
---|
122 | delete this.properties; |
---|
123 | } |
---|
124 | |
---|
125 | this.reset = function (value) |
---|
126 | { |
---|
127 | this.properties.min.x=NaN; |
---|
128 | this.properties.min.y=NaN; |
---|
129 | this.properties.max.x=NaN; |
---|
130 | this.properties.max.y=NaN; |
---|
131 | this.properties.size.x=NaN; |
---|
132 | this.properties.size.y=NaN; |
---|
133 | } |
---|
134 | |
---|
135 | this.copy = function (value) |
---|
136 | { |
---|
137 | if(value instanceof CDBoundBox) |
---|
138 | { |
---|
139 | this.properties.min.x=value.properties.min.x; |
---|
140 | this.properties.min.y=value.properties.min.y; |
---|
141 | this.properties.max.x=value.properties.max.x; |
---|
142 | this.properties.max.y=value.properties.max.y; |
---|
143 | this.properties.size.x=value.properties.size.x; |
---|
144 | this.properties.size.y=value.properties.size.y; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | this.applyTransformMatrix = function (value) |
---|
149 | { |
---|
150 | var tmpPt=[new CDPoint(), new CDPoint(), new CDPoint(), new CDPoint()], |
---|
151 | tmpPos=new CDPoint(); |
---|
152 | if(value instanceof CDTransformMatrix) |
---|
153 | { |
---|
154 | |
---|
155 | tmpPt[0].x=this.properties.min.x; |
---|
156 | tmpPt[0].y=this.properties.min.y; |
---|
157 | tmpPt[1].x=this.properties.max.x; |
---|
158 | tmpPt[1].y=this.properties.max.y; |
---|
159 | tmpPt[2].x=this.properties.min.x; |
---|
160 | tmpPt[2].y=this.properties.max.y; |
---|
161 | tmpPt[3].x=this.properties.max.x; |
---|
162 | tmpPt[3].y=this.properties.min.y; |
---|
163 | /* |
---|
164 | value.translation(this.properties.min.x, this.properties.min.y); |
---|
165 | tmpPos.x=this.properties.min.x; |
---|
166 | tmpPos.y=this.properties.min.y; |
---|
167 | tmpPt[0].x=0; |
---|
168 | tmpPt[0].y=0; |
---|
169 | tmpPt[1].x=this.properties.size.x; |
---|
170 | tmpPt[1].y=this.properties.size.y; |
---|
171 | tmpPt[2].x=0; |
---|
172 | tmpPt[2].y=this.properties.size.y; |
---|
173 | tmpPt[3].x=this.properties.size.x; |
---|
174 | tmpPt[3].y=0; |
---|
175 | */ |
---|
176 | |
---|
177 | this.reset(); |
---|
178 | for(var i=0;i<4;i++) |
---|
179 | { |
---|
180 | tmpPt[i]=value.apply(tmpPt[i]); |
---|
181 | this.setX(tmpPt[i].x); |
---|
182 | this.setY(tmpPt[i].y); |
---|
183 | } |
---|
184 | |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | this.isNull = function () |
---|
189 | { |
---|
190 | return( |
---|
191 | (isNaN(this.properties.size.x)) || |
---|
192 | (isNaN(this.properties.size.y)) || |
---|
193 | ((this.properties.size.x+this.properties.size.y)==0) |
---|
194 | ); |
---|
195 | } |
---|
196 | |
---|
197 | this.setX = function (value) |
---|
198 | { |
---|
199 | if(isNaN(this.properties.min.x) && isNaN(this.properties.max.x)) |
---|
200 | { |
---|
201 | this.setXMin(value); |
---|
202 | this.setXMax(value); |
---|
203 | } |
---|
204 | else if(value<this.properties.min.x) |
---|
205 | this.setXMin(value) |
---|
206 | else if(value>this.properties.max.x) |
---|
207 | this.setXMax(value); |
---|
208 | } |
---|
209 | |
---|
210 | this.setXMin = function (value) |
---|
211 | { |
---|
212 | if(value<this.properties.min.x || this.properties.min.x==this.properties.max.x || isNaN(this.properties.min.x)) |
---|
213 | { |
---|
214 | this.properties.min.x=value; |
---|
215 | if(this.properties.max.x<this.properties.min.x || isNaN(this.properties.max.x)) this.properties.max.x=this.properties.min.x; |
---|
216 | this.properties.size.x=this.properties.max.x-this.properties.min.x; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | this.setXMax = function (value) |
---|
221 | { |
---|
222 | if(value>this.properties.max.x || this.properties.min.x==this.properties.max.x || isNaN(this.properties.max.x)) |
---|
223 | { |
---|
224 | this.properties.max.x=value; |
---|
225 | if(this.properties.min.x>this.properties.max.x || isNaN(this.properties.min.x)) this.properties.min.x=this.properties.max.x; |
---|
226 | this.properties.size.x=this.properties.max.x-this.properties.min.x; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | this.setWidth = function (value) |
---|
231 | { |
---|
232 | if(value>0 && value>this.properties.size.x) |
---|
233 | { |
---|
234 | this.properties.size.x=value; |
---|
235 | this.properties.max.x=this.properties.min.x+value; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | this.setY = function (value) |
---|
240 | { |
---|
241 | if(isNaN(this.properties.min.y) && isNaN(this.properties.max.y)) |
---|
242 | { |
---|
243 | this.setYMin(value); |
---|
244 | this.setYMax(value); |
---|
245 | } |
---|
246 | else if(value<this.properties.min.y) |
---|
247 | this.setYMin(value) |
---|
248 | else if(value>this.properties.max.y) |
---|
249 | this.setYMax(value); |
---|
250 | } |
---|
251 | |
---|
252 | this.setYMin = function (value) |
---|
253 | { |
---|
254 | if(value<this.properties.min.y || this.properties.min.y==this.properties.max.y || isNaN(this.properties.min.y)) |
---|
255 | { |
---|
256 | this.properties.min.y=value; |
---|
257 | if(this.properties.max.y<this.properties.min.y || isNaN(this.properties.max.y)) this.properties.max.y=this.properties.min.y; |
---|
258 | this.properties.size.y=this.properties.max.y-this.properties.min.y; |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | this.setYMax = function (value) |
---|
263 | { |
---|
264 | if(value>this.properties.max.y || this.properties.min.y==this.properties.max.y || isNaN(this.properties.max.y)) |
---|
265 | { |
---|
266 | this.properties.max.y=value; |
---|
267 | if(this.properties.min.y>this.properties.max.y || isNaN(this.properties.min.y)) this.properties.min.y=this.properties.max.y; |
---|
268 | this.properties.size.y=this.properties.max.y-this.properties.min.y; |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | this.setHeight = function (value) |
---|
273 | { |
---|
274 | if(value>0 && value>this.properties.size.y) |
---|
275 | { |
---|
276 | this.properties.size.y=value; |
---|
277 | this.properties.max.y=this.properties.min.y+value; |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | /** |
---|
284 | * this CDRPoint is used to define a radial coordinate (for radial gradient) |
---|
285 | */ |
---|
286 | function CDRPoint (x,y,r) { |
---|
287 | this.x=x; |
---|
288 | this.y=y; |
---|
289 | this.r=r; |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * this CDGradientStep is used to defined a gradient step |
---|
294 | */ |
---|
295 | function CDGradientStep (step,color) { |
---|
296 | this.step=step; |
---|
297 | this.color=color; |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | /** |
---|
304 | * returns an object {r:RR, g:GG, b:BB} from a color string #RRGGBB |
---|
305 | * |
---|
306 | * if the given color is not a color, return r:0, g:0, b:0 |
---|
307 | * |
---|
308 | * @param String value : color value |
---|
309 | */ |
---|
310 | function colorHexToRGBInt(value) { |
---|
311 | returned={r:0, g:0, b:0} |
---|
312 | |
---|
313 | re=/#([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])/i; |
---|
314 | rgb=re.exec(value); |
---|
315 | if(rgb!=null) |
---|
316 | { |
---|
317 | returned.r=parseInt('0x'+rgb[1]); |
---|
318 | returned.g=parseInt('0x'+rgb[2]); |
---|
319 | returned.b=parseInt('0x'+rgb[3]); |
---|
320 | } |
---|
321 | return(returned); |
---|
322 | } |
---|
323 | |
---|
324 | |
---|
325 | /** |
---|
326 | * returns a rgba() color |
---|
327 | * |
---|
328 | * @param String color : a #rrggbb color value |
---|
329 | * @param Float opacity : the opacity |
---|
330 | * @return String : |
---|
331 | */ |
---|
332 | function toRGBA(color, opacity) { |
---|
333 | rgb=colorHexToRGBInt(color); |
---|
334 | return('rgba('+rgb.r+', '+rgb.g+', '+rgb.b+', '+opacity+')'); |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | |
---|
339 | /** |
---|
340 | * returns true if given color is a valid color string #rrggbb |
---|
341 | * |
---|
342 | * @param String value : color value |
---|
343 | */ |
---|
344 | function isValidColor(value) { |
---|
345 | re=/#([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])/i; |
---|
346 | if(re.exec(value)!=null) return(true); |
---|
347 | return(false); |
---|
348 | } |
---|
349 | |
---|
350 | /** |
---|
351 | * returns true if given opacity is valid |
---|
352 | * |
---|
353 | * @param Float value : color value |
---|
354 | */ |
---|
355 | function isValidOpacity(value) { |
---|
356 | if((value>=0)&&(value<=1)) return(true); |
---|
357 | return(false); |
---|
358 | } |
---|
359 | |
---|
360 | |
---|
361 | /** |
---|
362 | * matrix usage |
---|
363 | */ |
---|
364 | function CDTransformMatrix() |
---|
365 | { |
---|
366 | this.matrix=[]; |
---|
367 | |
---|
368 | this.identity = function () |
---|
369 | { |
---|
370 | // matrix[y][x] |
---|
371 | this.matrix= [ |
---|
372 | [1,0,0], |
---|
373 | [0,1,0] |
---|
374 | ]; |
---|
375 | }; |
---|
376 | |
---|
377 | this.get = function () |
---|
378 | { |
---|
379 | return(this.matrix); |
---|
380 | } |
---|
381 | |
---|
382 | this.rotation = function (angle) |
---|
383 | { |
---|
384 | var angleRad=angle*Math.PI/180, |
---|
385 | sinus=Math.sin(angleRad), |
---|
386 | cosinus=Math.cos(angleRad), |
---|
387 | returned=[[0,0],[0,0]]; |
---|
388 | |
---|
389 | returned[0][0]=this.matrix[0][0]*cosinus+this.matrix[1][0]*sinus; |
---|
390 | returned[1][0]=-this.matrix[0][0]*sinus+this.matrix[1][0]*cosinus; |
---|
391 | returned[0][1]=this.matrix[0][1]*cosinus+this.matrix[1][1]*sinus; |
---|
392 | returned[1][1]=-this.matrix[0][1]*sinus+this.matrix[1][1]*cosinus; |
---|
393 | |
---|
394 | this.matrix[0][0]=returned[0][0]; |
---|
395 | this.matrix[0][1]=returned[0][1]; |
---|
396 | this.matrix[1][0]=returned[1][0]; |
---|
397 | this.matrix[1][1]=returned[1][1]; |
---|
398 | } |
---|
399 | |
---|
400 | this.scale = function (scaleX, scaleY) |
---|
401 | { |
---|
402 | this.matrix[0][0]*=scaleX; |
---|
403 | this.matrix[1][1]*=scaleY; |
---|
404 | |
---|
405 | this.matrix[0][2]*=scaleX; |
---|
406 | this.matrix[1][2]*=scaleY; |
---|
407 | } |
---|
408 | |
---|
409 | this.translation = function (x, y) |
---|
410 | { |
---|
411 | this.matrix[0][2]+=x; |
---|
412 | this.matrix[1][2]+=y; |
---|
413 | } |
---|
414 | |
---|
415 | this.apply = function (point) |
---|
416 | { |
---|
417 | var returned=new CDPoint(); |
---|
418 | |
---|
419 | if(point instanceof CDPoint) |
---|
420 | { |
---|
421 | returned.x=(point.x * this.matrix[0][0] + point.y * this.matrix[0][1])+this.matrix[0][2]; |
---|
422 | returned.y=(point.x * this.matrix[1][0] + point.y * this.matrix[1][1])+this.matrix[1][2]; |
---|
423 | } |
---|
424 | |
---|
425 | return(returned); |
---|
426 | } |
---|
427 | |
---|
428 | this.identity(); |
---|
429 | } |
---|