summaryrefslogtreecommitdiff
path: root/web/main_var.js
blob: eb044490840c3e40787f986777bd8e3002e8f965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var ReReReRe = function() {
	$("#nojs").remove();
	var res = [window.innerWidth, window.innerHeight];
	var canvas = $("<canvas />").attr("width", res[0]).attr("height", res[1]).css("position", "absolute").css("width", "100%").css("height", "100%").appendTo($("body"));
	var context = canvas[0].getContext("2d");
	var sources = [];
	var actives = [];
	var supportedGCO = (function getGCOModes() {
		var ctx = document.createElement('canvas').getContext('2d');
  		//var gCO = ["source-over", "source-in", "source-out", "source-atop", "destination-over", "destination-in", "destination-out", "destination-atop", "lighter", "copy", "xor", "multiply", "screen", "overlay", "darken", "color-dodge", "color-burn", "hard-light", "soft-light", "difference", "exclusion", "hue", "saturation", "color", "luminosity"];
  		var gCO = ["destination-atop", "lighter", "copy", "xor", "multiply", "screen", "overlay", "darken", "color-dodge", "color-burn", "hard-light", "soft-light", "difference", "exclusion", "hue", "saturation", "color", "luminosity"];
		return gCO.filter(function(g) {
    		ctx.globalCompositeOperation = g;
    		return ctx.globalCompositeOperation === g;
  		});
	})();

	var enabledGCO = {};
	supportedGCO.forEach(function(val) {
		var item = $("<label />");
		var input = $("<input />", {id: "blend"+val}).attr("type", "checkbox").attr("checked", "checked");
		input.click(function(){
			enabledGCO[val] = $(this).prop("checked");
		});
		item.append(input);
		item.append(val);
		enabledGCO[val] = true;
		$("#blendtypes").append(item).append("<br />");
	});

	function randomBoolean() {
		return (Math.random() > 0.5);
	}

	function randomInt(max) {
		return Math.floor(Math.random()*max);
	}

	function randomPosition(wo, ho) {
		if (wo && ho) {
			xmax = res[0] - wo;
			ymax = res[1] - ho;
		} else {
			xmax = res[0];
			ymax = res[1];
		}
		return [Math.floor(xmax*Math.random()), Math.floor(ymax*Math.random())];
	}

	function randomSource() {
		return sources[Math.floor((sources.length - 1)*Math.random())]
	}

	function randomGCO() {
		var gcos = [];
		for (var k in enabledGCO) {
			if (enabledGCO[k]) gcos.push(k);
		}

		return gcos[Math.floor((gcos.length - 1)*Math.random())];
	}

	var Source = function() {
		var self = this;
		var src = randomSource();
		var staging = null;
		var scontext = null;
		var size = null;
		var blitpos = null;
		var gcotype = null;
		this.ready = false;
		var refreshing = false;

	
		function refresh() {
			refreshing = true;
			var img = new Image();
			img.src = src;
			img.onload = function() {
				if (!size) size = [this.width, this.height];
				if (!staging) staging = $("<canvas />").attr("width", size[0]).attr("height", size[1]);
				if (!scontext) scontext = staging[0].getContext("2d");
				scontext.clearRect(0, 0, size[0], size[1]);
				scontext.drawImage(img, 0, 0, size[0], size[1]);
				self.ready = true;
				this.onload = null;
				refreshing = false;
			}
			setTimeout(function(){
				delete img;
			}, 90);
		}

		this.blit = function() {
			if (!staging) return;
			if (!gcotype) gcotype = randomGCO(); //context.globalCompositeOperation = randomGCO();
			context.globalCompositeOperation = gcotype;
			//context.globalAlpha = (Math.random()/2) + 0.5;
			if (!blitpos) blitpos = randomPosition(size[0], size[1]);
			context.drawImage(staging[0], blitpos[0], blitpos[1], size[0], size[1]);

			self.ready = false;
			if (!refreshing) refresh();
		};

		refresh();
	};


	function step() {
		var anyready = false;
		actives.forEach(function(a){
			if (a.ready) {
				anyready = true;
			}
		});	
		if (anyready) {
			context.clearRect(0, 0, res[0], res[1]);
			actives.forEach(function(a){
				a.blit();
			});
		}
		setTimeout(step, 110);
	}

	function start() {
		for (var x = 0; x < randomInt(5)+2; x++) {
			actives.push(new Source());
		}
		step();
	}
	
	$.ajax({
		dataType: "json",
		url: "sources.json",
		success: function(response) {
			sources = response.data;
			start();
		}
	});

};





$(function(){
	window.rererere = new ReReReRe();
});