summaryrefslogtreecommitdiff
path: root/js/app.js
blob: 7bc392bf9df383893d96ceec338f99f92070ed79 (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
var dragging = false
var drawing = true
var erasing = false
var focused

var canvas, tools, palette, controls, brush, mode, current_tool

function init () {
  build()
  bind()
}
function build () {
	shader.init()
	shader.run(canvas)
	shader.animate()

	canvas.append(canvas_rapper)
	brush.append(brush_rapper)
	palette.append(palette_rapper)
	
  controls.circle.focus()
//  controls.shader.focus()

	brush.bg = colors.red
	brush.generate()
	brush.build()
}
function bind () {
	canvas.bind()
	palette.bind()
	brush.bind()
	controls.bind()
	
  window.addEventListener('mouseup', function(){
    dragging = erasing = false
		if (current_tool.name != 'shader') { cursor_input.focus() }
  });
  
	window.addEventListener('mousedown', function(e){
		if (current_tool.name == "shader") { return }
		cursor_input.focus()
	})
	cursor_input.addEventListener('keydown', function(e){
		if (focused) { focused.key(undefined, e.keyCode) }
	})
	cursor_input.addEventListener('input', function(e){
		if (current_tool.name == "shader") {
			cursor_input.value = ""
			return
		}
		if (! e.metaKey && ! e.ctrlKey && ! e.altKey) {
			e.preventDefault()
		}
		var char = cursor_input.value
		cursor_input.value = ""
		
		// var charFromKeyCode = String.fromCharCode(e.keyCode)

		switch (e.keyCode) {
			case 27: // esc
				if (focused) focused.blur()
				break
			case 219: // [
			  if (! focused && current_tool.name != "text") {
          brush.contract(1)
          break
        }
			case 221: // ]
			  if (! focused && current_tool.name != "text") {
          brush.expand(1)
          break
        }
			default:
				if (focused) focused.key(char, e.keyCode)
				break
		}
	})

	var contentType = 'text/plain;charset=utf-8'
	document.body.addEventListener('copy', function (e) {
		if (e.clipboardData) {
			e.preventDefault();
			e.clipboardData.setData(contentType, canvas.ascii());
		}
		if (window.clipboardData) {
			e.returnValue = false;
			window.clipboardData.setData(contentType, canvas.ascii());
		}
	}, false);

	document.addEventListener('DOMContentLoaded', function(){
		if (current_tool.name != 'shader') { cursor_input.focus() }
		document.body.classList.remove('loading')
	})
}


function int_key (f) {
	return function (key, keyCode) {
		var n = parseInt(key)
		! isNaN(n) && f(n)
	}
}

init()