-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphic-editor.html
79 lines (47 loc) · 1.78 KB
/
graphic-editor.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Online Graphic Editor</title>
<meta name="description" content="This is free online Graphic Editor. You can create basic shapes in this graphic editor and also move shapes.">
</head>
<body>
<div id ="container">
<h1>Draw Rectangles on Mouse Move in SVG</h1>
<svg id="my-svg" width="700" height="450" style="background-color: blue; margin-top:0px; margin-left:0px"></svg>
</div>
<script>
let svg = document.getElementById('my-svg');
let startX, startY;
let rectangle;
let rectangle2;
function handleMouseDown(event) {
rectangle2 = true;
startX = event.clientX
startY = event.clientY
rectangle = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
rectangle.setAttribute('stroke', 'black');
rectangle.setAttribute('fill', 'transparent');
svg.appendChild(rectangle);
}
function handleMouseMove(event) {
if (rectangle2 ===true) {
var rect = svg.getBoundingClientRect();
const width = Math.abs(event.clientX - startX);
const height = Math.abs(event.clientY - startY);
rectangle.setAttribute('x', Math.min(startX, event.clientX-rect.left));
rectangle.setAttribute('y', Math.min(startY, event.clientY-rect.top));
rectangle.setAttribute('width', width);
rectangle.setAttribute('height', height);
}
}
function handleMouseUp() {
rectangle2 = false;
}
svg.addEventListener('mousedown', handleMouseDown);
svg.addEventListener('mousemove', handleMouseMove);
svg.addEventListener('mouseup', handleMouseUp);
</script>
</body>
</html>