summaryrefslogtreecommitdiff
path: root/index.html
blob: 1e4485430d97ed98b259b06290b9f9cfcc07a95a (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<link rel="stylesheet" href="css/sally.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="css/ak.css" type="text/css" charset="utf-8" />
<style type="text/css">
#rapper { white-space: pre; font-family: Menlo, monospace; cursor: crosshair; }
/* 
body.grid span { border-right: 1px solid #444; border-top: 1px solid #444; border-bottom: 1px solid #444; }
 */
span.selected { border-bottom: #fff; }
</style>
<body class="grid">

<div id="canvas_rapper" class="rapper"></div>
<div id="brush_rapper" class="rapper"></div>
<div id="palette_rapper" class="rapper"></div>

<script>
var contentType = 'text/plain;charset=utf-8'

var cols = 80
var rows = 24
var dragging = false
var erasing = false

var canvas, tools, palette, brush, colors, mode

var color_names = ("white black dark-blue green red dark-red purple orange" +
                   "yellow lime dark-cyan cyan blue magenta dark-gray light-gray").split(" ");
var letters = "abcdefghijklmnop";
var colors = {}
color_names.forEach(function(name, i){ colors[name] = i })

function init () {
  build()
  bind()
}
function build () {
	canvas = new Matrix (rows, cols, function(x,y){
		var lex = new Lex (x,y)
		if (x > y || y > x + 20 || x > y / 4 + 10) {
			lex.clear()
		}
		else {
			lex.bg = x+y*y
			lex.fg = (x+y)%2
			lex.char = ":"
		}
		lex.build()
		return lex
	})
	brush = new Matrix (5, 5, function(x,y){
		var lex = new Lex (x,y)
		lex.build()
		return lex
	})
	palette = new Matrix (2, 32, function(x,y){
		var lex = new Lex (x,y)
		lex.bg = y>>1
		lex.build()
		return lex
	})
	canvas.build(canvas_rapper)
	brush.build(brush_rapper)
	palette.build(palette_rapper)
}
function bind () {
  canvas.forEach(function(lex, x, y){
  	lex.span.addEventListener('mousedown', function(e){
      e.preventDefault()
      dragging = true
      erasing = e.which == "3"
			draw(lex, x, y, erasing)
    })
    lex.span.addEventListener("mousemove", function(){
      dragging && draw(lex, x, y, erasing)
    })
  })
  palette.forEach(function(lex, x, y){
  	lex.span.addEventListener('mousedown', function(e){
      e.preventDefault()
      dragging = true
      erasing = e.which == "3"
			brush.fill(lex.fg, lex.bg)
    })
	})
  window.addEventListener('mouseup', function(){
    dragging = erasing = false
  })
}

function Lex (x,y) {
	this.x = x
	this.y = y
	this.span = document.createElement("span")
	this.fg = colors.white
	this.bg = colors.black
	this.char = " "
}
Lex.prototype.build = function(){
	this.span.className = this.css()
	this.span.innerHTML = this.html()
}
Lex.prototype.css = function(){
	return "f" + letters[this.fg&15] + "b" + letters[this.bg&15]
}
Lex.prototype.html = function(){
	return this.char == " " ? "&nbsp;" : this.char
}
Lex.prototype.irc = function(){
	if (this.bg == 1 && this.fg == 0) {
// 		return "\\x03" + "," + (this.bg&15) + this.char
		return this.char
	}
	else {
		return "\\x03" + (this.fg&15) + "," + (this.bg&15) + this.char
	}
}
Lex.prototype.clone = function (lex){
	this.fg = lex.fg
	this.bg = lex.bg
	this.char = lex.char
	this.build()
}
Lex.prototype.eq = function(lex){
	return lex && this.fg == lex.fg && this.bg == lex.fg && this.char == lex.char
}
Lex.prototype.clear = function(){
	this.bg=1
	this.fg=0
	this.char = " "
}

function Matrix (w,h,f){
	this.w = w, this.h = h
	var aa = new Array (w)
	for (var i = 0; i < w; i++) {
		aa[i] = new Array (h)
		for (var j = 0; j < h; j++) {
			aa[i][j] = f(i,j)
		}
	}
	this.aa = h == 1 ? aa[0] : aa
}
Matrix.prototype.forEach = function(f){
	this.aa.forEach(function(row, y){
		row.forEach(function(lex, x){
			f(lex, x, y)
		})
	})
}
Matrix.prototype.clear = function(){
	this.forEach(function(lex,x,y){ lex.clear() })
}
Matrix.prototype.fill = function(fg,bg){
	this.forEach(function(lex,x,y){
		lex.fg = fg
		lex.bg = bg
		lex.build()
	})
}
Matrix.prototype.build = function(rapper){
  this.aa.forEach(function(row, y){
  	var div = document.createElement("div")
  	row.forEach(function(lex, x) {
			div.appendChild(lex.span)
		})
		rapper.appendChild( div )
	})
//   rapper.appendChild(frag)
}

function draw (lex, x, y, erasing) {
	stamp (canvas, brush, x, y, erasing)
}
function stamp (canvas, brush, x, y, erasing) {
	hh = brush.w/2|0
	brush.forEach(function(lex, s, t){
		s += x-hh
		t += y-hh
		if (s >= 0 || s < canvas.w || t >= 0 && t < canvas.h) {
			canvas.aa[t][s].clone(lex)
		}
	})
}

function ascii () {
  var lines = canvas.aa.map(function(row, y){
    var last, line = ""
    row.forEach(function(lex, x) {
			if (lex.eq(last)) {
				line += lex.char
			}
			else {
				if (x > 0 && last && (last.bg != 1 || last.fg != 0)) line += "\\x03"
				line += lex.irc()
				last = lex
			}
		})
    return line.replace(/\s+$/,"")
	}).filter(function(line){ return line.length > 0 })
  var txt = '/exec -out printf "' + lines.join("\\n") + '"\n'
  return txt
}

document.body.addEventListener('copy', function (e) {
  if (e.clipboardData) {
    e.preventDefault();
    e.clipboardData.setData(contentType, ascii());
  }
  if (window.clipboardData) {
    e.returnValue = false;
    window.clipboardData.setData(contentType, ascii());
  }
}, false);

window.cssRule = function (selector, declaration) {
  var x = document.styleSheets,y=x.length-1;
  x[y].insertRule(selector+"{"+declaration+"}",x[y].cssRules.length);
};

init()
</script>