-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinteraction.js
59 lines (48 loc) · 1.18 KB
/
interaction.js
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
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var mouseDown = false;
var mouseX = 0;
var mouseY = 0;
var key = 0;
function handleMouse(event) {
mouseX = event.offsetX;
mouseY = event.offsetY;
if (event.type == 'mousedown') {
mouseDown = true;
} else if (event.type == 'mouseup') {
mouseDown = false;
}
redraw();
}
function handleKey(event) {
if (event.type == 'keydown') {
key = event.which;
}
// Disable navigation (e.g. delete key).
event.preventDefault();
redraw();
}
function redraw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawCursor();
drawLabel();
}
var cursorStartAngle = Math.PI;
var cursorEndAngle = Math.PI * 1.75;
function drawCursor() {
var radius = mouseDown ? 15 : 10;
ctx.beginPath();
ctx.arc(mouseX, mouseY, radius, cursorStartAngle, cursorEndAngle)
ctx.closePath();
ctx.fillStyle = 'white';
ctx.fill();
}
function drawLabel() {
var labelTypeface = 'Menlo';
var labelSize = 14;
var labelOffset = 3;
var text = mouseX+","+mouseY+" -- "+key+" "+String.fromCharCode(key);
ctx.font = labelSize+'pt '+labelTypeface;
ctx.fillStyle = 'white';
ctx.fillText(text, labelOffset, labelOffset + labelSize);
}