summaryrefslogtreecommitdiff
path: root/js/shader.js
blob: d203abe0c8ea7d9b59b20040a99f045a3930ec08 (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
var shader = (function(){
	var fn_str, fn, lex
	var exports = {}
	var animating = false
	
	exports.init = function(){
		lex = new Lex (0, 0)
		exports.build(demo_shader.innerHTML)
	}
	exports.build = function (fn_str){
		new_fn = new Function('lex', 'x', 'y', 'w', 'h', 't', fn_str)
		try {
			new_fn(lex, 0, 0, 1, 1, 0)
		}
		catch (e) {
			throw 'Shader execution error'
		}
		exports.fn = fn = new_fn
		return fn
	}
	exports.run = function(canvas){
		var t = +new Date, w = canvas.w, h = canvas.h
		canvas.forEach(function(lex, x, y){
			fn(lex, x, y, w, h, t)
			lex.build()
		})
	}
	exports.toggle = function(state){
		animating = typeof state == "boolean" ? state : ! animating
		return animating
	}
	exports.pause = function(){
		animating = false
	}
	exports.play = function(){
		animating = true
	}
	exports.animate = function (t){
		requestAnimationFrame(exports.animate)
		if (! animating) { return }
		exports.run(canvas)
	}
	
	return exports

})()