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
|
var draw = (function(){
var last_point = [0,0]
function down (e, lex, point) {
erasing = (e.which == "3" || e.ctrlKey)
if (e.shiftKey) {
line (lex, last_point, point, erasing)
}
else {
stamp (canvas, brush, point[0], point[1], erasing)
}
last_point[0] = point[0]
last_point[1] = point[1]
}
function move (e, lex, point) {
line(lex, last_point, point, erasing)
last_point[0] = point[0]
last_point[1] = point[1]
}
function point (lex, x, y, erasing) {
stamp (canvas, brush, x, y, erasing)
}
function line (lex, a, b, erasing) {
var len = dist(a[0], a[1], b[0], b[1])
var bw = 1
var x, y, i;
for (var i = 0; i < len; i += bw) {
x = lerp(i / len, a[0], b[0])
y = lerp(i / len, a[1], b[1])
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 = round( s + x-hh )
t = round( t + y-hh )
if (lex.opacity > 0 && s >= 0 && s < canvas.w && t >= 0 && t < canvas.h) {
if (erasing) {
canvas.aa[t][s].erase(lex)
}
else {
canvas.aa[t][s].clone(lex)
}
}
})
}
var draw = {}
draw.down = down
draw.move = move
draw.stamp = stamp
draw.line = line
draw.point = point
return draw
})()
|