Advertisement
Guest User

main.ts

a guest
Jan 5th, 2015
217
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="Delaunay.ts" />
  4.  
  5. module djankey {
  6.     export class DelaunayTriangulation {
  7.         private canvas:HTMLCanvasElement;
  8.         private stage:createjs.Stage;
  9.         private stats:Stats;
  10.         private w:number;
  11.         private h:number;
  12.  
  13.         private disp:createjs.Shape;
  14.         private delaunay:djankey.Delaunay;
  15.         private indices:Array<number> = [];
  16.         private points:Array<createjs.Point> = new Array<createjs.Point>(250);
  17.  
  18.  
  19.         constructor(canvasID:string) {
  20.             this.canvas = <HTMLCanvasElement>document.getElementById(canvasID);
  21.             if (!this.canvas || !this.canvas.getContext) {
  22.                 alert('HTML5 Canvas is not supported!');
  23.             } else {
  24.                 this.init(canvasID);
  25.             }
  26.         }
  27.  
  28.         init = (id:string):void => {
  29.             // stage
  30.             this.stage = new createjs.Stage(this.canvas);
  31.             this.stage.mouseChildren = false;
  32.             this.stage.mouseEnabled = false;
  33.  
  34.             this.w = this.canvas.width;
  35.             this.h = this.canvas.height;
  36.  
  37.             // stats
  38.             this.stats = new Stats();
  39.             this.stats.setMode(0);
  40.             document.body.appendChild(this.stats.domElement);
  41.  
  42.             this.disp = new createjs.Shape();
  43.             this.stage.addChild(this.disp);
  44.  
  45.             this.delaunay = new djankey.Delaunay();
  46.             this.reset();
  47.  
  48.             // update
  49.             createjs.Ticker.timingMode = createjs.Ticker.RAF;
  50.             createjs.Ticker.addEventListener("tick", this.update);
  51.         }
  52.  
  53.         reset = ():void => {
  54.             this.disp.graphics.clear();
  55.             this.disp.graphics.beginFill("#CC0000");
  56.  
  57.             var p:createjs.Point;
  58.             for (var i:number = 0; i < this.points.length; i++)
  59.             {
  60.                 if (this.points[i] == null) this.points[i] = new createjs.Point( Math.random() * this.w, Math.random() * this.h );
  61.                 else {
  62.                     this.points[i].x += Math.sin(i);
  63.                     this.points[i].y += Math.cos(i);
  64.                     while (this.points[i].x > this.w) this.points[i].x -= this.w;
  65.                     while (this.points[i].x < 0) this.points[i].x += this.w;
  66.                     while (this.points[i].y > this.h) this.points[i].y -= this.h;
  67.                     while (this.points[i].y < 0) this.points[i].y += this.h;
  68.                 }
  69.  
  70.                 p = this.points[i];
  71.                 this.disp.graphics.moveTo(p.x, p.y).drawCircle(p.x, p.y, 2);
  72.             }
  73.  
  74.             this.disp.graphics.endFill().setStrokeStyle(1, "round").beginStroke("#DDDDDD");
  75.             this.indices = this.delaunay.compute(this.points);
  76.             this.delaunay.render( this.disp.graphics, this.points, this.indices);
  77.         }
  78.  
  79.         update = ():void => {
  80.             this.reset();
  81.             this.stage.update();
  82.             this.stats.update();
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement