Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Day 29 - user's birds slowly fade away

  • Loading branch information...
commit 7f0c388bf2f55a8ea4c426a1ba0ef6d430b2ff4b 1 parent b103fe3
Florian Cargoët authored

Showing 1 changed file with 26 additions and 5 deletions. Show diff stats Hide diff stats

  1. +26 5 365.js
31 365.js
@@ -477,6 +477,7 @@ Canvas365.registerDay('365', function(){
477 477 this.y = config.y;
478 478 this.t = 0;
479 479 config.scale = config.scale || 1;
  480 + this.scale = config.scale;
480 481 config.color = config.color || [0, 0, 0];
481 482 config.opacity = config.opacity || 1; // can't be 0
482 483
@@ -506,7 +507,7 @@ Canvas365.registerDay('365', function(){
506 507 ctx.fillStyle = 'rgba(' + config.color.join(',') + ',' + config.opacity + ')';
507 508
508 509 ctx.translate(this.x, this.y);
509   - ctx.scale(config.scale, config.scale);
  510 + ctx.scale(this.scale, this.scale);
510 511
511 512 ctx.beginPath();
512 513 ctx.moveTo(0, 0);
@@ -523,8 +524,16 @@ Canvas365.registerDay('365', function(){
523 524 };
524 525
525 526 Bird.prototype.update = function(){
526   - this.t += 0.1;
527   - this.m = 7 * (1 + Math.sin(this.t));
  527 + this.t++;
  528 + this.m = 7 * (1 + Math.sin(this.t/10));
  529 +
  530 + if(this.config.ttl){
  531 + var dt = this.config.ttl - this.t;
  532 + this.scale = this.config.scale * (dt/this.config.ttl);
  533 + if(dt < 0){
  534 + this.toRemove = true;
  535 + }
  536 + }
528 537 };
529 538
530 539 var ModeSelector = function(ctx, config){
@@ -834,9 +843,9 @@ Canvas365.registerDay('365', function(){
834 843 return new Bird(ctx, {
835 844 x : -100,
836 845 y : -100,
837   - color : [Util.rand(255), Util.rand(255), Util.rand(255)],
  846 + color : [Util.rand(255), Util.rand(255), Util.rand(255)],
838 847 opacity : 0.5,
839   - scale : 0.5 + Math.random()
  848 + scale : 0.5 + Math.random()
840 849 });
841 850 }
842 851
@@ -872,7 +881,9 @@ Canvas365.registerDay('365', function(){
872 881 // add bird
873 882 if(y < beach.config.yWater){
874 883 if(ModeSelector.mode === 'add'){
  884 + transparentBird.t = 0; // reset lifetime
875 885 transparentBird.config.opacity = 1;
  886 + transparentBird.config.ttl = 500;
876 887 transparentBird.register();
877 888 drawList.push(transparentBird);
878 889
@@ -937,9 +948,19 @@ Canvas365.registerDay('365', function(){
937 948 main : function(ctx){
938 949 ctx.fillStyle = '#0582C2'; // sky
939 950 ctx.fillRect(0, 0, 500, 500);
  951 + var itemsToRm = [];
940 952 drawList.forEach(function(item, i){
941 953 item.update();
942 954 item.draw();
  955 + if(item.toRemove){
  956 + itemsToRm.push(item);
  957 + }
  958 + });
  959 + // remove dead items
  960 + itemsToRm.forEach(function(item){
  961 + var i = drawList.indexOf(item);
  962 + drawList.splice(i, 1);
  963 + BBoxRegistry.remove(item);
943 964 });
944 965
945 966 if(ModeSelector.mode === 'edit'){

0 comments on commit 7f0c388

Please sign in to comment.
Something went wrong with that request. Please try again.