-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
332 lines (273 loc) · 11.9 KB
/
index.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Drawing App</title>
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://code.jquery.com/jquery-3.5.1.slim.js"></script>
<!-- Fontawsome icons: -->
<script src=https://kit.fontawesome.com/3a0ae6b487.js></script>
</head>
<body>
<h1>Welcome to the Drawing App! (Latest Chrome or Firefox for Desktop PC are recommended).</h1>
<div class= "container">
<div class="color-container">
<button class="btn btn-black" type="button"></button>
<button class="btn btn-red" type="button"></button>
<button class="btn btn-orange" type="button"></button>
<button class="btn btn-yellow" type="button"></button>
<button class="btn btn-green" type="button"></button>
<button class="btn btn-blue" type="button"></button>
<button class="btn btn-purple" type="button"></button>
<button class="btn btn-pink" type="button"></button>
<button class="btn btn-brown" type="button"></button>
<button class="btn btn-grey" type="button"></button>
</div>
<!-- "Stage" container: this is the place where cnavases will be added by Stage class. -->
<div id="stage"></div>
<div class="toolbar-container">
<fieldset class="display-container" id="shapes">
<legend>Shapes:</legend>
<div class="shapes">
<button id="line" class="btn btn-line" type="button" title="Line"></button>
<button id="circle" class="btn btn-circle" type="button" title="Circle"></button>
<button id="ellipse" class="btn btn-ellipse" type="button" title="Ellipse"></button>
<button id="square" class="btn btn-square" type="button" title="Rectangle"></button>
</div>
</fieldset>
<div class="vgap" style="margin-bottom: 10px;"></div>
<fieldset class="display-container" id="position">
<legend>Position:</legend>
<div class="display">
<div class="label" title="X coordinate">x:</div>
<div class="value" name="x">0</div>
</div>
<div class="display">
<div class="label" title="Y coordinate">y:</div>
<div class="value" name="y">0</div>
</div>
</fieldset>
<div class="vgap" style="margin-bottom: 15px;"></div>
<div class="display-container" id="zoom">
<div>Zoom:</div>
<div class="display">
<div class="label" title="Zoom factor">z:</div>
<div class="value" name="z">1.0</div>
</div>
</div>
<div class="vgap" style="margin-bottom: 15px;"></div>
<fieldset class="display-container" id="offset">
<legend>Offset:</legend>
<div class="display">
<div class="label" title="offset by X axis">x:</div>
<div class="value" name="x">0</div>
</div>
<div class="display">
<div class="label" title="offset by Y axis">y:</div>
<div class="value" name="y">0</div>
</div>
<div class="vgap" style="margin-bottom: 15px;"></div>
<div class="move-controls">
<div class="move-control"></div>
<div class="move-control">
<button id="move-up" type="button" class="fas fa-angle-up"></button>
</div>
<div class="move-control"></div>
<div class="move-control">
<button id="move-left" type="button" class="fas fa-angle-left"></button>
</div>
<div class="move-control">
<button id="move-center" type="button" class="fas fa-expand-arrows-alt"></button>
</div>
<div class="move-control">
<button id="move-right" type="button" class="fas fa-angle-right"></button>
</div>
<div class="move-control"></div>
<div class="move-control">
<button id="move-down" type="button" class="fas fa-angle-down"></button>
</div>
<div class="move-control"></div>
</div>
</fieldset>
<div class="vgap" style="margin-bottom:auto; align-self:stretch;"></div>
<div class="actions">
<button id="clear" class="btn-clear" type="button">Clear</button>
</div>
</div>
</div>
<script type="module">
/**
* Since ES6, Javascript Supports import/export statements:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
*
* limitation:
* this doesn't work without a web server. If we want to import anything below,
* the index.html file must be openned through a web-server, but not as a local file.
* More theory about it here: https://web.dev/cross-origin-resource-sharing/
* In Chrome it is possible to use "Web Server for Chrome" extention, or
* on VS Code - "Live Server" extention.
*/
import {App} from "./include/App.js";
import {Display} from "./include/Display.js";
import {Dispatcher} from "./include/Dispatcher.js";
$(document).ready( () => {
const app = App.getInstance();
app.init($("#stage"));
const dispatcher = new Dispatcher(app);
let props = {};
/**
* Make this looking fancier.
* with some sort of JQuery UI plugin.
*/
$("#move-center")
.css('cursor','pointer')
.attr("title", "Use buttons to drag the drawing over the canvas, or drag it with middle mouse-button down");
/**
* For each color button attach click handler:
* onClick make button's width wider, so that it was seen that
* the color-button has been clicked.
*/
$.each($('.color-container').find(".btn"), (index, button) => {
$(button).on("click", (event) => {
$.each($('.color-container').find(".btn"), (index, button) => {
if ($(event.target).attr("class") == $(button).attr("class")) {
$(button).css("width", "40px");
$(button).blur();
props.color = $(button).css("background-color");
} else {
$(button).css("width", "30px");
}
});
});
});
/**
* box-shadow decoration for the selected shape button:
*/
$.each($('.shapes').find(".btn"), (index, button) => {
$(button).on("click", (event) => {
$.each($('.shapes').find(".btn"), (index, button) => {
if ($(event.target).attr("id") == $(button).attr("id")) {
$(button).css("box-shadow", "0 0 1px #51abff, 0 0 2px #51abff, 0 0 6px #51abff, 0 0 12px #51abff");
$(button).blur();
} else {
$(button).css("box-shadow", "");
}
});
});
});
/**
* Trigger clisk event at black=color button,
* to pick black color as default.
*/
$('.color-container').find(".btn.btn-black").click();
/**
* Event handlers for shape-buttons
*/
$("#line").on("click", (event) => {
props.shape = "line";
});
$("#circle").on("click", (event) => {
props.shape = "circle";
});
$("#ellipse").on("click", (event) => {
props.shape = "ellipse";
});
$("#square").on("click", (event) => {
props.shape = "rectangle"
});
$("#clear").on("click", (event) => {
app.stage.clearAndReset();
});
/**
* Trace the mouse pointer and process its "mousemove" and "click" events:
*/
$("#trace").on("mousemove", (event) => {
dispatcher.dispatch(event, Object.assign({}, props));
});
$("#trace").on("click mousewheel", (event) => {
dispatcher.dispatch(event, Object.assign({}, props));
});
$("#trace").on("mousedown mouseup mouseover mouseout", (event) => {
dispatcher.dispatch(event, Object.assign({}, props));
});
/**
* Trace ESC key-up event
*/
$(document).on("keyup", (event) => {
if (event.key === "Escape") {
dispatcher.dispatch(event, Object.assign({}, props));
}
});
/**
* Dragging with buttons:
*/
$("#move-up").on("click", () => {
app.stage.drag(0, -50);
});
$("#move-down").on("click", () => {
app.stage.drag(0, 50);
});
$("#move-left").on("click", () => {
app.stage.drag(-50, 0);
});
$("#move-right").on("click", () => {
app.stage.drag(50, 0);
});
$("#move-center").on("click", () => {
app.stage.center();
});
$(window).resize(() => {
if ($("#stage").children().attr("width") != $("#stage").css("width")) {
$("#stage").children().attr("width", $("#stage").css("width"));
}
if ($("#stage").children().attr("height") != $("#stage").css("height")) {
$("#stage").children().attr("height", $("#stage").css("height"));
}
app.layer("trace").context.globalAlpha = 0.3;
app.layer("trace").context.setLineDash([1, 3]);
app.stage.drag(0, 0);
})
/**
* Two Display objects that assigned for notifications from
* Stage and Dispatcher.MouseTracker.
*/
let xyDisplay = new Display();
xyDisplay.addKey("x", $("#position").find("div.display").find("[name='x']").get(0));
xyDisplay.addKey("y", $("#position").find("div.display").find("[name='y']").get(0));
dispatcher.addObserver(xyDisplay);
let zoDiplay = new Display();
zoDiplay.addKey("x", $("#offset").find("div.display").find("[name='x']").get(0));
zoDiplay.addKey("y", $("#offset").find("div.display").find("[name='y']").get(0));
zoDiplay.addKey("z", $("#zoom").find("div.display").find("[name='z']").get(0));
app.stage.addObserver(zoDiplay);
/**
* The below peice of code stands for testing purpose only.
* It creates a hidden buttton which could be clicked virtually
* by the code in browser's dev-tools console:
*
* $("#draw-button").click();
*
* more about creating DOM elements in JQuery here:
* https://api.jquery.com/jquery/#entry-examples-1
*/
$( "<button/>", {
"class": "btn-clear",
"id": "draw-button",
"text": "Draw",
"style": "display: none;",
"click": function() {
app.stage.clearAndReset();
app.layer("main").draw({shape: "rectangle", x:50, y:350, w:200, h: 100});
app.layer("main").draw({shape: "circle", x:150, y:400, r:2});
app.layer("main").draw({shape: "circle", x:600, y:100, r:80});
app.layer("main").draw({shape: "circle", x:600, y:100, r:2});
// app.layer("main").draw({shape: "rectangle", start: {x:50, y:350}, end: {x:250, y: 450}});
}
}).appendTo($(".actions").get(0));
});
</script>
</body>
</html>