github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/website/source/assets/javascripts/app/Engine.Shape.js (about)

     1  (function(
     2  	Engine,
     3  	Point,
     4  	Polygon,
     5  	Vector
     6  ){
     7  
     8  Engine.Shape = function(x, y, width, height, points, polygons){
     9  	var i, ref, point, poly;
    10  
    11  	this.pos  = new Vector(x, y);
    12  	this.size = new Vector(width, height);
    13  	this.sizeRef = this.size.clone();
    14  
    15  	ref = {};
    16  	this.points = [];
    17  	this.polygons = [];
    18  
    19  	for (i = 0; i < points.length; i++) {
    20  		point = new Point(
    21  			points[i].id,
    22  			points[i].x,
    23  			points[i].y,
    24  			this.size
    25  		);
    26  		ref[point.id] = point;
    27  		this.points.push(point);
    28  	}
    29  
    30  	for (i = 0; i < polygons.length; i++) {
    31  		poly = polygons[i];
    32  		this.polygons.push(new Polygon(
    33  			ref[poly.points[0]],
    34  			ref[poly.points[1]],
    35  			ref[poly.points[2]],
    36  			poly.color,
    37  			poly.stroke
    38  		));
    39  	}
    40  };
    41  
    42  Engine.Shape.prototype = {
    43  
    44  	breathing: false,
    45  
    46  	breath: 0,
    47  	breathLength: 1,
    48  	breatheIn: false,
    49  
    50  	resize: function(newSize, offset){
    51  		var len, p;
    52  
    53  		this.size.x = newSize;
    54  		this.size.y = newSize;
    55  		this.sizeRef.x = newSize;
    56  		this.sizeRef.y = newSize;
    57  
    58  		this.pos.x = -(newSize / 2);
    59  		this.pos.y = -(newSize / 2 + offset);
    60  
    61  		for (p = 0, len = this.points.length; p < len; p++) {
    62  			this.points[p].resize();
    63  		}
    64  	},
    65  
    66  	startBreathing: function(){
    67  		var p;
    68  
    69  		this.breathing = true;
    70  		this.breath = this.breathLength;
    71  
    72  		for (p = 0; p < this.points.length; p++) {
    73  			this.points[p].updateBreathingPhysics();
    74  		}
    75  	},
    76  
    77  	breathe: function(tick){
    78  		var p, scale, newSize;
    79  
    80  		this.breath += tick;
    81  
    82  		if (this.breath < this.breathLength) {
    83  			return;
    84  		}
    85  
    86  		scale = 1;
    87  
    88  		newSize = Vector.mult(this.sizeRef, scale);
    89  
    90  		for (p = 0; p < this.points.length; p++) {
    91  			this.points[p].updateTarget(newSize);
    92  		}
    93  
    94  		this.breath = 0;
    95  	},
    96  
    97  	update: function(engine){
    98  		var p;
    99  
   100  		if (this.breathing === true) {
   101  			this.breathe(engine.tick);
   102  		}
   103  
   104  		for (p = 0; p < this.points.length; p++)  {
   105  			this.points[p].update(engine);
   106  		}
   107  
   108  		for (p = 0; p < this.polygons.length; p++) {
   109  			this.polygons[p].update(engine);
   110  		}
   111  
   112  		return this;
   113  	},
   114  
   115  	draw: function(ctx, scale, engine){
   116  		var p, poly;
   117  
   118  		ctx.translate(
   119  			this.pos.x * scale >> 0,
   120  			this.pos.y * scale >> 0
   121  		);
   122  		for (p = 0; p < this.polygons.length; p++) {
   123  			poly = this.polygons[p];
   124  			ctx.beginPath();
   125  			ctx.moveTo(
   126  				poly.a.pos.x * scale,
   127  				poly.a.pos.y * scale
   128  			);
   129  			ctx.lineTo(
   130  				poly.b.pos.x * scale,
   131  				poly.b.pos.y * scale
   132  			);
   133  			ctx.lineTo(
   134  				poly.c.pos.x * scale,
   135  				poly.c.pos.y * scale
   136  			);
   137  			ctx.closePath();
   138  			ctx.fillStyle   = poly.fillStyle;
   139  			ctx.fill();
   140  			ctx.lineWidth = poly.strokeWidth * scale;
   141  			ctx.strokeStyle = poly.strokeStyle;
   142  			ctx.stroke();
   143  		}
   144  		ctx.setTransform(1, 0, 0, 1, 0, 0);
   145  		ctx.translate(
   146  			engine.width  / 2 * engine.scale >> 0,
   147  			engine.height / 2 * engine.scale >> 0
   148  		);
   149  		return this;
   150  	}
   151  
   152  };
   153  
   154  })(
   155  	window.Engine,
   156  	window.Engine.Point,
   157  	window.Engine.Polygon,
   158  	window.Vector
   159  );