github.com/jmbataller/terraform@v0.6.8-0.20151125192640-b7a12e3a580c/website/source/assets/javascripts/app/Engine.Point.js (about)

     1  (function(
     2  	Engine,
     3  	Vector
     4  ){ 'use strict';
     5  
     6  Engine.Point = function(id, x, y, shapeSize){
     7  	this.id = id;
     8  
     9  	this.shapeSize = shapeSize;
    10  	this.ref = new Vector(x, y);
    11  
    12  	this.pos = new Vector(
    13  		x * shapeSize.x,
    14  		y * shapeSize.y
    15  	);
    16  
    17  	this.target = this.pos.clone();
    18  	this.pos.x  = shapeSize.x / 2;
    19  	this.pos.y  = shapeSize.y / 2;
    20  	this.accel  = Vector.coerce(this.accel);
    21  	this.vel    = Vector.coerce(this.vel);
    22  
    23  	this.stiffness = Engine.getRandomFloat(150, 600);
    24  	this.friction = Engine.getRandomFloat(12, 18);
    25  };
    26  
    27  Engine.Point.prototype = {
    28  
    29  	radius: 1,
    30  
    31  	stiffness : 200,
    32  	friction  : 13,
    33  	threshold : 0.03,
    34  
    35  	pos: {
    36  		x: 0,
    37  		y: 0
    38  	},
    39  
    40  	accel: {
    41  		x: 0,
    42  		y: 0
    43  	},
    44  
    45  	vel : {
    46  		x: 0,
    47  		y: 0
    48  	},
    49  
    50  	target: {
    51  		x: 0,
    52  		y: 0
    53  	},
    54  
    55  	resize: function(){
    56  		this.target.x = this.pos.x = this.ref.x * this.shapeSize.x;
    57  		this.target.y = this.pos.y = this.ref.y * this.shapeSize.y;
    58  	},
    59  
    60  	updateBreathingPhysics: function(){
    61  		this.stiffness = Engine.getRandomFloat(2, 4);
    62  		this.friction  = Engine.getRandomFloat(1, 2);
    63  	},
    64  
    65  	updateTarget: function(newSize){
    66  		var diff;
    67  
    68  		this.target.x = this.ref.x * newSize.x;
    69  		this.target.y = this.ref.y * newSize.y;
    70  
    71  		diff = Vector.sub(newSize, this.shapeSize).div(2);
    72  
    73  		this.target.sub(diff);
    74  
    75  		this.target.add({
    76  			x: Engine.getRandomFloat(-3, 3),
    77  			y: Engine.getRandomFloat(-3, 3)
    78  		});
    79  	},
    80  
    81  	update: function(engine){
    82  		var newAccel;
    83  
    84  		newAccel = Vector.sub(this.target, this.pos)
    85  			.mult(this.stiffness)
    86  			.sub(Vector.mult(this.vel, this.friction));
    87  
    88  		this.accel.set(newAccel);
    89  
    90  		this.vel.add(Vector.mult(this.accel, engine.tick));
    91  
    92  		this.pos.add(
    93  			Vector.mult(this.vel, engine.tick)
    94  		);
    95  
    96  		newAccel = null;
    97  
    98  		return this;
    99  	},
   100  
   101  	draw: function(ctx, scale){
   102  		ctx.beginPath();
   103  		ctx.arc(
   104  			this.pos.x  * scale,
   105  			this.pos.y  * scale,
   106  			this.radius * scale,
   107  			0,
   108  			Math.PI * 2,
   109  			false
   110  		);
   111  		ctx.fillStyle = '#ffffff';
   112  		ctx.fill();
   113  		return this;
   114  	}
   115  
   116  };
   117  
   118  })(window.Engine, window.Vector);