-
Notifications
You must be signed in to change notification settings - Fork 107
/
drawing.py
50 lines (40 loc) · 1.21 KB
/
drawing.py
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
drawing = {'num': 0}
with open('canvas.html', encoding='utf8') as template_file:
template = template_file.read()
def start():
drawing['num'] += 1
canvas_id = 'state_canvas%d' % drawing['num']
drawing['code'] = template.replace(r'%id', canvas_id) \
.replace(r'%width', '600') \
.replace(r'%height', '600')
def end():
return drawing['code'].replace(r'%drawing', '')
def add(code):
drawing['code'] = drawing['code'].replace(r'%drawing', code + r'%drawing')
# User methods
def ink(r, g, b):
add('''
c$.fillStyle = 'rgb(%d, %d, %d)';
c$.strokeStyle = 'rgb(%d, %d, %d)';''' % (r, g, b, r, g, b))
def line(x1, y1, x2, y2):
add('''
c$.beginPath();
c$.moveTo(%d, %d);
c$.lineTo(%d, %d);
c$.closePath();
c$.stroke();''' % (x1, y1, x2, y2))
def rect(x, y, w, h):
add('''
c$.fillRect(%d, %d, %d, %d);''' % (x, y, w, h))
def circ(x, y, r):
add('''
c$.beginPath();
c$.arc(%d, %d, %d, 0, 2 * Math.PI);
c$.fill();
c$.stroke();''' % (x, y, r))
def font(size):
add('''
c$.font = '%dpx Arial';''' % size)
def text(s, x, y):
add('''
c$.fillText(%s, %d, %d);''' % (repr(s), x, y))