web/static/res/js/incmosaic.js
changeset 39 6605de5fe0bd
parent 38 cdbb56b876c9
child 42 01415303372e
--- a/web/static/res/js/incmosaic.js	Mon Dec 10 20:55:18 2012 +0100
+++ b/web/static/res/js/incmosaic.js	Mon Dec 10 22:19:35 2012 +0100
@@ -11,7 +11,8 @@
 	this.imageData;
 
 	/// Images
-	this.pairUrl = [];
+	this.imageUrls = [];
+	this.listeUrls = [];	// The images list to blend
 	this.pairImages = [];
 	this.imagesLoaded = 0;
 	this.imageWidth;
@@ -34,10 +35,9 @@
 	// Functions
 	// --------------------------------------------------------------------------------------------------------------------
 
-	this.addPairImages = function(url1, url2)
+	this.addImageUrl = function(url)
 	{
-		var urls = new IncPairUrl(url1, url2);
-		this.pairUrl.push(urls);
+		this.imageUrls.push(url);
 	};
 
 	this.start = function(canvasId, squareCountX, squareCountY)
@@ -94,15 +94,44 @@
 		incMosaic.squareHeight = Math.floor(newCanvasHeight / incMosaic.squareCountY);
 	};
 
+	this.fillListUrl = function()
+	{
+		var urls = [];
+		for (var i = 1; i < this.imageUrls.length; ++i) {
+			urls.push(this.imageUrls[i]);
+		}
+
+		// Set random url
+
+		// The first images is always the first in this array 
+		this.listeUrls.push(this.imageUrls[0]);
+
+		while (urls.length > 0) {
+			var randInd = this.randomInt(0, urls.length);
+			this.listeUrls.push(urls[randInd]);
+			urls.remove(randInd);
+		}
+	};
+
 	this.setRandomPairImages = function()
 	{
-		this.imagesLoaded = 0;
-		var randInd = this.randomInt(0, this.pairUrl.length);
-		var pairUrl = this.pairUrl[randInd];
+		if (this.listeUrls.length == 0) {
+			this.fillListUrl();
+		}
 
-		this.pairImages.length = 0;
-		this.pairImages.push(this.getImageFromUrl(pairUrl.imageUrl1));
-		this.pairImages.push(this.getImageFromUrl(pairUrl.imageUrl2));
+		if (this.pairImages.length == 0) {
+			this.imagesLoaded = 0;
+			this.pairImages.push(this.getImageFromUrl(this.listeUrls[0]));
+			this.pairImages.push(this.getImageFromUrl(this.listeUrls[1]));
+			this.listeUrls.remove(0);
+		} else {
+			this.imagesLoaded = 1;
+			this.pairImages[0] = this.pairImages[1]; // Swap
+			this.pairImages[1] = this.getImageFromUrl(this.listeUrls[0])
+		}
+		this.listeUrls.remove(0);
+
+		// Todo preload the next image
 	};
 
 	this.setRandomSquareEffect = function()
@@ -288,12 +317,6 @@
 	};
 }
 
-function IncPairUrl(url1, url2)
-{
-	this.imageUrl1 = url1;
-	this.imageUrl2 = url2;
-}
-
 // --------------------------------------------------------------------------------------------------------------------
 // Effects
 // --------------------------------------------------------------------------------------------------------------------
@@ -443,5 +466,11 @@
 		};    
 })();
 
+Array.prototype.remove = function(from, to) {
+  var rest = this.slice((to || from) + 1 || this.length);
+  this.length = from < 0 ? this.length + from : from;
+  return this.push.apply(this, rest);
+};
+
 var incMosaic = new IncMosaic();