From 33be6008d2734b8ed92bb15f91592d0dcb6dbb8d Mon Sep 17 00:00:00 2001 From: Amadeus Demarzi Date: Sat, 26 Jul 2014 13:52:03 -0700 Subject: [PATCH] Basic breathing... needs more refinement --- .../javascripts/app/Engine.Point.Puller.js | 13 ++-- .../source/javascripts/app/Engine.Point.js | 41 ++++++++++-- .../source/javascripts/app/Engine.Shape.js | 62 ++++++++++++++++--- .../javascripts/app/Engine.Typewriter.js | 2 - website/source/javascripts/app/Engine.js | 1 + 5 files changed, 95 insertions(+), 24 deletions(-) diff --git a/website/source/javascripts/app/Engine.Point.Puller.js b/website/source/javascripts/app/Engine.Point.Puller.js index 83da7fd44..910093a60 100644 --- a/website/source/javascripts/app/Engine.Point.Puller.js +++ b/website/source/javascripts/app/Engine.Point.Puller.js @@ -7,16 +7,13 @@ Engine.Point.Puller = function(id, x, y, shapeSize){ this.id = id; this.shapeSize = shapeSize; + this.ref = new Vector(x, y); - this.ref = { - x: x, - y: y - }; + this.pos = new Vector( + x * shapeSize.x, + y * shapeSize.y + ); - this.pos.x = x * shapeSize.x; - this.pos.y = y * shapeSize.y; - - this.pos = Vector.coerce(this.pos); this.home = this.pos.clone(); this.accel = Vector.coerce(this.accel); this.vel = Vector.coerce(this.vel); diff --git a/website/source/javascripts/app/Engine.Point.js b/website/source/javascripts/app/Engine.Point.js index 244f0b981..5a3539b22 100644 --- a/website/source/javascripts/app/Engine.Point.js +++ b/website/source/javascripts/app/Engine.Point.js @@ -3,14 +3,22 @@ Vector ){ 'use strict'; -Engine.Point = function(id, x, y, width, height){ +Engine.Point = function(id, x, y, shapeSize){ this.id = id; - this.pos = new Vector(x, y); + + this.shapeSize = shapeSize; + this.ref = new Vector(x, y); + + this.pos = new Vector( + x * shapeSize.x, + y * shapeSize.y + ); + this.target = this.pos.clone(); - this.pos.x = width / 2; - this.pos.y = height / 2; - this.accel = Vector.coerce(this.accel); - this.vel = Vector.coerce(this.vel); + this.pos.x = shapeSize.x / 2; + this.pos.y = shapeSize.y / 2; + this.accel = Vector.coerce(this.accel); + this.vel = Vector.coerce(this.vel); this.stiffness = Engine.getRandomFloat(3, 6); this.friction = Engine.getRandomFloat(0.15, 0.3); @@ -45,6 +53,27 @@ Engine.Point.prototype = { y: 0 }, + updateBreathingPhysics: function(){ + this.stiffness = 0.1; + this.friction = 0.05; + }, + + updateTarget: function(newSize){ + var diff; + + this.target.x = this.ref.x * newSize.x; + this.target.y = this.ref.y * newSize.y; + + diff = Vector.sub(newSize, this.shapeSize).div(2); + + this.target.sub(diff); + + this.target.add({ + x: Engine.getRandomFloat(-8, 8), + y: Engine.getRandomFloat(-8, 8) + }); + }, + update: function(engine){ var newAccel; diff --git a/website/source/javascripts/app/Engine.Shape.js b/website/source/javascripts/app/Engine.Shape.js index a0383f89e..62558bd76 100644 --- a/website/source/javascripts/app/Engine.Shape.js +++ b/website/source/javascripts/app/Engine.Shape.js @@ -5,11 +5,12 @@ Vector ){ -Engine.Shape = function(x, y, width, height, points, polygons, simple){ +Engine.Shape = function(x, y, width, height, points, polygons){ var i, ref, point, poly; - this.pos = new Vector(x, y); + this.pos = new Vector(x, y); this.size = new Vector(width, height); + this.sizeRef = this.size.clone(); ref = {}; this.points = []; @@ -18,10 +19,9 @@ Engine.Shape = function(x, y, width, height, points, polygons, simple){ for (i = 0; i < points.length; i++) { point = new Point( points[i].id, - points[i].x * this.size.x, - points[i].y * this.size.y, - this.size.x, - this.size.y + points[i].x, + points[i].y, + this.size ); ref[point.id] = point; this.points.push(point); @@ -33,17 +33,63 @@ Engine.Shape = function(x, y, width, height, points, polygons, simple){ ref[poly.points[0]], ref[poly.points[1]], ref[poly.points[2]], - poly.color, - simple + poly.color )); } }; Engine.Shape.prototype = { + breathing: false, + + breath: 0, + breathLength: 1, + breatheIn: false, + + startBreathing: function(){ + var p; + + this.breathing = true; + this.breath = this.breathLength; + + for (p = 0; p < this.points.length; p++) { + this.points[p].updateBreathingPhysics(); + } + }, + + breathe: function(tick){ + var p, scale, newSize; + + this.breath += tick; + + if (this.breath < this.breathLength) { + return; + } + + if (this.breatheIn) { + scale = 1; + } else { + scale = 1.05; + } + + this.breatheIn = !this.breatheIn; + + newSize = Vector.mult(this.sizeRef, scale); + + for (p = 0; p < this.points.length; p++) { + this.points[p].updateTarget(newSize); + } + + this.breath = 0; + }, + update: function(engine){ var p; + if (this.breathing === true) { + this.breathe(engine.tick); + } + for (p = 0; p < this.points.length; p++) { this.points[p].update(engine); } diff --git a/website/source/javascripts/app/Engine.Typewriter.js b/website/source/javascripts/app/Engine.Typewriter.js index 17dcf7b0d..9629a822a 100644 --- a/website/source/javascripts/app/Engine.Typewriter.js +++ b/website/source/javascripts/app/Engine.Typewriter.js @@ -6,8 +6,6 @@ Engine.Typewriter = function(element){ this.element = element; this.content = this.element.textContent.split(''); this.element.innerHTML = ''; - - console.dir(this); }; Engine.Typewriter.prototype = { diff --git a/website/source/javascripts/app/Engine.js b/website/source/javascripts/app/Engine.js index be6661693..96710766d 100644 --- a/website/source/javascripts/app/Engine.js +++ b/website/source/javascripts/app/Engine.js @@ -97,6 +97,7 @@ Engine = Base.extend({ .wait(1000) .then(function(){ this.showGrid = true; + this.logo.startBreathing(); }, this) .wait(1000) .then(function(){