-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.html
60 lines (56 loc) · 1.37 KB
/
vector.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
<!DOCTYPE html>
<html lang="kr">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
width: 100vw;
height: 100vh;
margin: 0;
}
.line {
border: 2px solid black;
position: absolute;
height: 0;
}
</style>
</head>
<body>
<script>
let x, y;
let lineIdx = 0;
let isDown = false;
onmousedown = (e) => {
isDown = true;
function moveAt(e) {
if(!isDown) return;
const line = document.getElementById(`line${lineIdx}`);
let dy = e.y-y;
let dx = e.x-x;
angle = Math.atan(dy/dx) * (180/Math.PI);
if(dx < 0.0) angle += 180;
else if(dy<0.0) angle += 360;
line.style.width = Math.sqrt((dx**2) + (dy**2)) + 'px';
line.style.transform = `rotate(${angle}deg)`;
line.style.transformOrigin = '0 0';
}
x = e.x;
y = e.y;
const line = document.createElement("div");
line.id = `line${lineIdx}`;
line.className = 'line';
line.style.top = `${y}px`;
line.style.left = `${x}px`;
document.body.append(line);
addEventListener("mousemove", moveAt);
}
onmouseup = () => {
isDown = false;
lineIdx++;
}
</script>
</body>
</html>