-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.html
215 lines (204 loc) · 5.99 KB
/
example.html
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
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf8">
<!-- <script src="build/texty.js"></script> -->
<script src="lib/emitter.js"></script>
<script src="lib/caret.js"></script>
<script src="lib/style.js"></script>
<script src="lib/texty.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>
o = $;
o(function(){
var canvas = o('canvas').get(0)
, ctx = canvas.getContext('2d')
, text = new Text
, prev
, start;
o(canvas).mousedown(function(e){
var now = new Date
, x = e.offsetX
, y = e.offsetY;
// double-click
if (prev && now - prev < 200) {
// TODO: triple-click for select all
text.selectWordAt(x, y).caret.hide();
} else {
start = e;
if (text.selected) text.deselect().caret.show();
if (text.visible) {
text.caret.moveTo(x, y);
} else {
text.show().moveTo(x, y);
}
}
prev = new Date;
});
o(document).mousemove(function(e){
if (!start) return;
text.select(start.offsetX, start.offsetY, e.offsetX, e.offsetY);
text.caret.hide();
});
o(document).mouseup(function(e){
start = null;
});
o(document).keydown(function(e){
var selection, n = 1;
text.caret.show();
switch (e.which) {
// newline
case 0x0d:
text.insert('\n');
break;
// V
case 0x56:
if (e.metaKey) {
e.preventDefault();
alert('TODO: implement paste');
}
break;
// C
case 0x43:
if (e.metaKey) {
e.preventDefault();
selection = text.selection();
alert('TODO: implement copy "' + selection.string + '"');
}
break;
// A
case 0x41:
if (e.metaKey) {
e.preventDefault();
text.selectAll().caret.hide();
}
break;
// -
case 0xbd:
if (e.metaKey) {
e.preventDefault();
text.size(text.size() - 5);
}
break;
// +
case 0xbb:
if (e.metaKey) {
e.preventDefault();
text.size(text.size() + 5);
}
break;
// backspace
case 0x08:
if (text.selected) {
text.removeSelection();
} else {
text.remove(1);
}
break;
// left
case 0x25:
// ctrl for soft / hard jumps
if (e.ctrlKey) n = text.caret.moveLeftSoft();
else if (e.altKey) n = text.caret.moveLeftHard();
else text.caret.moveLeft();
// selection
if (e.shiftKey) text.selectLeft(n).caret.hide();
else text.deselect();
break;
// up
case 0x26:
text.deselect().caret.moveUp();
break;
// right
case 0x27:
// ctrl for soft / hard jumps
if (e.ctrlKey) n = text.caret.moveRightSoft();
else if (e.altKey) n = text.caret.moveRightHard();
else text.caret.moveRight();
// selection
if (e.shiftKey) text.selectRight(n).caret.hide();
else text.deselect();
break;
// down
case 0x28:
text.deselect().caret.moveDown();
break;
// delete
case 0x2E:
if (text.selected) {
text.removeSelection();
} else {
text.removeAt(1, text.caret.pos + 1);
text.caret.moveRight().hide();
}
break;
}
});
o(document).keypress(function(e){
if (!text.visible) return;
if (e.ctrlKey) return;
if (text.selected) text.removeSelection();
text.insert(String.fromCharCode(e.which));
});
o('[name=size]').change(function(){
var n = parseInt(o(this).val(), 10);
text.size(n);
});
o('[name=css]').change(function(){
if (o(this).is(':checked')) {
text.inheritCSS();
} else {
text.clearCSS();
}
});
o('[name=bounds]').change(function(){
if (o(this).is(':checked')) {
text.showBounds = true;
} else {
text.showBounds = false;
}
});
var fps = 24;
setInterval(function(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
text.draw(ctx);
if (text.showBounds) text.drawBounds(ctx);
}, 1000 / fps);
});
</script>
<style>
* {
-webkit-user-select: none;
}
body {
font: 14px helvetica, arial, sans-serif;
padding: 20px;
}
.text {
font-size: 20px;
color: #888;
}
.text .line {
list-style: disc;
line-height: 1.5;
}
.text .caret {
color: red;
}
.text .selection {
background: rgba(255,0,0,0.1);
}
input {
margin-right: 15px;
}
</style>
</head>
<body>
<p>
Inherit CSS: <input type="checkbox" name="css" />
Show bounds: <input type="checkbox" name="bounds" />
Size: <input type="range" name="size" min="3" max="100" />
</p>
<canvas width="900" height="400" style="border: 1px solid #eee;">
</canvas>
</body>
</html>