Total Complexity | 71 |
Complexity/F | 7.89 |
Lines of Code | 274 |
Function Count | 9 |
Duplicated Lines | 253 |
Ratio | 92.34 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like public/demo/intro/codef_scrolltext.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*------------------------------------------------------------------------------ |
||
28 | function ltrobj(posx,posy,ltr){ |
||
29 | this.posx=posx; |
||
30 | this.posy=posy; |
||
31 | this.ltr=ltr; |
||
32 | return this; |
||
33 | } |
||
34 | |||
35 | function sortPosx(a, b) { |
||
36 | var x = a.posx; |
||
37 | var y = b.posx; |
||
38 | return ((x < y) ? -1 : ((x > y) ? 1 : 0)); |
||
39 | } |
||
40 | |||
41 | function sortPosy(a, b) { |
||
42 | var x = a.posy; |
||
43 | var y = b.posy; |
||
44 | return ((x < y) ? -1 : ((x > y) ? 1 : 0)); |
||
45 | } |
||
46 | |||
47 | View Code Duplication | function scrolltext_horizontal(){ |
|
|
|||
48 | this.scroffset=0; |
||
49 | this.oldspeed=0; |
||
50 | this.speed=1; |
||
51 | this.font; |
||
52 | this.letters = new Object(); |
||
53 | this.scrtxt=" "; |
||
54 | this.pausetimer=0; |
||
55 | this.pausedelay=0; |
||
56 | |||
57 | this.init = function(dst, font,speed,sinparam,type){ |
||
58 | this.speed=speed; |
||
59 | this.dst=dst; |
||
60 | this.font=font; |
||
61 | this.fontw = this.font.tilew; |
||
62 | this.fonth = this.font.tileh; |
||
63 | this.fontstart = this.font.tilestart; |
||
64 | this.wide=Math.ceil(this.dst.canvas.width/this.fontw)+1; |
||
65 | for(i=0;i<=this.wide;i++){ |
||
66 | this.letters[i]=new ltrobj(Math.ceil((this.wide*this.fontw)+i*this.fontw),0,this.scrtxt.charCodeAt(this.scroffset)); |
||
67 | this.scroffset++; |
||
68 | } |
||
69 | if(typeof(sinparam)!='undefined') |
||
70 | this.sinparam=sinparam; |
||
71 | if(typeof(type)=='undefined') |
||
72 | this.type=0; |
||
73 | else |
||
74 | this.type=type; |
||
75 | } |
||
76 | |||
77 | this.draw = function(posy){ |
||
78 | var prov = 0; |
||
79 | var temp = new Array(); |
||
80 | var tmp=this.dst.contex.globalAlpha; |
||
81 | this.dst.contex.globalAlpha=1; |
||
82 | var oldvalue=new Array(); |
||
83 | var i; |
||
84 | if(typeof(this.sinparam)!='undefined'){ |
||
85 | for(var j=0;j<this.sinparam.length;j++){ |
||
86 | oldvalue[j]=this.sinparam[j].myvalue; |
||
87 | } |
||
88 | } |
||
89 | if(this.speed==0){ |
||
90 | this.pausetimer+=1; |
||
91 | if(this.pausetimer==60*this.pausedelay){ |
||
92 | this.speed=this.oldspeed; |
||
93 | } |
||
94 | } |
||
95 | var speed=this.speed; |
||
96 | for(i=0;i<=this.wide;i++){ |
||
97 | this.letters[i].posx-=speed; |
||
98 | if(this.letters[i].posx<=-this.fontw){ |
||
99 | if(this.scrtxt.charAt(this.scroffset) =="^"){ |
||
100 | if(this.scrtxt.charAt(this.scroffset+1) =="P"){ |
||
101 | this.pausedelay=this.scrtxt.charAt(this.scroffset+2); |
||
102 | this.pausetimer=0; |
||
103 | this.oldspeed=this.speed; |
||
104 | this.speed=0; |
||
105 | this.scroffset+=3; |
||
106 | } |
||
107 | else if(this.scrtxt.charAt(this.scroffset+1) =="S"){ |
||
108 | this.speed=this.scrtxt.charAt(this.scroffset+2); |
||
109 | this.scroffset+=3; |
||
110 | } |
||
111 | // |
||
112 | // ADDON by Robert Annett |
||
113 | // |
||
114 | else if(this.scrtxt.charAt(this.scroffset+1) =="C"){ |
||
115 | var end = this.scrtxt.indexOf(';', this.scroffset+2); |
||
116 | var functionName = this.scrtxt.substring(this.scroffset+2, end); |
||
117 | window[functionName](); |
||
118 | this.scroffset+=(end-this.scroffset)+1; |
||
119 | } |
||
120 | }else{ |
||
121 | this.letters[i].posx=this.wide*this.fontw+(this.letters[i].posx+this.fontw); |
||
122 | if(typeof(this.sinparam)!='undefined'){ |
||
123 | for(var j=0;j<this.sinparam.length;j++){ |
||
124 | oldvalue[j]+=this.sinparam[j].inc; |
||
125 | } |
||
126 | } |
||
127 | this.letters[i].ltr=this.scrtxt.charCodeAt(this.scroffset); |
||
128 | this.scroffset++; |
||
129 | if(this.scroffset> this.scrtxt.length-1) |
||
130 | this.scroffset=0; |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | if(typeof(this.sinparam)!='undefined'){ |
||
135 | for(var j=0;j<this.sinparam.length;j++){ |
||
136 | this.sinparam[j].myvalue=oldvalue[j]; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | for(j=0;j<=this.wide;j++){ |
||
141 | temp[j]={indice:j, posx:this.letters[j].posx}; |
||
142 | } |
||
143 | temp.sort(sortPosx); |
||
144 | for(i=0;i<=this.wide;i++){ |
||
145 | if(typeof(this.sinparam)!='undefined'){ |
||
146 | prov = 0; |
||
147 | for(var j=0;j<this.sinparam.length;j++){ |
||
148 | if(this.type==0) |
||
149 | prov += Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp; |
||
150 | if(this.type==1) |
||
151 | prov += -Math.abs(Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp); |
||
152 | if(this.type==2) |
||
153 | prov += Math.abs(Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp); |
||
154 | |||
155 | } |
||
156 | } |
||
157 | this.font.drawTile(this.dst,this.letters[temp[i].indice].ltr-this.fontstart,this.letters[temp[i].indice].posx,prov+posy); |
||
158 | |||
159 | if(typeof(this.sinparam)!='undefined'){ |
||
160 | for(var j=0;j<this.sinparam.length;j++){ |
||
161 | this.sinparam[j].myvalue+=this.sinparam[j].inc; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | if(typeof(this.sinparam)!='undefined'){ |
||
166 | for(var j=0;j<this.sinparam.length;j++){ |
||
167 | this.sinparam[j].myvalue=oldvalue[j]+this.sinparam[j].offset; |
||
168 | } |
||
169 | } |
||
170 | this.dst.contex.globalAlpha=tmp; |
||
171 | } |
||
172 | return this; |
||
173 | } |
||
174 | |||
175 | |||
176 | View Code Duplication | function scrolltext_vertical(){ |
|
177 | this.scroffset=0; |
||
178 | this.oldspeed=0; |
||
179 | this.speed=1; |
||
180 | this.font; |
||
181 | this.letters = new Object(); |
||
182 | this.scrtxt=" "; |
||
183 | this.pausetimer=0; |
||
184 | this.pausedelay=0; |
||
185 | |||
186 | this.init = function(dst, font,speed,sinparam,type){ |
||
187 | this.speed=speed; |
||
188 | this.dst=dst; |
||
189 | this.font=font; |
||
190 | this.fontw = this.font.tilew; |
||
191 | this.fonth = this.font.tileh; |
||
192 | this.fontstart = this.font.tilestart; |
||
193 | this.wide=Math.ceil(this.dst.canvas.height/this.fonth)+1; |
||
194 | for(i=0;i<=this.wide;i++){ |
||
195 | this.letters[i]=new ltrobj(0,Math.ceil((this.wide*this.fonth)+i*this.fonth),this.scrtxt.charCodeAt(this.scroffset)); |
||
196 | this.scroffset++; |
||
197 | } |
||
198 | if(typeof(sinparam)!='undefined') |
||
199 | this.sinparam=sinparam; |
||
200 | if(typeof(type)=='undefined') |
||
201 | this.type=0; |
||
202 | else |
||
203 | this.type=type; |
||
204 | } |
||
205 | |||
206 | this.draw = function(posx){ |
||
207 | var prov = 0; |
||
208 | var temp = new Array(); |
||
209 | var tmp=this.dst.contex.globalAlpha; |
||
210 | this.dst.contex.globalAlpha=1; |
||
211 | var oldvalue=new Array(); |
||
212 | var i; |
||
213 | if(typeof(this.sinparam)!='undefined'){ |
||
214 | for(var j=0;j<this.sinparam.length;j++){ |
||
215 | oldvalue[j]=this.sinparam[j].myvalue; |
||
216 | } |
||
217 | } |
||
218 | if(this.speed==0){ |
||
219 | this.pausetimer+=1; |
||
220 | if(this.pausetimer==60*this.pausedelay){ |
||
221 | this.speed=this.oldspeed; |
||
222 | } |
||
223 | } |
||
224 | var speed=this.speed; |
||
225 | for(i=0;i<=this.wide;i++){ |
||
226 | this.letters[i].posy-=speed; |
||
227 | if(this.letters[i].posy<=-this.fonth){ |
||
228 | if(this.scrtxt.charAt(this.scroffset) =="^"){ |
||
229 | if(this.scrtxt.charAt(this.scroffset+1) =="P"){ |
||
230 | this.pausedelay=this.scrtxt.charAt(this.scroffset+2); |
||
231 | this.pausetimer=0; |
||
232 | this.oldspeed=this.speed; |
||
233 | this.speed=0; |
||
234 | this.scroffset+=3; |
||
235 | } |
||
236 | else if(this.scrtxt.charAt(this.scroffset+1) =="S"){ |
||
237 | this.speed=this.scrtxt.charAt(this.scroffset+2); |
||
238 | this.scroffset+=3; |
||
239 | } |
||
240 | // |
||
241 | // ADDON by Robert Annett |
||
242 | // |
||
243 | else if(this.scrtxt.charAt(this.scroffset+1) =="C"){ |
||
244 | var end = this.scrtxt.indexOf(';', this.scroffset+2); |
||
245 | var functionName = this.scrtxt.substring(this.scroffset+2, end); |
||
246 | window[functionName](); |
||
247 | this.scroffset+=(end-this.scroffset)+1; |
||
248 | } |
||
249 | }else{ |
||
250 | this.letters[i].posy=this.wide*this.fonth+(this.letters[i].posy+this.fonth); |
||
251 | if(typeof(this.sinparam)!='undefined'){ |
||
252 | for(var j=0;j<this.sinparam.length;j++){ |
||
253 | oldvalue[j]+=this.sinparam[j].inc; |
||
254 | } |
||
255 | } |
||
256 | this.letters[i].ltr=this.scrtxt.charCodeAt(this.scroffset); |
||
257 | this.scroffset++; |
||
258 | if(this.scroffset> this.scrtxt.length-1) |
||
259 | this.scroffset=0; |
||
260 | } |
||
261 | } |
||
262 | } |
||
263 | if(typeof(this.sinparam)!='undefined'){ |
||
264 | for(var j=0;j<this.sinparam.length;j++){ |
||
265 | this.sinparam[j].myvalue=oldvalue[j]; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | for(j=0;j<=this.wide;j++){ |
||
270 | temp[j]={indice:j, posy:this.letters[j].posy}; |
||
271 | } |
||
272 | temp.sort(sortPosy); |
||
273 | for(i=0;i<=this.wide;i++){ |
||
274 | if(typeof(this.sinparam)!='undefined'){ |
||
275 | prov = 0; |
||
276 | for(var j=0;j<this.sinparam.length;j++){ |
||
277 | if(this.type==0) |
||
278 | prov += Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp; |
||
279 | if(this.type==1) |
||
280 | prov += -Math.abs(Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp); |
||
281 | if(this.type==2) |
||
282 | prov += Math.abs(Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp); |
||
283 | } |
||
284 | } |
||
285 | this.font.drawTile(this.dst,this.letters[temp[i].indice].ltr-this.fontstart,prov+posx,this.letters[temp[i].indice].posy); |
||
286 | |||
287 | if(typeof(this.sinparam)!='undefined'){ |
||
288 | for(var j=0;j<this.sinparam.length;j++){ |
||
289 | this.sinparam[j].myvalue+=this.sinparam[j].inc; |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | if(typeof(this.sinparam)!='undefined'){ |
||
294 | for(var j=0;j<this.sinparam.length;j++){ |
||
295 | this.sinparam[j].myvalue=oldvalue[j]+this.sinparam[j].offset; |
||
296 | } |
||
297 | } |
||
298 | this.dst.contex.globalAlpha=tmp; |
||
299 | } |
||
300 | return this; |
||
301 | } |
||
302 | |||
303 |