summaryrefslogtreecommitdiff
path: root/js/ui/brush.js
blob: a055a977bb539bb0c3ee09ea22740f525b3a7a5b (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
var brush = (function(){
	
	var brush = new Matrix (5, 5, function(x,y){
		var lex = new Lex (x,y)
		lex.build()
		return lex
	})

	brush.modified = false
	
	brush.bind = function(){
		brush.forEach(function(lex, x, y){
  		if (lex.bound) return
  		lex.bound = true

			lex.span.addEventListener('mousedown', function(e){
				e.preventDefault()
				dragging = true
	// 			lex.fill(lex.fg, lex.bg)
			})

		})
	}

  brush.bind = function(){

		var last_point = [0,0]
		var dragging = false

  	brush.forEach(function(lex, x, y){

  		if (lex.bound) return
  		lex.bound = true
  		
			var point = [x,y]
			lex.span.addEventListener('contextmenu', function(e){
				e.preventDefault()
			})
			lex.span.addEventListener('mousedown', function(e){
				e.preventDefault()
				current_canvas = brush
				brush.modified = true
				dragging = true
				erasing = (e.which == "3" || e.ctrlKey)
				if (erasing) {
					lex.clear()
				}
				else {
					lex.fill(brush.fg, brush.bg)
				}
				lex.focus()
			})
			lex.span.addEventListener('mousemove', function(e){
				e.preventDefault()
				if (! dragging) {
					return
				}
				erasing = (e.which == "3" || e.ctrlKey)
				if (erasing) {
					lex.clear()
				}
				else {
					lex.fill(brush.fg, brush.bg)
				}
				lex.focus()
			})
		})
		window.addEventListener("mouseup", function(){
			dragging = false
		})
	}

	brush.fg = 0
	brush.bg = 1
	
	return brush

})()