2 * Copyright (c) 2013, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
9 var cancelShadowFlickerInterval = undefined;
11 TextObject = function(ctx, args)
14 this.name = args.name;
16 this.text = args.text ? args.text : "";
17 this.xLoc = args.xLoc;
18 this.yLoc = args.yLoc;
19 this.zLoc = args.zLoc == undefined ? 0 : args.zLoc;
20 this.width = args.width;
21 this.height = args.lineHeight;
22 this.lineHeight = args.lineHeight;
23 this.wordWrap = args.wordWrap;
24 this.largeShadow = args.largeShadow;
26 this.visible = args.visible == undefined ? true : args.visible;
27 this.onClick = undefined;
30 this.font = args.font;
31 this.fillStyle = args.fillStyle;
32 this.lineWidth = args.lineWidth;
33 this.strokeStyle = args.strokeStyle;
34 this.textAlign = args.textAlign;
35 this.textBaseline = args.textBaseline;
36 this.shadowOffsetX = args.shadowOffsetX;
37 this.shadowOffsetY = args.shadowOffsetY;
38 this.shadowBlur = args.shadowBlur;
39 this.shadowColor = args.shadowColor;
42 this.applyTemplate(args.template);
46 var words = this.text.split(" ");
48 var tmpYloc = this.yLoc;
50 for(var i = 0; i < words.length; i++)
52 var testLine = line + words[i] + " ";
53 var metrics = this.ctx.measureText(testLine);
54 var testWidth = metrics.width;
56 if(testWidth > this.width)
58 line = words[i] + " ";
59 tmpYloc += this.lineHeight;
60 this.height += this.lineHeight;
70 TextObject.prototype.update = function(args)
73 if (args.name) {this.name = args.name;}
74 if (args.text) {this.text = args.text;}
75 if (args.xLoc) {this.xLoc = args.xLoc;}
76 if (args.yLoc) {this.yLoc = args.yLoc;}
77 if (args.zLoc) {this.zLoc = args.zLoc;}
78 if (args.width) {this.width = args.width;}
79 if (args.lineHeight) {this.height = args.lineHeight;}
80 if (args.lineHeight) {this.lineHeight = args.lineHeight;}
81 if (args.wordWrap) {this.wordWrap = args.wordWrap;}
82 if (args.largeShadow) {this.largeShadow = args.largeShadow;}
83 if (args.ctx) {this.ctx = ctx;}
84 if (args.visible) {this.visible = args.visible;}
85 if (args.onClick) {this.onClick = args.onClick;}
88 if (args.font) {this.font = args.font;}
89 if (args.fillStyle) {this.fillStyle = args.fillStyle;}
90 if (args.lineWidth) {this.lineWidth = args.lineWidth;}
91 if (args.strokeStyle) {this.strokeStyle = args.strokeStyle;}
92 if (args.textAlign) {this.textAlign = args.textAlign;}
93 if (args.textBaseline) {this.textBaseline = args.textBaseline;}
94 if (args.shadowOffsetX) {this.shadowOffsetX = args.shadowOffsetX;}
95 if (args.shadowOffsetY) {this.shadowOffsetY = args.shadowOffsetY;}
96 if (args.shadowBlur) {this.shadowBlur = args.shadowBlur;}
97 if (args.shadowColor) {this.shadowColor = args.shadowColor;}
100 this.applyTemplate(args.template);
103 TextObject.prototype.drawObj = function()
109 this.ctx.font = this.font;
110 this.ctx.fillStyle = this.fillStyle;
111 this.ctx.lineWidth = this.lineWidth ;
112 this.ctx.strokeStyle = this.strokeStyle;
113 this.ctx.textAlign = this.textAlign;
114 this.ctx.textBaseline = this.textBaseline;
115 this.ctx.shadowOffsetX = this.shadowOffsetX;
116 this.ctx.shadowOffsetY = this.shadowOffsetY;
117 this.ctx.shadowBlur = this.shadowBlur;
118 this.ctx.shadowColor = this.shadowColor;
125 if (this.strokeStyle)
126 this.ctx.strokeText(this.text, this.xLoc, this.yLoc);
129 this.ctx.fillText(this.text, this.xLoc, this.yLoc);
138 TextObject.prototype.applyTemplate = function(template)
140 this.font = template.font;
141 this.fillStyle = template.fillStyle;
142 this.lineWidth = template.lineWidth;
143 this.strokeStyle = template.strokeStyle;
144 this.textAlign = template.textAlign;
145 this.textBaseline = template.textBaseline;
146 this.shadowOffsetX = template.shadowOffsetX;
147 this.shadowOffsetY = template.shadowOffsetY;
148 this.shadowBlur = template.shadowBlur;
149 this.shadowColor = template.shadowColor;
153 var words = this.text.split(" ");
155 var tmpYloc = this.yLoc;
157 for(var i = 0; i < words.length; i++)
159 var testLine = line + words[i] + " ";
160 var metrics = this.ctx.measureText(testLine);
161 var testWidth = metrics.width;
163 if(testWidth > this.width)
165 line = words[i] + " ";
166 tmpYloc += this.lineHeight;
167 this.height += this.lineHeight;
177 TextObject.prototype.drawLargeShadow = function()
179 var origX = this.shadowOffsetX;
180 var origY = this.shadowOffsetY;
182 this.shadowOffsetX = this.largeShadow;
183 this.shadowOffsetY = 0;
185 this.shadowOffsetY = this.largeShadow;
187 this.shadowOffsetX = 0;
189 this.shadowOffsetX = -this.largeShadow;
191 this.shadowOffsetY = 0;
193 this.shadowOffsetY = -this.largeShadow;
195 this.shadowOffsetX = -0;
197 this.shadowOffsetX = -this.largeShadow;
200 this.shadowOffsetX = origX;
201 this.shadowOffsetY = origY;
204 TextObject.prototype.drawNoShadow = function()
208 this.ctx.shadowOffsetX = 0;
209 this.ctx.shadowOffsetY = 0;
210 this.ctx.shadowBlur = 0;
212 if (this.strokeStyle)
213 this.ctx.strokeText(this.text, this.xLoc, this.yLoc);
216 this.ctx.fillText(this.text, this.xLoc, this.yLoc);
222 TextObject.prototype.wrapText = function()
226 this.ctx.font = this.font;
227 this.ctx.fillStyle = this.fillStyle;
228 this.ctx.lineWidth = this.lineWidth ;
229 this.ctx.strokeStyle = this.strokeStyle;
230 this.ctx.textAlign = this.textAlign;
231 this.ctx.textBaseline = this.textBaseline;
232 this.ctx.shadowOffsetX = this.shadowOffsetX;
233 this.ctx.shadowOffsetY = this.shadowOffsetY;
234 this.ctx.shadowBlur = this.shadowBlur;
235 this.ctx.shadowColor = this.shadowColor;
236 this.height = this.lineHeight;
238 var words = this.text.split(" ");
240 var tmpYloc = this.yLoc;
243 for(var i = 0; i < words.length; i++)
245 var testLine = line + words[i] + " ";
246 var metrics = this.ctx.measureText(testLine);
247 var testWidth = metrics.width;
249 if(testWidth > this.width)
251 if (this.strokeStyle)
252 this.ctx.strokeText(line, this.xLoc, tmpYloc);
255 this.ctx.fillText(line, this.xLoc, tmpYloc);
257 line = words[i] + " ";
258 tmpYloc += this.lineHeight;
259 this.height += this.lineHeight;
271 if (this.strokeStyle)
272 this.ctx.strokeText(line, this.xLoc, tmpYloc);
275 this.ctx.fillText(line, this.xLoc, tmpYloc);
280 function shadowFlicker(obj, min, max, interval)
284 if (cancelShadowFlickerInterval)
285 clearInterval(cancelShadowFlickerInterval);
287 cancelShadowFlickerInterval = setInterval(
291 if (tmpObj.largeShadow)
292 var shadowOffset = tmpObj.largeShadow;
294 var shadowOffset = tmpObj.shadowOffsetX;
296 if (shadowOffset > max)
297 shadowOffset -= Math.floor(Math.random()*interval) + 1;
298 else if (shadowOffset <= min)
299 shadowOffset += Math.floor(Math.random()*interval) + 1;
302 if (Math.floor(Math.random()*1))
303 shadowOffset += Math.floor(Math.random()*interval) + 1;
305 shadowOffset -= Math.floor(Math.random()*interval) + 1;
308 if (shadowOffset < 1)
311 tmpObj.ctx.clearRect(0,0,500,500);
314 if (!tmpObj.largeShadow)
316 tmpObj.shadowOffsetX = shadowOffset;
317 tmpObj.shadowOffsetY = shadowOffset;
319 tmpObj.largeShadow = shadowOffset;
320 tmpObj.drawLargeShadow();
324 tmpObj.largeShadow = shadowOffset;
325 tmpObj.drawLargeShadow();
331 function shadowGlow(obj, min, max, speed)
335 if (cancelShadowFlickerInterval)
336 clearInterval(cancelShadowFlickerInterval);
338 var direction = "OUT";
339 cancelShadowFlickerInterval = setInterval(
344 if (tmpObj.largeShadow)
345 var shadowOffset = tmpObj.largeShadow;
347 var shadowOffset = tmpObj.shadowOffsetX;
349 if (shadowOffset > max)
351 else if (shadowOffset <= min)
354 if (direction === "OUT")
359 if (shadowOffset < 1)
362 tmpObj.ctx.clearRect(0,0,500,500);
365 if (!tmpObj.largeShadow)
367 tmpObj.shadowOffsetX = shadowOffset;
368 tmpObj.shadowOffsetY = shadowOffset;
370 tmpObj.largeShadow = shadowOffset;
371 tmpObj.drawLargeShadow();
375 tmpObj.largeShadow = shadowOffset;
376 tmpObj.drawLargeShadow();