1 2 3 4 (function(GLGE){ 5 /** 6 * @class Creates a heightmap for a region of the world based on an image. Originally created as a quick and easy collision detection. At least until we have a better physics implementation. 7 * @deprecated not intended as a permanent addition 8 * @param {string} imageURL The url of the image to generate the hightmap for 9 * @param {number} imageWidth The width of the image 10 * @param {number} imageHeight The height of the image 11 * @param {number} x1 The lower X bound of the height map in world coords 12 * @param {number} x2 The upper X bound of the height map in world coords 13 * @param {number} y1 The lower Y bound of the height map in world coords 14 * @param {number} y2 The upper Y bound of the height map in world coords 15 * @param {number} z1 The lower Z bound of the height map in world coords 16 * @param {number} z2 The upper Z bound of the height map in world coords 17 */ 18 GLGE.HeightMap=function(imageURL,imageWidth,imageHeight,x1,x2,y1,y2,z1,z2){ 19 this.canvas=document.createElement("canvas"); 20 this.context = this.canvas.getContext('2d'); 21 this.canvas.width=imageWidth; 22 this.canvas.height=imageHeight; 23 this.minX=x1; 24 this.maxX=x2; 25 this.minY=y1; 26 this.maxY=y2; 27 this.minZ=z1; 28 this.maxZ=z2; 29 var image=new Image(); 30 image.heightmap=this; 31 image.onload=function(e){ 32 this.heightmap.context.drawImage(this, 0, 0); 33 this.heightmap.data=this.heightmap.context.getImageData(0,0,this.heightmap.canvas.width,this.heightmap.canvas.height).data; 34 }; 35 image.src=imageURL; 36 } 37 GLGE.HeightMap.prototype.canvas=null; 38 GLGE.HeightMap.prototype.context=null; 39 GLGE.HeightMap.prototype.minZ=null; 40 GLGE.HeightMap.prototype.maxZ=null; 41 GLGE.HeightMap.prototype.minY=null; 42 GLGE.HeightMap.prototype.maxY=null; 43 GLGE.HeightMap.prototype.minX=null; 44 GLGE.HeightMap.prototype.maxX=null; 45 GLGE.HeightMap.prototype.data=null; 46 /** 47 * Gets the pixel height at the specified image coords 48 * @param {number} x the x image coord 49 * @param {number} y the y image coord 50 * @private 51 */ 52 GLGE.HeightMap.prototype.getPixelAt=function(x,y){ 53 if(this.data){ 54 return (this.data[(this.canvas.width*y+x)*4])/255*(this.maxZ-this.minZ); 55 } 56 else 57 { 58 return 0; 59 } 60 } 61 /** 62 * Function to get he height as specified x, y world coords 63 * @param {number} x the x world coord 64 * @param {number} y the y world coord 65 * @returns {number} the height of the level in world units 66 */ 67 GLGE.HeightMap.prototype.getHeightAt=function(x,y){ 68 var imgX=Math.round((x-this.minX)/(this.maxX-this.minX)*this.canvas.width); 69 var imgY=Math.round((y-this.minY)/(this.maxY-this.minY)*this.canvas.height); 70 return this.getPixelAt(imgX,imgY); 71 } 72 /** 73 * @class Monitors keyboard input for use in render loops 74 */ 75 GLGE.KeyInput=function(){ 76 if(!document.keyStates) document.keyStates=[]; 77 document.addEventListener("keydown",this.onKeyDown,false); 78 document.addEventListener("keyup",this.onKeyUp,false); 79 } 80 /** 81 * Tests if a key is pressed 82 * @param {number} the keycode to check 83 * @returns {boolean} key returns true if the key is being pressed 84 */ 85 GLGE.KeyInput.prototype.isKeyPressed=function(key){ 86 if(document.keyStates[key]) return true; 87 else return false; 88 }; 89 /** 90 * document keydown event used to monitor the key states 91 * @param {event} e the event being fired 92 * @private 93 */ 94 GLGE.KeyInput.prototype.onKeyDown=function(e){ 95 document.keyStates[e.keyCode]=true; 96 }; 97 /** 98 * Document keyup event used to monitor the key states 99 * @param {event} e the event being fired 100 * @private 101 */ 102 GLGE.KeyInput.prototype.onKeyUp=function(e){ 103 document.keyStates[e.keyCode]=false; 104 }; 105 /** 106 * @class Monitors mouse input for use in render loops 107 */ 108 GLGE.MouseInput=function(element){ 109 this.element=element; 110 this.element.mouseX=0; 111 this.element.mouseY=0; 112 if(!this.element.buttonState) this.element.buttonState=[]; 113 element.addEventListener("mousemove",this.onMouseMove,false); 114 element.addEventListener("mousedown",this.onMouseDown,false); 115 element.addEventListener("mouseup",this.onMouseUp,false); 116 } 117 GLGE.MouseInput.prototype.element=null; 118 /** 119 * Elements mousemove event used to monitor the mouse states 120 * @param {event} e the event being fired 121 * @private 122 */ 123 GLGE.MouseInput.prototype.onMouseMove=function(e){ 124 this.mouseX=e.clientX; 125 this.mouseY=e.clientY; 126 } 127 /** 128 * Elements mousedown event used to monitor the mouse states 129 * @param {event} e the event being fired 130 * @private 131 */ 132 GLGE.MouseInput.prototype.onMouseDown=function(e){ 133 this.buttonState[e.button]=true; 134 } 135 /** 136 * Elements mouseup event used to monitor the mouse states 137 * @param {event} e the event being fired 138 * @private 139 */ 140 GLGE.MouseInput.prototype.onMouseUp=function(e){ 141 this.buttonState[e.button]=false; 142 } 143 /** 144 * Tests if a mouse button is pressed 145 * @param {number} button the button to check 146 * @returns {boolean} returns true if the button is being pressed 147 */ 148 GLGE.MouseInput.prototype.isButtonDown=function(button){ 149 if(this.element.buttonState[button]) return true; 150 else return false; 151 } 152 /** 153 * Gets the mouse coords 154 * @returns {object} the current mouse coors 155 */ 156 GLGE.MouseInput.prototype.getMousePosition=function(){ 157 return {x:this.element.mouseX,y:this.element.mouseY} 158 } 159 160 /** 161 * @constant 162 * @description Enumeration for the left mouse button 163 */ 164 GLGE.MI_LEFT=0; 165 /** 166 * @constant 167 * @description Enumeration for the middle mouse button 168 */ 169 GLGE.MI_MIDDLE=1; 170 /** 171 * @constant 172 * @description Enumeration for the right mouse button 173 */ 174 GLGE.MI_RIGHT=2; 175 176 /** 177 * @constant 178 * @description Enumeration for the backspace key 179 */ 180 GLGE.KI_BACKSPACE=8; 181 /** 182 * @constant 183 * @description Enumeration for the tab key 184 */ 185 GLGE.KI_TAB=9; 186 /** 187 * @constant 188 * @description Enumeration for the enter key 189 */ 190 GLGE.KI_ENTER=13; 191 /** 192 * @constant 193 * @description Enumeration for the shift key 194 */ 195 GLGE.KI_SHIFT=16; 196 /** 197 * @constant 198 * @description Enumeration for the ctrl key 199 */ 200 GLGE.KI_CTRL=17; 201 /** 202 * @constant 203 * @description Enumeration for the alt key 204 */ 205 GLGE.KI_ALT=18; 206 /** 207 * @constant 208 * @description Enumeration for the pause/break key 209 */ 210 GLGE.KI_PAUSE_BREAK=19; 211 /** 212 * @constant 213 * @description Enumeration for the caps lock key 214 */ 215 GLGE.KI_CAPS_LOCK=20; 216 /** 217 * @constant 218 * @description Enumeration for the escape key 219 */ 220 GLGE.KI_ESCAPE=27; 221 /** 222 * @constant 223 * @description Enumeration for the page up key 224 */ 225 GLGE.KI_PAGE_UP=33; 226 /** 227 * @constant 228 * @description Enumeration for the page down key 229 */ 230 GLGE.KI_PAGE_DOWN=34; 231 /** 232 * @constant 233 * @description Enumeration for the end key 234 */ 235 GLGE.KI_END=35; 236 /** 237 * @constant 238 * @description Enumeration for the home key 239 */ 240 GLGE.KI_HOME=36; 241 /** 242 * @constant 243 * @description Enumeration for the left arrow key 244 */ 245 GLGE.KI_LEFT_ARROW=37; 246 /** 247 * @constant 248 * @description Enumeration for the up arrow key 249 */ 250 GLGE.KI_UP_ARROW=38; 251 /** 252 * @constant 253 * @description Enumeration for the right arrow key 254 */ 255 GLGE.KI_RIGHT_ARROW=39; 256 /** 257 * @constant 258 * @description Enumeration for the down arrow key 259 */ 260 GLGE.KI_DOWN_ARROW=40; 261 /** 262 * @constant 263 * @description Enumeration for the insert key 264 */ 265 GLGE.KI_INSERT=45; 266 /** 267 * @constant 268 * @description Enumeration for the delete key 269 */ 270 GLGE.KI_DELETE=46; 271 /** 272 * @constant 273 * @description Enumeration for the 0 key 274 */ 275 GLGE.KI_0=48; 276 /** 277 * @constant 278 * @description Enumeration for the 1 key 279 */ 280 GLGE.KI_1=49; 281 /** 282 * @constant 283 * @description Enumeration for the 2 key 284 */ 285 GLGE.KI_2=50; 286 /** 287 * @constant 288 * @description Enumeration for the 3 key 289 */ 290 GLGE.KI_3=51; 291 /** 292 * @constant 293 * @description Enumeration for the 4 key 294 */ 295 GLGE.KI_4=52; 296 /** 297 * @constant 298 * @description Enumeration for the 5 key 299 */ 300 GLGE.KI_5=53; 301 /** 302 * @constant 303 * @description Enumeration for the 6 key 304 */ 305 GLGE.KI_6=54; 306 /** 307 * @constant 308 * @description Enumeration for the 7 key 309 */ 310 GLGE.KI_7=55; 311 /** 312 * @constant 313 * @description Enumeration for the 8 key 314 */ 315 GLGE.KI_8=56; 316 /** 317 * @constant 318 * @description Enumeration for the 9 key 319 */ 320 GLGE.KI_9=57; 321 /** 322 * @constant 323 * @description Enumeration for the a key 324 */ 325 GLGE.KI_A=65; 326 /** 327 * @constant 328 * @description Enumeration for the b key 329 */ 330 GLGE.KI_B=66; 331 /** 332 * @constant 333 * @description Enumeration for the c key 334 */ 335 GLGE.KI_C=67; 336 /** 337 * @constant 338 * @description Enumeration for the d key 339 */ 340 GLGE.KI_D=68; 341 /** 342 * @constant 343 * @description Enumeration for the e key 344 */ 345 GLGE.KI_E=69; 346 /** 347 * @constant 348 * @description Enumeration for the f key 349 */ 350 GLGE.KI_F=70; 351 /** 352 * @constant 353 * @description Enumeration for the g key 354 */ 355 GLGE.KI_G=71; 356 /** 357 * @constant 358 * @description Enumeration for the h key 359 */ 360 GLGE.KI_H=72; 361 /** 362 * @constant 363 * @description Enumeration for the i key 364 */ 365 GLGE.KI_I=73; 366 /** 367 * @constant 368 * @description Enumeration for the j key 369 */ 370 GLGE.KI_J=74; 371 /** 372 * @constant 373 * @description Enumeration for the k key 374 */ 375 GLGE.KI_K=75; 376 /** 377 * @constant 378 * @description Enumeration for the l key 379 */ 380 GLGE.KI_L=76; 381 /** 382 * @constant 383 * @description Enumeration for the m key 384 */ 385 GLGE.KI_M=77; 386 /** 387 * @constant 388 * @description Enumeration for the n key 389 */ 390 GLGE.KI_N=78; 391 /** 392 * @constant 393 * @description Enumeration for the o key 394 */ 395 GLGE.KI_O=79; 396 /** 397 * @constant 398 * @description Enumeration for the p key 399 */ 400 GLGE.KI_P=80; 401 /** 402 * @constant 403 * @description Enumeration for the q key 404 */ 405 GLGE.KI_Q=81; 406 /** 407 * @constant 408 * @description Enumeration for the r key 409 */ 410 GLGE.KI_R=82; 411 /** 412 * @constant 413 * @description Enumeration for the s key 414 */ 415 GLGE.KI_S=83; 416 /** 417 * @constant 418 * @description Enumeration for the t key 419 */ 420 GLGE.KI_T=84; 421 /** 422 * @constant 423 * @description Enumeration for the u key 424 */ 425 GLGE.KI_U=85; 426 /** 427 * @constant 428 * @description Enumeration for the v key 429 */ 430 GLGE.KI_V=86; 431 /** 432 * @constant 433 * @description Enumeration for the w key 434 */ 435 GLGE.KI_W=87; 436 /** 437 * @constant 438 * @description Enumeration for the x key 439 */ 440 GLGE.KI_X=88; 441 /** 442 * @constant 443 * @description Enumeration for the y key 444 */ 445 GLGE.KI_Y=89; 446 /** 447 * @constant 448 * @description Enumeration for the z key 449 */ 450 GLGE.KI_Z=90; 451 /** 452 * @constant 453 * @description Enumeration for the left window key key 454 */ 455 GLGE.KI_LEFT_WINDOW_KEY=91; 456 /** 457 * @constant 458 * @description Enumeration for the right window key key 459 */ 460 GLGE.KI_RIGHT_WINDOW_KEY=92; 461 /** 462 * @constant 463 * @description Enumeration for the select key key 464 */ 465 GLGE.KI_SELECT_KEY=93; 466 /** 467 * @constant 468 * @description Enumeration for the numpad 0 key 469 */ 470 GLGE.KI_NUMPAD_0=96; 471 /** 472 * @constant 473 * @description Enumeration for the numpad 1 key 474 */ 475 GLGE.KI_NUMPAD_1=97; 476 /** 477 * @constant 478 * @description Enumeration for the numpad 2 key 479 */ 480 GLGE.KI_NUMPAD_2=98; 481 /** 482 * @constant 483 * @description Enumeration for the numpad 3 key 484 */ 485 GLGE.KI_NUMPAD_3=99; 486 /** 487 * @constant 488 * @description Enumeration for the numpad 4 key 489 */ 490 GLGE.KI_NUMPAD_4=100; 491 /** 492 * @constant 493 * @description Enumeration for the numpad 5 key 494 */ 495 GLGE.KI_NUMPAD_5=101; 496 /** 497 * @constant 498 * @description Enumeration for the numpad 6 key 499 */ 500 GLGE.KI_NUMPAD_6=102; 501 /** 502 * @constant 503 * @description Enumeration for the numpad 7 key 504 */ 505 GLGE.KI_NUMPAD_7=103; 506 /** 507 * @constant 508 * @description Enumeration for the numpad 8 key 509 */ 510 GLGE.KI_NUMPAD_8=104; 511 /** 512 * @constant 513 * @description Enumeration for the numpad 9 key 514 */ 515 GLGE.KI_NUMPAD_9=105; 516 /** 517 * @constant 518 * @description Enumeration for the multiply key 519 */ 520 GLGE.KI_MULTIPLY=106; 521 /** 522 * @constant 523 * @description Enumeration for the add key 524 */ 525 GLGE.KI_ADD=107; 526 /** 527 * @constant 528 * @description Enumeration for the subtract key 529 */ 530 GLGE.KI_SUBTRACT=109; 531 /** 532 * @constant 533 * @description Enumeration for the decimal point key 534 */ 535 GLGE.KI_DECIMAL_POINT=110; 536 /** 537 * @constant 538 * @description Enumeration for the divide key 539 */ 540 GLGE.KI_DIVIDE=111; 541 /** 542 * @constant 543 * @description Enumeration for the f1 key 544 */ 545 GLGE.KI_F1=112; 546 /** 547 * @constant 548 * @description Enumeration for the f2 key 549 */ 550 GLGE.KI_F2=113; 551 /** 552 * @constant 553 * @description Enumeration for the f3 key 554 */ 555 GLGE.KI_F3=114; 556 /** 557 * @constant 558 * @description Enumeration for the f4 key 559 */ 560 GLGE.KI_F4=115; 561 /** 562 * @constant 563 * @description Enumeration for the f5 key 564 */ 565 GLGE.KI_F5=116; 566 /** 567 * @constant 568 * @description Enumeration for the f6 key 569 */ 570 GLGE.KI_F6=117; 571 /** 572 * @constant 573 * @description Enumeration for the f7 key 574 */ 575 GLGE.KI_F7=118; 576 /** 577 * @constant 578 * @description Enumeration for the f8 key 579 */ 580 GLGE.KI_F8=119; 581 /** 582 * @constant 583 * @description Enumeration for the f9 key 584 */ 585 GLGE.KI_F9=120; 586 /** 587 * @constant 588 * @description Enumeration for the f10 key 589 */ 590 GLGE.KI_F10=121; 591 /** 592 * @constant 593 * @description Enumeration for the f11 key 594 */ 595 GLGE.KI_F11=122; 596 /** 597 * @constant 598 * @description Enumeration for the f12 key 599 */ 600 GLGE.KI_F12=123; 601 /** 602 * @constant 603 * @description Enumeration for the num lock key 604 */ 605 GLGE.KI_NUM_LOCK=144; 606 /** 607 * @constant 608 * @description Enumeration for the scroll lock key 609 */ 610 GLGE.KI_SCROLL_LOCK=145; 611 /** 612 * @constant 613 * @description Enumeration for the semi-colon key 614 */ 615 GLGE.KI_SEMI_COLON=186; 616 /** 617 * @constant 618 * @description Enumeration for the equal sign key 619 */ 620 GLGE.KI_EQUAL_SIGN=187; 621 /** 622 * @constant 623 * @description Enumeration for the comma key 624 */ 625 GLGE.KI_COMMA=188; 626 /** 627 * @constant 628 * @description Enumeration for the dash key 629 */ 630 GLGE.KI_DASH=189; 631 /** 632 * @constant 633 * @description Enumeration for the period key 634 */ 635 GLGE.KI_PERIOD=190; 636 /** 637 * @constant 638 * @description Enumeration for the forward slash key 639 */ 640 GLGE.KI_FORWARD_SLASH=191; 641 /** 642 * @constant 643 * @description Enumeration for the grave accent key 644 */ 645 GLGE.KI_GRAVE_ACCENT=192; 646 /** 647 * @constant 648 * @description Enumeration for the open bracket key 649 */ 650 GLGE.KI_OPEN_BRACKET=219; 651 /** 652 * @constant 653 * @description Enumeration for the back slash key 654 */ 655 GLGE.KI_BACK_SLASH=220; 656 /** 657 * @constant 658 * @description Enumeration for the close braket key 659 */ 660 GLGE.KI_CLOSE_BRAKET=221; 661 /** 662 * @constant 663 * @description Enumeration for the single quote key 664 */ 665 GLGE.KI_SINGLE_QUOTE=222; 666 })(GLGE); 667 668 669 670 671 /* 672 junk code 673 var canvas=document.createElement("canvas"); 674 canvas.height=300; 675 canvas.width=300; 676 //var canvas=document.getElementById("canvas"); 677 var canvasContext = canvas.getContext('2d'); 678 var map=new Image(); 679 map.onload=function(e){ 680 canvasContext.drawImage(this, 0, 0); 681 document.getElementById('test').src=canvas.toDataURL("image/png"); 682 } 683 map.src="http://ecom.armourhome.co.uk/ecom/themes/q2radio/images/q2background.jpg"; 684 */