| Conditions | 38 |
| Total Lines | 363 |
| Code Lines | 148 |
| Lines | 71 |
| Ratio | 19.56 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like codef_core.js ➔ canvas 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 | /*------------------------------------------------------------------------------ |
||
| 60 | function canvas(w, h, divname){ |
||
| 61 | this.width=w; |
||
| 62 | this.height=h; |
||
| 63 | this.canvas; |
||
| 64 | this.contex; |
||
| 65 | this.canvas = document.createElement("canvas"); |
||
| 66 | if(divname) document.getElementById(divname).appendChild(this.canvas); |
||
| 67 | this.canvas.setAttribute('width', w); |
||
| 68 | this.canvas.setAttribute('height', h); |
||
| 69 | this.contex = this.canvas.getContext('2d'); |
||
| 70 | |||
| 71 | this.handlex=0; |
||
| 72 | this.handley=0; |
||
| 73 | this.midhandled=false; |
||
| 74 | this.tilew=0; |
||
| 75 | this.tileh=0; |
||
| 76 | this.tilestart=0; |
||
| 77 | |||
| 78 | /** |
||
| 79 | <b>Fill the canvas.</b><br> |
||
| 80 | canvas.fill(color)<br> |
||
| 81 | |||
| 82 | @function canvas.fill |
||
| 83 | @param {Color} color The color you want to fill the canvas with. |
||
| 84 | @example |
||
| 85 | mycanvas.fill('#FF0000'); |
||
| 86 | */ |
||
| 87 | this.fill = function(color){ |
||
| 88 | var tmp = this.contex.fillStyle; |
||
| 89 | var tmp2= this.contex.globalAlpha; |
||
| 90 | this.contex.globalAlpha=1; |
||
| 91 | this.contex.fillStyle = color; |
||
| 92 | this.contex.fillRect (0, 0, this.canvas.width, this.canvas.height); |
||
| 93 | this.contex.fillStyle = tmp |
||
| 94 | this.contex.globalAlpha=tmp2; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | <b>Clear the canvas.</b><br> |
||
| 99 | |||
| 100 | @function canvas.clear |
||
| 101 | @example |
||
| 102 | mycanvas.clear(); |
||
| 103 | */ |
||
| 104 | this.clear = function(){ |
||
| 105 | this.contex.clearRect (0, 0, this.canvas.width, this.canvas.height); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | <b>Draw a plot on the canvas.</b><br> |
||
| 110 | canvas.plot(x1,y1,width,color)<br> |
||
| 111 | |||
| 112 | @function canvas.plot |
||
| 113 | @param {Number in pixel} x The x coord you want to plot in the canvas. |
||
| 114 | @param {Number in pixel} y The y coord you want to plot in the canvas. |
||
| 115 | @param {Number in pixel} width The size of the plot. |
||
| 116 | @param {Color} color The color of the plot. |
||
| 117 | @example |
||
| 118 | mycanvas.plot(20,20,50,'#FF0000'); |
||
| 119 | */ |
||
| 120 | this.plot = function(x,y,width,color){ |
||
| 121 | // save old fillstyle |
||
| 122 | var oldcolor = this.contex.fillStyle ; |
||
| 123 | |||
| 124 | this.contex.fillStyle=color; |
||
| 125 | this.contex.fillRect(x,y,width,width) ; |
||
| 126 | |||
| 127 | // restore old fillstyle |
||
| 128 | this.contex.fillStyle=oldcolor; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | <b>Draw a line on the canvas.</b><br> |
||
| 133 | canvas.line(x1,y1,x2,y2,width,color)<br> |
||
| 134 | |||
| 135 | @function canvas.line |
||
| 136 | @param {Number in pixel} x1 The x coord of the line start in the canvas. |
||
| 137 | @param {Number in pixel} y1 The y coord of the line start in the canvas. |
||
| 138 | @param {Number in pixel} x2 The x coord of the line end in the canvas. |
||
| 139 | @param {Number in pixel} y2 The y coord of the line end in the canvas. |
||
| 140 | @param {Number in pixel} width The width of the line. |
||
| 141 | @param {Color} color The color of the plot. |
||
| 142 | @example |
||
| 143 | mycanvas.line(0,0,50,50,2,'#FF0000'); |
||
| 144 | */ |
||
| 145 | this.line = function(x1,y1,x2,y2,width,color){ |
||
| 146 | var tmp=this.contex.strokeStyle; |
||
| 147 | this.contex.strokeStyle=color; |
||
| 148 | this.contex.lineWidth=width; |
||
| 149 | this.contex.beginPath(); |
||
| 150 | this.contex.moveTo(x1,y1); |
||
| 151 | this.contex.lineTo(x2,y2); |
||
| 152 | this.contex.stroke(); |
||
| 153 | this.contex.closePath(); |
||
| 154 | this.contex.strokeStyle=tmp; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | <b>Draw a filled triangle on the canvas.</b><br> |
||
| 159 | canvas.triangle(x1,y1,x2,y2,x3,y3,color)<br> |
||
| 160 | |||
| 161 | @function canvas.triangle |
||
| 162 | @param {Number in pixel} x1 The x coord of the 1st edge of the triangle in the canvas. |
||
| 163 | @param {Number in pixel} y1 The y coord of the 1st edge of the triangle in the canvas. |
||
| 164 | @param {Number in pixel} x2 The x coord of the 2nd edge of the triangle in the canvas. |
||
| 165 | @param {Number in pixel} y2 The y coord of the 2nd edge of the triangle in the canvas. |
||
| 166 | @param {Number in pixel} x3 The x coord of the 3rd edge of the triangle in the canvas. |
||
| 167 | @param {Number in pixel} y3 The y coord of the 3rd edge of the triangle in the canvas. |
||
| 168 | @param {Color} color The color of the plot. |
||
| 169 | @example |
||
| 170 | mycanvas.triangle(150,150,250,250,50,250,'#FF0000'); |
||
| 171 | */ |
||
| 172 | this.triangle = function(x1,y1,x2,y2,x3,y3,color){ |
||
| 173 | this.contex.beginPath(); |
||
| 174 | this.contex.moveTo(x1,y1); |
||
| 175 | this.contex.lineTo(x2,y2); |
||
| 176 | this.contex.lineTo(x3,y3); |
||
| 177 | this.contex.closePath(); |
||
| 178 | this.contex.fillStyle=color; |
||
| 179 | this.contex.fill(); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | <b>Draw a filled quad on the canvas.</b><br> |
||
| 184 | <br> |
||
| 185 | it can be used with 5 params : <br> |
||
| 186 | canvas.quad(x1,y1,w,h,color)<br> |
||
| 187 | <br> |
||
| 188 | or it can be used with 9 params : <br> |
||
| 189 | canvas.quad(x1,y1,x2,y2,x3,y3,x4,y4,color)<br> |
||
| 190 | @function canvas.quad |
||
| 191 | @param {Number in pixel} x1 The x coord of the 1st edge of the quad in the canvas. |
||
| 192 | @param {Number in pixel} y1 The y coord of the 1st edge of the quad in the canvas. |
||
| 193 | @param {Number in pixel} x2 The x coord of the 2nd edge of the quad in the canvas. |
||
| 194 | @param {Number in pixel} y2 The y coord of the 2nd edge of the quad in the canvas. |
||
| 195 | @param {Number in pixel} x3 The x coord of the 3rd edge of the quad in the canvas. |
||
| 196 | @param {Number in pixel} y3 The y coord of the 3rd edge of the quad in the canvas. |
||
| 197 | @param {Number in pixel} x4 The x coord of the 4th edge of the quad in the canvas. |
||
| 198 | @param {Number in pixel} y4 The y coord of the 4th edge of the quad in the canvas. |
||
| 199 | @param {Number in pixel} w The Width of the quad in the canvas. |
||
| 200 | @param {Number in pixel} h The Height of the quad in the canvas. |
||
| 201 | @param {Color} color The color of the plot. |
||
| 202 | @example |
||
| 203 | mycanvas.quad(150,150,250,250,'#FF0000'); |
||
| 204 | or |
||
| 205 | mycanvas.quad(0,150,300,150,250,250,50,250,'#FF0000'); |
||
| 206 | */ |
||
| 207 | this.quad = function(x1,y1,x2,y2,x3,y3,x4,y4,color){ |
||
| 208 | // save old fillstyle |
||
| 209 | var oldcolor = this.contex.fillStyle ; |
||
| 210 | |||
| 211 | // if x1 y1 width height color |
||
| 212 | if(arguments.length==5){ |
||
| 213 | this.contex.fillStyle=x3; |
||
| 214 | this.contex.fillRect(x1,y1,x2,y2) ; |
||
| 215 | } |
||
| 216 | // if all quad coordinates |
||
| 217 | else{ |
||
| 218 | this.contex.beginPath(); |
||
| 219 | this.contex.moveTo(x1,y1); |
||
| 220 | this.contex.lineTo(x2,y2); |
||
| 221 | this.contex.lineTo(x3,y3); |
||
| 222 | this.contex.lineTo(x4,y4); |
||
| 223 | this.contex.closePath(); |
||
| 224 | this.contex.fillStyle=color; |
||
| 225 | this.contex.fill(); |
||
| 226 | } |
||
| 227 | // restore old fillstyle |
||
| 228 | this.contex.fillStyle=oldcolor; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | <b>Init a tileset canvas.</b><br> |
||
| 233 | canvas.initTile(tilew,tileh, tilestart)<br> |
||
| 234 | |||
| 235 | @function canvas.initTile |
||
| 236 | @param {Number in pixel} tilew The Width of one tile. |
||
| 237 | @param {Number in pixel} tileh The Height of one tile. |
||
| 238 | @param {Number} [tilestart] The number of the first tile. (0 by default) |
||
| 239 | @example |
||
| 240 | mycanvas.initTile(32,32); |
||
| 241 | */ |
||
| 242 | this.initTile=function(tilew,tileh, tilestart){ |
||
| 243 | this.tileh=tileh; |
||
| 244 | this.tilew=tilew; |
||
| 245 | if(typeof(tilestart)!='undefined') |
||
| 246 | this.tilestart=tilestart; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | <b>Draw the canvas to another canvas.</b><br> |
||
| 251 | canvas.draw(dst,x,y,alpha, rot,w,h)<br> |
||
| 252 | |||
| 253 | @function canvas.draw |
||
| 254 | @param {Object} dst The destination canvas. |
||
| 255 | @param {Number in pixel} x The x coord in the destination canvas (based on the handle coord). |
||
| 256 | @param {Number in pixel} y The y coord in the destination canvas (based on the handle coord). |
||
| 257 | @param {Number} [alpha] The normalized value of the alpha (1 by default). |
||
| 258 | @param {Number} [rot] The rotation angle in degrees (0 by default) (will use the handle coord as rotation axis). |
||
| 259 | @param {Number} [w] The normalized zoom factor on x (1 by default). |
||
| 260 | @param {Number} [h] The normalized zoom factor on y (1 by default). |
||
| 261 | @example |
||
| 262 | mycanvas.draw(destcanvas,10,10,1,0,1,1); |
||
| 263 | */ |
||
| 264 | View Code Duplication | this.draw = function(dst,x,y,alpha, rot,w,h){ |
|
| 265 | var tmp=dst.contex.globalAlpha; |
||
| 266 | if(typeof(alpha)=='undefined') alpha=1; |
||
| 267 | dst.contex.globalAlpha=alpha; |
||
| 268 | if(arguments.length==3 || arguments.length==4) |
||
| 269 | dst.contex.drawImage(this.canvas, x-this.handlex,y-this.handley); |
||
| 270 | else if(arguments.length==5){ |
||
| 271 | dst.contex.translate(x,y); |
||
| 272 | dst.contex.rotate(rot*Math.PI/180); |
||
| 273 | dst.contex.translate(-this.handlex,-this.handley); |
||
| 274 | dst.contex.drawImage(this.canvas, 0,0); |
||
| 275 | dst.contex.setTransform(1, 0, 0, 1, 0, 0); |
||
| 276 | } |
||
| 277 | else{ |
||
| 278 | dst.contex.translate(x,y); |
||
| 279 | dst.contex.rotate(rot*Math.PI/180); |
||
| 280 | dst.contex.scale(w,h); |
||
| 281 | dst.contex.translate(-this.handlex,-this.handley); |
||
| 282 | dst.contex.drawImage(this.canvas, 0,0); |
||
| 283 | dst.contex.setTransform(1, 0, 0, 1, 0, 0); |
||
| 284 | } |
||
| 285 | dst.contex.globalAlpha=tmp; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | <b>Draw a tile from this canvas to another canvas.</b><br> |
||
| 290 | canvas.drawTile(dst, nb, x, y, alpha, rot, w, h)<br> |
||
| 291 | |||
| 292 | @function canvas.drawTile |
||
| 293 | @param {Object} dst The destination canvas. |
||
| 294 | @param {Number} nb the tile number. |
||
| 295 | @param {Number in pixel} x The x coord in the destination canvas (based on the handle coord). |
||
| 296 | @param {Number in pixel} y The y coord in the destination canvas (based on the handle coord). |
||
| 297 | @param {Number} [alpha] The normalized value of the alpha (1 by default). |
||
| 298 | @param {Number} [rot] The rotation angle in degrees (0 by default) (will use the handle coord as rotation axis). |
||
| 299 | @param {Number} [w] The normalized zoom factor on x (1 by default). |
||
| 300 | @param {Number} [h] The normalized zoom factor on y (1 by default). |
||
| 301 | @example |
||
| 302 | mycanvas.drawTile(destcanvas,5,10,10,1,0,1,1); |
||
| 303 | */ |
||
| 304 | this.drawTile = function(dst, nb, x, y, alpha, rot, w, h){ |
||
| 305 | var tmp=dst.contex.globalAlpha; |
||
| 306 | if(typeof(alpha)=='undefined') alpha=1; |
||
| 307 | dst.contex.globalAlpha=alpha; |
||
| 308 | this.drawPart(dst,x,y,Math.floor((nb%(this.canvas.width/this.tilew)))*this.tilew,Math.floor(nb/(this.canvas.width/this.tilew))*this.tileh,this.tilew,this.tileh,alpha, rot, w, h); |
||
| 309 | dst.contex.globalAlpha=tmp; |
||
| 310 | |||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | <b>Draw a part of this canvas to another canvas.</b><br> |
||
| 315 | canvas.drawPart(dst,x,y,partx,party,partw,parth,alpha, rot,zx,zy)<br> |
||
| 316 | |||
| 317 | @function canvas.drawPart |
||
| 318 | @param {Object} dst The destination canvas. |
||
| 319 | @param {Number in pixel} x The x coord in the destination canvas (based on the handle coord). |
||
| 320 | @param {Number in pixel} y The y coord in the destination canvas (based on the handle coord). |
||
| 321 | @param {Number in pixel} partx The x coord of the part in the source canvas. |
||
| 322 | @param {Number in pixel} party The y coord of the part in the source canvas. |
||
| 323 | @param {Number in pixel} partw The width of the part in the source canvas. |
||
| 324 | @param {Number in pixel} parth The height of the part in the source canvas. |
||
| 325 | @param {Number} [alpha] The normalized value of the alpha (1 by default). |
||
| 326 | @param {Number} [rot] The rotation angle in degrees (0 by default) (will use the handle coord as rotation axis). |
||
| 327 | @param {Number} [zx] The normalized zoom factor on x (1 by default). |
||
| 328 | @param {Number} [zy] The normalized zoom factor on y (1 by default). |
||
| 329 | @example |
||
| 330 | mycanvas.drawTile(mycanvas,10,10,0,0,50,50,1,0,1,1); |
||
| 331 | */ |
||
| 332 | View Code Duplication | this.drawPart = function(dst,x,y,partx,party,partw,parth,alpha, rot,zx,zy){ |
|
| 333 | if(partx<0) { |
||
| 334 | x-=partx/(this.midhandled==true?2:1); |
||
| 335 | partw+=partx; |
||
| 336 | partx=0; |
||
| 337 | } else { |
||
| 338 | if (this.midhandled==false) { |
||
| 339 | partw = Math.min(partw,this.width-partx); |
||
| 340 | } |
||
| 341 | } |
||
| 342 | if(party<0) { |
||
| 343 | y-=party/(this.midhandled==true?2:1); |
||
| 344 | parth+=party; |
||
| 345 | party=0; |
||
| 346 | } else { |
||
| 347 | if (this.midhandled==false) { |
||
| 348 | parth = Math.min(parth,this.height-party); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | if(partw<=0 || parth<=0){ |
||
| 352 | return; |
||
| 353 | } |
||
| 354 | var tmp=dst.contex.globalAlpha; |
||
| 355 | if(typeof(alpha)=='undefined') alpha=1; |
||
| 356 | dst.contex.globalAlpha=alpha; |
||
| 357 | if(arguments.length==7 || arguments.length==8){ |
||
| 358 | dst.contex.translate(x,y); |
||
| 359 | if(this.midhandled==true) dst.contex.translate(-partw/2,-parth/2); else dst.contex.translate(-this.handlex,-this.handley); |
||
| 360 | dst.contex.drawImage(this.canvas,partx,party,partw,parth,0,0,partw,parth); |
||
| 361 | dst.contex.setTransform(1, 0, 0, 1, 0, 0); |
||
| 362 | } |
||
| 363 | else if(arguments.length==9){ |
||
| 364 | dst.contex.translate(x,y); |
||
| 365 | dst.contex.rotate(rot*Math.PI/180); |
||
| 366 | if(this.midhandled==true) dst.contex.translate(-partw/2,-parth/2); else dst.contex.translate(-this.handlex,-this.handley); |
||
| 367 | dst.contex.drawImage(this.canvas,partx,party,partw,parth,0,0,partw,parth); |
||
| 368 | dst.contex.setTransform(1, 0, 0, 1, 0, 0); |
||
| 369 | } |
||
| 370 | else{ |
||
| 371 | dst.contex.translate(x,y); |
||
| 372 | dst.contex.rotate(rot*Math.PI/180); |
||
| 373 | dst.contex.scale(zx,zy); |
||
| 374 | if(this.midhandled==true) dst.contex.translate(-partw/2,-parth/2); else dst.contex.translate(-this.handlex,-this.handley); |
||
| 375 | dst.contex.drawImage(this.canvas,partx,party,partw,parth,0,0,partw,parth); |
||
| 376 | dst.contex.setTransform(1, 0, 0, 1, 0, 0); |
||
| 377 | } |
||
| 378 | dst.contex.globalAlpha=tmp; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | <b>Set the handle coord of this canvas to the center.</b><br> |
||
| 383 | |||
| 384 | @function canvas.setmidhandle |
||
| 385 | @example |
||
| 386 | mycanvas.setmidhandle(); |
||
| 387 | */ |
||
| 388 | this.setmidhandle=function(){ |
||
| 389 | this.handlex=parseInt(this.canvas.width/2); |
||
| 390 | this.handley=parseInt(this.canvas.height/2); |
||
| 391 | this.midhandled=true; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | <b>Set the handle of the canvas.</b><br> |
||
| 396 | canvas.sethandle(x,y)<br> |
||
| 397 | |||
| 398 | @function canvas.sethandle |
||
| 399 | @param {Number in pixel} x The x coord of the handle of the canvas. |
||
| 400 | @param {Number in pixel} y The y coord of the handle of the canvas. |
||
| 401 | @example |
||
| 402 | mycanvas.sethandle(50,50); |
||
| 403 | */ |
||
| 404 | this.sethandle=function(x,y){ |
||
| 405 | this.handlex=x; |
||
| 406 | this.handley=y; |
||
| 407 | this.midhandled=false; |
||
| 408 | } |
||
| 409 | |||
| 410 | this.print=function(dst, str, x, y, alpha, rot, w, h){ |
||
| 411 | for(var i=0; i<str.length; i++){ |
||
| 412 | if(typeof(w)!='undefined') |
||
| 413 | this.drawTile(dst, str[i].charCodeAt(0)-this.tilestart,x+i*this.tilew*w,y,alpha,rot,w,h); |
||
| 414 | else |
||
| 415 | this.drawTile(dst, str[i].charCodeAt(0)-this.tilestart,x+i*this.tilew,y,alpha,rot,w,h); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | |||
| 420 | return this; |
||
| 421 | |||
| 422 | } |
||
| 423 | |||
| 648 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.