Advertisement
Guest User

main.ts

a guest
Jan 2nd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <reference path="defs/easeljs.d.ts" />
  2. /// <reference path="defs/stats.d.ts" />
  3. /// <reference path="CustomShape.ts"/>
  4. /// <reference path="Shape3D.ts"/>
  5.  
  6. module djankey {
  7.     export class ShapesEffect {
  8.         private canvas:HTMLCanvasElement;
  9.         private stage:createjs.Stage;
  10.         private canvas_rect:ClientRect;
  11.         private shapes:Array<djankey.Shape3D> = [];
  12.         private mouse_x:number;
  13.         private mouse_y:number;
  14.         private stats:Stats;
  15.  
  16.         constructor(canvasID:string) {
  17.             this.canvas = <HTMLCanvasElement>document.getElementById(canvasID);
  18.             if (!this.canvas || !this.canvas.getContext) {
  19.                 alert('HTML5 Canvas is not supported!');
  20.             } else {
  21.                 this.init(canvasID);
  22.             }
  23.         }
  24.  
  25.         init = (id:string):void => {
  26.             // stage
  27.             this.stage = new createjs.Stage(this.canvas);
  28.  
  29.             //this.stage = new createjs.SpriteStage(id, false, false);
  30.             //this.stage.updateViewport(this.canvas.width, this.canvas.height);
  31.             this.stage.mouseChildren = false;
  32.             this.stage.mouseEnabled = false;
  33.  
  34.             // stats
  35.             this.stats = new Stats();
  36.             this.stats.setMode(0);
  37.             document.getElementById("stats").appendChild(this.stats.domElement);
  38.  
  39.             // pointer
  40.             this.canvas_rect = this.canvas.getBoundingClientRect();
  41.             this.canvas.addEventListener("mousemove", this.onMousemove, true);
  42.             this.canvas.addEventListener("touchmove", this.onTouchmove, true);
  43.  
  44.             // update
  45.             createjs.Ticker.timingMode = createjs.Ticker.RAF;
  46.             createjs.Ticker.addEventListener("tick", this.update);
  47.         }
  48.  
  49.         onMousemove = (event:any):void => {
  50.             this.mouse_x = event.clientX - this.canvas_rect.left;
  51.             this.mouse_y = event.clientY - this.canvas_rect.top;
  52.             this.pointerMove();
  53.         }
  54.  
  55.         onTouchmove = (event:any):void => {
  56.             this.mouse_x = event.touches[0].clientX - this.canvas_rect.left;
  57.             this.mouse_y = event.touches[0].clientY - this.canvas_rect.top;
  58.             this.pointerMove();
  59.         }
  60.  
  61.         pointerMove = ():void => {
  62.             var s:djankey.Shape3D;
  63.             var c:number = 3;
  64.             var color:string = '#'+Math.floor(Math.random()*16777215).toString(16);     // http://www.paulirish.com/2009/random-hex-color-code-snippets/
  65.             var cs:djankey.CustomShape;
  66.  
  67.             while(c-->0){
  68.                 cs = new djankey.CustomShape(color);
  69.                 s = new djankey.Shape3D(cs.cacheCanvas.toDataURL());
  70.                 this.stage.addChild(s);
  71.                 s.x = this.mouse_x;
  72.                 s.y = this.mouse_y;
  73.                 this.shapes.push(s);
  74.             }
  75.         }
  76.  
  77.         update = ():void => {
  78.             var t:number = new Date().getTime();
  79.             var s:djankey.Shape3D;
  80.  
  81.             for (var i:number = this.shapes.length - 1; i >= 0;i-- ) {
  82.                 s = this.shapes[i];
  83.                 s.update(t);
  84.                 if (s.power<=0) {
  85.                     this.shapes.splice(i, 1);
  86.                     if (s.parent) {
  87.                         s.parent.removeChild(s);
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             this.stage.update();
  93.             this.stats.update();
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement