forked from BasicPrimitives/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestShape.html
More file actions
175 lines (155 loc) · 7.59 KB
/
TestShape.html
File metadata and controls
175 lines (155 loc) · 7.59 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
<!DOCTYPE html>
<html>
<head>
<title>Shape Annotation JavaScript Control Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="../../css/primitives.css" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../primitives.js"></script>
<script type="text/javascript" src="../../javascriptsamples.js"></script>
<script type="text/javascript" src="../../interactivetests.js"></script>
<link href="./css/styles.css" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var shapeAnnotationControl;
var timer = null;
var { Point, Size, Rect, ShapeAnnotationControl } = primitives;
var { ValueType, Render, PanelConfig, ColorConfig, SizeConfig, CheckBoxConfig, RadioBoxConfig, RangeConfig, DropDownBoxConfig, CaptionConfig, DropDownBoxConfig } = javascriptsamples;
var { Shape, createGraphics, PlacementType, Colors, LineType } = interactivetests;
var optionsRender = new Render([
new javascriptsamples.PanelConfig("Layout & Shape", [
new RadioBoxConfig("shapeType", primitives.ShapeType.Rectangle, "Shape type", primitives.ShapeType, ValueType.Integer, onUpdate),
new RadioBoxConfig("orientationType", primitives.OrientationType.Top, "Diagram orientation type", primitives.OrientationType, ValueType.Integer, onUpdate),
new DropDownBoxConfig("offset", 5, "Offset", [-50, -20, -10, -5, 0, 5, 10, 20, 50], ValueType.Thickness, onUpdate),
new DropDownBoxConfig("cornerRadius", "5%", "Corner Radius", ["0%", "5%", "10%", "20%", 0, 5, 10, 20, 50], ValueType.String, onUpdate),
new RadioBoxConfig("labelPlacement", primitives.PlacementType.Bottom, "Label Placement", primitives.PlacementType, ValueType.Integer, onUpdate),
]),
new javascriptsamples.PanelConfig("Style", [
new RadioBoxConfig("lineType", primitives.LineType.Solid, "Line Type", primitives.LineType, ValueType.Integer, onUpdate),
new ColorConfig("borderColor", primitives.Colors.Black, "Border Color", false, onUpdate),
new RangeConfig("lineWidth",2, "Line width", 0, 10, 1, onUpdate),
new ColorConfig("fillColor", primitives.Colors.LightGray, "Fill Color", false, onUpdate),
new RangeConfig("opacity", 1.0, "Opacity", 0, 1, 0.1, onUpdate)
]),
new javascriptsamples.PanelConfig("Label", [
new CheckBoxConfig("hasLabel", true, "Show", onUpdate),
new RangeConfig("labelOffset", 3, "Offset", 0, 100, 1, onUpdate),
new SizeConfig("labelSize", new Size(4, 4), "Size", 1, 200, 1, onUpdate),
])
], {
shapeType: primitives.ShapeType.Rectangle,
orientationType: primitives.OrientationType.Top,
offset: 20,
cornerRadius: "5%",
labelPlacement: primitives.PlacementType.Bottom,
lineType: primitives.LineType.Solid,
borderColor: primitives.Colors.Black,
lineWidth: 2,
fillColor: primitives.Colors.LightGray,
opacity: 1.0,
hasLabel: true,
labelOffset: 4,
labelSize: new Size(60, 30)
});
document.addEventListener('DOMContentLoaded', function () {
optionsRender.render(document.getElementById("westpanel"));
onUpdate();
window.addEventListener('resize', function (event) {
onWindowResize();
});
});
var position = new Rect(200, 200, 200, 100);
const dragContext = {
name: null,
rectRef: null,
startPoint: null,
topLeftStartPoint: null,
bottomRightStartPoint: null
};
function drag(event) {
dragContext.startPoint = { x: event.clientX, y: event.clientY };
dragContext.name = event.target.id;
dragContext.topLeftStartPoint = new Point(position);
event.dataTransfer.setDragImage(new Image(), 0, 0);
}
function dragResize(event) {
dragContext.startPoint = { x: event.clientX, y: event.clientY };
dragContext.name = event.target.id;
dragContext.bottomRightStartPoint = new Point(position.right(), position.bottom());
event.dataTransfer.setDragImage(new Image(), 0, 0);
}
function onDragOver(event) {
event.preventDefault();
const { name, startPoint, topLeftStartPoint, bottomRightStartPoint } = dragContext;
var endPoint = new Point(event.clientX, event.clientY);
switch(name) {
case "rect":
position.x = topLeftStartPoint.x + endPoint.x - startPoint.x;
position.y = topLeftStartPoint.y + endPoint.y - startPoint.y;
break;
case "resizer":
var bottomRight = new Point(bottomRightStartPoint.x + endPoint.x - startPoint.x, bottomRightStartPoint.y + endPoint.y - startPoint.y);
var topLeft = new Point(position);
position = new Rect(topLeft, bottomRight);
break;
}
onUpdate()
}
function drop(event) {
event.preventDefault();
dragContext.name = null;
}
function onUpdate() {
var rect = document.getElementById("rect");
rect.style.left = position.x + "px";
rect.style.top = position.y + "px";
rect.style.width = position.width + "px";
rect.style.height = position.height + "px";
var options = optionsRender.getValues();
options.label = options.hasLabel ? "100" : null;
options.position = position;
if (!shapeAnnotationControl) {
shapeAnnotationControl = new ShapeAnnotationControl(document.getElementById("annotation"), options);
} else {
shapeAnnotationControl.setOptions(options);
shapeAnnotationControl.update();
}
}
function onWindowResize() {
if (timer == null) {
timer = window.setTimeout(function () {
onUpdate();
window.clearTimeout(timer);
timer = null;
}, 300);
}
}
</script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-4 col-lg-2 d-md-block bg-light sidebar collapse" style="position: fixed; top: 0; bottom: 0px; overflow-y: auto; float: none;">
<div class="position-sticky pt-3">
<div id="westpanel" class="flex-column">
</div>
</div>
</nav>
<main class="col-md-8 ms-sm-auto col-lg-10 px-md-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Shape Annotation Control</h1>
</div>
<div id="centerpanel" class="mediaPlaceholder my-2 w-100" ondrop="drop(event)" ondragover="onDragOver(event)" style="border-style: dotted; border-width: 1px;">
<div id="placeholder" style="overflow: hidden; position: relative; width: 100%; height: 100%;">
<div id="annotation" style="position: absolute; padding: 0px; margin: 0px; width: 100%; height: 100%;"></div>
<div id="rect" style="position: absolute; top: 300px; left: 300px; width: 200px; height: 100px; border-width: 1px; border-style: dotted;" draggable="true" ondragstart="drag(event)">Drag & Resize Rect
<div id="resizer" style="position: absolute; right: 0px; bottom: 0px; width: 16px; height: 8px; border-width: 1px; border-style: dotted;" draggable="true" ondragstart="dragResize(event)"></div>
</div>
</div>
</div>
<div id="southpanel" class="alert alert-primary" style="min-height: 60px;" role="alert">
</div>
</main>
</div>
</div>
</body>
</html>