web/static/res/js/incmosaic.js
changeset 32 6b23f677b621
parent 30 6d5b46559bd9
child 36 6cd5bc3dc7a2
equal deleted inserted replaced
31:0e185eeea3e1 32:6b23f677b621
    19 	this.squareX;
    19 	this.squareX;
    20 	this.squareY;
    20 	this.squareY;
    21 	this.effects = [];
    21 	this.effects = [];
    22 	this.squareEffects = [];
    22 	this.squareEffects = [];
    23 
    23 
       
    24 	// Time
       
    25 	this.startTime;
       
    26 
    24 	this.noEffectinfo = new IncEffectInfo();
    27 	this.noEffectinfo = new IncEffectInfo();
    25 	this.noEffectinfo.color = new IncColor(255, 255, 255, 1)
    28 	this.noEffectinfo.color = new IncColor(255, 255, 255, 1)
    26 	this.noEffectinfo.alpha = 1;
    29 	this.noEffectinfo.alpha = 1;
    27 
    30 
    28 	// --------------------------------------------------------------------------------------------------------------------
    31 	// --------------------------------------------------------------------------------------------------------------------
    52 		// Set a random pair images
    55 		// Set a random pair images
    53 		this.setRandomPairImages();
    56 		this.setRandomPairImages();
    54 
    57 
    55 		// Set random effect on the squares
    58 		// Set random effect on the squares
    56 		this.setRandomSquareEffect();
    59 		this.setRandomSquareEffect();
       
    60 
       
    61 		// Time
       
    62 		this.startTime = new Date().getTime();
    57 										
    63 										
    58 		// Main loop
    64 		// Main loop
    59 		this.loopCallback();
    65 		this.loopCallback();
    60 	};
    66 	};
    61 
    67 
    70 
    76 
    71 	this.registerEffects = function()
    77 	this.registerEffects = function()
    72 	{
    78 	{
    73 		// No effect 
    79 		// No effect 
    74 		//-----------------------------------
    80 		//-----------------------------------
    75 		var noEffect = new IncSquareEffect_NoEffet()
    81 		//var noEffect = new IncSquareEffect_NoEffet()
    76 		var fullEffect0 = new IncFullEffect(0, 0, noEffect);
    82 		//var fullEffect0 = new IncFullEffect(0, 0, noEffect);
    77 		this.effects.push(fullEffect0);
    83 		//this.effects.push(fullEffect0);
    78 
    84 
    79 		// Alpha 
    85 		// Create semi random effects
    80 		//-----------------------------------
    86 		for (var i = 0; i < 20; ++i) {
    81 		{
    87 			var time1 = this.randomInt(3000, 10000);
    82 			var effectParam1 = new IncEffectParams(new IncColor(255, 255, 255, 0), new IncAnim(1, 5000), 1, new IncAnim(-1, 5000), 8000);
    88 			var time2 = this.randomInt(3000, 10000);
    83 			var effect1 = new IncSquareEffect_Alpha(effectParam1, createjs.Ease.linear, createjs.Ease.linear);
    89 			var c1 = this.randomInt(0, 128);
    84 			var effectParam2 = new IncEffectParams(new IncColor(255, 255, 255, 1), new IncAnim(0, 5000), 0, new IncAnim(1, 5000), 8000);
    90 
    85 			var effect2 = new IncSquareEffect_Alpha(effectParam2, createjs.Ease.linear, createjs.Ease.linear, 2500);
    91 			var effectParam1 = new IncEffectParams(new IncColor(c1, c1, c1, 0), new IncAnim(1, time1), 1, new IncAnim(-1, time1));
    86 			var fullEffect1 = new IncFullEffect(1, 1, effect2, effect1);
    92 			var effect1 = new IncSquareEffect_Alpha(effectParam1, createjs.Ease.quadOut, createjs.Ease.quadOut);
    87 			this.effects.push(fullEffect1);
    93 			
    88 		}
    94 			var effectParam2 = new IncEffectParams(new IncColor(c1, c1, c1, 1), new IncAnim(0, time2), 0, new IncAnim(1, time2));
    89 
    95 			var effect2 = new IncSquareEffect_Alpha(effectParam2, createjs.Ease.quadIn, createjs.Ease.quadIn, time1 / 2.5);
    90 		// Alpha 2
    96 
    91 		//-----------------------------------
    97 			var fullEffect1 = new IncFullEffect(effect1, effect2, time1, time2);
    92 		{
       
    93 			var effectParam1 = new IncEffectParams(new IncColor(32, 32, 32, 0), new IncAnim(1, 5000), 1, new IncAnim(-1, 5000), 8000);
       
    94 			var effect1 = new IncSquareEffect_Alpha(effectParam1, createjs.Ease.linear, createjs.Ease.linear);
       
    95 			var effectParam2 = new IncEffectParams(new IncColor(32, 32, 32, 1), new IncAnim(0, 5000), 0, new IncAnim(1, 5000), 8000);
       
    96 			var effect2 = new IncSquareEffect_Alpha(effectParam2, createjs.Ease.linear, createjs.Ease.linear, 2500);
       
    97 			var fullEffect1 = new IncFullEffect(1, 1, effect2, effect1);
       
    98 			this.effects.push(fullEffect1);
    98 			this.effects.push(fullEffect1);
    99 		}
    99 		}
   100 	}
   100 	}
   101 
   101 
   102 	this.setRandomPairImages = function()
   102 	this.setRandomPairImages = function()
   128 			// Images are not loaded yet
   128 			// Images are not loaded yet
   129 			requestAnimationFrame(self.loopCallback);
   129 			requestAnimationFrame(self.loopCallback);
   130 			return;
   130 			return;
   131 		}
   131 		}
   132 
   132 
   133 		// Clear canvas
       
   134 		self.ctx.fillStyle = "#ffffff";
       
   135 		self.ctx.fillStyle = "#000000";
       
   136 		self.ctx.fillRect(0, 0, self.canvas.width, self.canvas.height);	
       
   137 
       
   138 		// Get time
   133 		// Get time
   139 		var time = new Date().getTime();
   134 		var time = new Date().getTime();
   140 
   135 
   141 		// Update effect
   136 		if (time < self.startTime + 3000) {
       
   137 			// Draw the first image
       
   138 			self.ctx.drawImage(self.pairImages[0], 0, 0);
       
   139 			requestAnimationFrame(self.loopCallback);
       
   140 			return;
       
   141 		}
       
   142 
       
   143 		// Clear canvas
       
   144 		//self.ctx.fillStyle = "#000000";
       
   145 		//self.ctx.fillRect(0, 0, self.canvas.width, self.canvas.height);	
       
   146 
       
   147 		// Update effects
   142 		for (var i = 0; i < self.squareEffects.length; ++i) {
   148 		for (var i = 0; i < self.squareEffects.length; ++i) {
       
   149 			
       
   150 			// Update
   143 			var fullEffect = self.squareEffects[i];
   151 			var fullEffect = self.squareEffects[i];
       
   152 			fullEffect.update(time);
   144 
   153 
   145 			for (var j = 0; j < 2; ++j) {
   154 			for (var j = 0; j < 2; ++j) {
   146 				var effect = fullEffect.effects[j];
   155 				var effectInfo = fullEffect.effectInfos[j];
   147 				if (effect !== undefined) {
   156 				if (effectInfo !== null) {
   148 					// Draw square
   157 					// Draw square
   149 					self.drawSquare(fullEffect, self.pairImages[j], effect, time);									
   158 					self.drawSquare(fullEffect, self.pairImages[j], effectInfo);	
   150 				}
   159 				}
   151 			}
   160 			}
   152 		}
   161 		}
   153 
   162 
   154 		// Loop
   163 		// Loop
   155 		requestAnimationFrame(self.loopCallback);
   164 		requestAnimationFrame(self.loopCallback);
   156 	};
   165 	};
   157 
   166 
   158 
   167 
   159 	this.drawSquare = function(fullEffect, image, effect, time)
   168 	this.drawSquare = function(fullEffect, image, effectInfo)
   160 	{
   169 	{
   161 		// Update effect
       
   162 		var effectInfo = effect.update(fullEffect, time);
       
   163 
       
   164 		if (effectInfo === null) {
       
   165 			return;
       
   166 		}
       
   167 
       
   168 		// Compute square position
   170 		// Compute square position
   169 		var posX = fullEffect.x * this.squareX;
   171 		var posX = fullEffect.x * this.squareX;
   170 		var posY = fullEffect.y * this.squareY;
   172 		var posY = fullEffect.y * this.squareY;
   171 
   173 
   172 		this.ctx.save();
   174 		this.ctx.save();
   242 {
   244 {
   243 	this.color;
   245 	this.color;
   244 	this.alpha;
   246 	this.alpha;
   245 }
   247 }
   246 
   248 
   247 function IncEffectParams(color, colorAnim, alpha, alphaAnim, time)
   249 function IncEffectParams(color, colorAnim, alpha, alphaAnim)
   248 {
   250 {
   249 	// Color
   251 	// Color
   250 	this.color = color;
   252 	this.color = color;
   251 	this.colorAnim = colorAnim;
   253 	this.colorAnim = colorAnim;
   252 
   254 
   253 	// Alpha
   255 	// Alpha
   254 	this.alpha = alpha;
   256 	this.alpha = alpha;
   255 	this.alphaAnim = alphaAnim;
   257 	this.alphaAnim = alphaAnim;
   256 
   258 
   257 	// Time
       
   258 	this.time = time;
       
   259 
       
   260 	this.computeColorAnimValue = function(elapsedTime, easeFunc)
   259 	this.computeColorAnimValue = function(elapsedTime, easeFunc)
   261 	{
   260 	{
   262 		return this.computeAnimValue(this.colorAnim, elapsedTime, easeFunc);
   261 		return this.computeAnimValue(this.colorAnim, elapsedTime, easeFunc);
   263 	};
   262 	};
   264 
   263 
   277 	};	
   276 	};	
   278 }
   277 }
   279 
   278 
   280 function IncSquareEffect_NoEffet()
   279 function IncSquareEffect_NoEffet()
   281 {
   280 {
   282 	this.update = function(fullEffect, time)
   281 	this.update = function(time)
   283 	{
   282 	{
   284 		return incMosaic.noEffectinfo;
   283 		return incMosaic.noEffectinfo;
   285 	}
   284 	}
   286 }
   285 }
   287 
   286 
   293 	// Tween functions
   292 	// Tween functions
   294 	this.tweenColorFunc = tweenColorFunc;
   293 	this.tweenColorFunc = tweenColorFunc;
   295 	this.tweenAlphaFunc = tweenAlphaFunc;
   294 	this.tweenAlphaFunc = tweenAlphaFunc;
   296 
   295 
   297 	// Time
   296 	// Time
   298 	this.waitTime = (waitTime!==undefined) ? (new Date().getTime() + waitTime) : 0; 
   297 	this.waitTime = (waitTime!==undefined) ? waitTime : 0; 
   299 
   298 
   300 	this.update = function(fullEffect, time)
   299 	this.update = function(elapsedTime)
   301 	{
   300 	{
   302 		if (this.waitTime > time) {
       
   303 			return null;
       
   304 		}
       
   305 
       
   306 		if (fullEffect.startTime == 0) {
       
   307 			fullEffect.startTime = time;			
       
   308 		}
       
   309 
       
   310 		var elapsedTime = time - fullEffect.startTime;		
       
   311 		var info = new IncEffectInfo();
   301 		var info = new IncEffectInfo();
   312 
   302 
   313 		// Compute new color
   303 		// Compute new color
   314 		var newColorValue = this.effectParms.computeColorAnimValue(elapsedTime, this.tweenColorFunc);
   304 		var newColorValue = this.effectParms.computeColorAnimValue(elapsedTime, this.tweenColorFunc);
   315 		info.color = this.effectParms.color.copy();
   305 		info.color = this.effectParms.color.copy();
   322 
   312 
   323 		return info;
   313 		return info;
   324 	};
   314 	};
   325 }
   315 }
   326 
   316 
   327 function IncFullEffect(x, y, effect1, effect2)
   317 function IncFullEffect(effect1, effect2, totalTime1, totalTime2)
   328 {
   318 {
   329 	// Position
   319 	// Position
   330 	this.x = x;
   320 	this.x = 0;
   331 	this.y = y;
   321 	this.y = 0;
   332 
   322 
   333 	// Effect	
   323 	// Effect	
   334 	this.effects = [effect1, effect2];
   324 	this.effects = [effect1, effect2];
       
   325 	this.effectInfos = [null, null];
   335 
   326 
   336 	// Time
   327 	// Time
   337 	this.startTime = 0;	
   328 	this.startTime = [0, 0];	
       
   329 	this.totalTime = [totalTime1, totalTime2];	
   338 
   330 
   339 	this.copy = function()
   331 	this.copy = function()
   340 	{
   332 	{
   341 		return new IncFullEffect(this.x, this.y, this.effects[0], this.effects.length > 1 ? this.effects[1] : undefined);
   333 		return new IncFullEffect(this.effects[0], this.effects[1], this.totalTime[0], this.totalTime[1]);
       
   334 	};
       
   335 
       
   336 	this.update = function(time)
       
   337 	{
       
   338 		if (this.startTime[0] == 0) {
       
   339 			this.startTime[0] = time;
       
   340 			this.startTime[1] = time;
       
   341 			return;
       
   342 		}
       
   343 
       
   344 		for (var i = 0; i < 2; ++i) {
       
   345 			// If we are in the good time range we update the effect
       
   346 			var waitTime = this.startTime[i] + this.effects[i].waitTime;
       
   347 			if (time > waitTime && time < this.totalTime[i] + waitTime) {
       
   348 				this.effectInfos[i] = this.effects[i].update(time - waitTime);
       
   349 			}
       
   350 		}
   342 	};	
   351 	};	
   343 }
   352 }
   344 
   353 
   345 // --------------------------------------------------------------------------------------------------------------------
   354 // --------------------------------------------------------------------------------------------------------------------
   346 // Tools
   355 // Tools