-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometry.html
More file actions
198 lines (130 loc) · 3.87 KB
/
Copy pathGeometry.html
File metadata and controls
198 lines (130 loc) · 3.87 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<style>
body{
margin: 0;
}
header{
padding: 15px 0;
border-bottom: solid white 2px;
}
.svg-debug{
stroke: rgb(95, 95, 95);
stroke-width: 2px;
}
.sidebar{
min-width: 250px;
border: solid white 2px;
padding-left: 5px;
}
</style>
</head>
<body>
<header>
<div class="flex">
<button id="add-point-button">Add point</button>
<p id="debug-current-action"></p>
</div>
</header>
<div class="flex">
<svg height="500" width="100%" id="main-svg">
<rect class="svg-debug" x="0" y="0" width="100%" height="100%" stroke-width="4px" fill="red" fill-opacity="0" />
<line class="svg-debug" x1="0%" y1="0%" x2="100%" y2="100%"/>
<!--Injected by script-->
</svg>
<div class="sidebar" id="sidebar">
<p id="sidebar-type">none</p>
<p id="sidebar-name">none</p>
<button id="add-line-button">Add line</button>
</div>
</div>
<script>
const main_svg = document.getElementById("main-svg")
const sidebar = document.getElementById('sidebar')
const sidebar_type = document.getElementById('sidebar-type')
const sidebar_name = document.getElementById('sidebar-name')
const svgns = "http://www.w3.org/2000/svg"
const mouse_position = {x:0,y:0}
let current_action = "none"
let selected_object
let drag_point_interval_id
setInterval(()=>{
document.getElementById("debug-current-action").textContent = current_action
})
main_svg.addEventListener("mousemove", (e)=>{
mouse_position.x = e.offsetX
mouse_position.y = e.offsetY
})
function get_point_position(point){
console.log(point.cx.baseVal.value, point.cy.baseVal.value)
return{x:point.cx.baseVal.value, y:point.cy.baseVal.value}
}
function drag_point(point){
point.cx.baseVal.value = mouse_position.x
point.cy.baseVal.value = mouse_position.y
}
function create_point(pos_x, pos_y, name){
const circle = document.createElementNS(svgns, 'circle')
circle.setAttributeNS(null, 'cx', pos_x)
circle.setAttributeNS(null, 'cy', pos_y)
circle.setAttributeNS(null, 'r', '8px')
circle.setAttributeNS(null, 'fill', 'rgb(29, 31, 37)')
circle.setAttributeNS(null, 'stroke-width', '4px')
circle.setAttributeNS(null, 'stroke', 'white')
circle.addEventListener("click", ()=>{
if(current_action === "add_line"){
const pos_1 = get_point_position(circle)
const pos_2 = get_point_position(selected_object)
main_svg.appendChild(create_line(pos_1.x, pos_1.y, pos_2.x, pos_2.y))
current_action = "none"
}
select(name, 'point', circle)
})
circle.addEventListener("mousedown", ()=>{
if(current_action === "none"){
current_action = "drag_point"
drag_point_interval_id = setInterval(drag_point, 10, circle)
}
})
circle.addEventListener("mouseup", ()=>{
if(current_action === "drag_point"){
current_action = "none"
clearInterval(drag_point_interval_id)
}
})
return circle
}
function create_line(x1, y1, x2, y2){
const line = document.createElementNS(svgns, "line")
line.setAttributeNS(null, "x1", x1)
line.setAttributeNS(null, "y1", y1)
line.setAttributeNS(null, "x2", x2)
line.setAttributeNS(null, "y2", y2)
line.setAttributeNS(null, 'stroke-width', '4px')
line.setAttributeNS(null, 'stroke', 'white')
return line
}
function select(name, type, element){
sidebar_name.textContent = name
sidebar_type.textContent = type
selected_object = element
console.log(selected_object)
}
document.getElementById("add-point-button").addEventListener("click", ()=>{
main_svg.appendChild(create_point(250, 250, "a"));
})
document.getElementById("add-line-button").addEventListener("click", ()=>{
const position = get_point_position(selected_object)
console.log(position.x, position.y)
if(current_action = "none"){
current_action = "add_line"
}
})
</script>
</body>
</html>