-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle_drawer.html
More file actions
136 lines (124 loc) · 3.38 KB
/
circle_drawer.html
File metadata and controls
136 lines (124 loc) · 3.38 KB
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
<!DOCTYPE html>
<button id="undo" onmousedown="undo()">Undo</button>
<button id="redo" onmousedown="redo()">Redo</button>
<br />
<canvas id="canvas"
width="480"
height="320"
style="border: 1px solid #000;"
onmousedown="selectCreate(event)"
oncontextmenu="return false"
></canvas>
<dialog id="diameter-dialog">
<label for="diameter">
Adjust diameter of circle at (<span id="coord"></span>).<br />
<input type="range" id="diameter" min="10" max="400" value="20" oninput="updateDiameter()" />
</label>
</dialog>
<script>
const diameterDialog = document.getElementById('diameter-dialog');
const diameterInput = document.getElementById('diameter');
const diameterCoor = document.getElementById('coord');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let actionIndex = -1;
const actions = [];
function coorDistance(a, b) {
const xDiff = a.x - b.x;
const yDiff = a.y - b.y;
return Math.sqrt((xDiff*xDiff) + (yDiff*yDiff));
}
function getCircles() {
const circlesMap = new Map();
let selected = '';
for (let i = 0; i <= actionIndex; ++i) {
const action = actions[i];
const key = `${action.x}:${action.y}`;
const circle = {
x: action.x,
y: action.y,
d: action.d,
s: false
};
if (action.s) selected = key;
circlesMap.set(key, circle);
}
if (selected) {
circlesMap.get(selected).s = true;
}
return [...circlesMap.values()];
}
function getSelected() {
return getCircles().find(c => c.s);
}
function getHit(coor) {
let minDistance = Infinity;
let selected = null;
for (const circle of getCircles()) {
const distance = coorDistance(circle, coor);
if (distance < 0.5 * circle.d && distance < minDistance) {
minDistance = distance;
selected = circle;
}
}
return selected;
}
function draw() {
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, 480, 320);
ctx.fillStyle = '#aaa';
ctx.strokeStyle = '#000';
for (const circle of getCircles()) {
ctx.beginPath();
ctx.arc(circle.x, circle.y, 0.5 * circle.d, 0, 2*Math.PI);
ctx.stroke();
if (circle.s) ctx.fill();
}
}
function addAction(action) {
actions[++actionIndex] = action;
actions.splice(actionIndex + 1);
draw();
}
function updateAction(action, props) {
for (const [key, value] of Object.entries(props)) {
action[key] = value;
}
draw();
}
function updateDiameter() {
const selected = getSelected();
const action = actions[actionIndex];
const d = Number(diameterInput.value);
if (selected.x === action.x &&
selected.y === action.y) {
updateAction(action, {d});
} else {
addAction({x: selected.x, y: selected.y, d});
}
}
function selectCreate(event) {
diameterDialog.close();
const selected = getHit({x: event.offsetX, y: event.offsetY});
if (selected) {
addAction({x: selected.x, y: selected.y, d: selected.d, s: true});
}
if (event.button === 0 && !selected) {
addAction({x: event.offsetX, y: event.offsetY, d: 20, s: true});
} else if (event.button === 2 && selected) {
diameterCoor.innerText = `${selected.x}, ${selected.y}`;
diameterInput.value = selected.d;
diameterDialog.show();
}
}
function undo() {
diameterDialog.close();
actionIndex = Math.max(-1, actionIndex - 1);
draw();
}
function redo() {
diameterDialog.close();
actionIndex = Math.min(actionIndex + 1, actions.length - 1);
draw();
}
</script>