-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragGraphicEinzel.js
More file actions
408 lines (344 loc) · 13.4 KB
/
DragGraphicEinzel.js
File metadata and controls
408 lines (344 loc) · 13.4 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
Daten aus der data.json Datei laden
Das ganze Programm läuft dann in der Ladefunktion
*/
var img;
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function () {
jsonData = JSON.parse(this.responseText);
/*
Titel eintragen
*/
document.getElementById("Titel").innerHTML = jsonData.titel;
document.getElementById("Angabe").innerHTML = jsonData.angabe;
document.getElementById("Ueberschrift").innerHTML = jsonData.titel;
/*
Variablen umwandeln
*/
var dateiname = jsonData.dateiname; // Name des geladenen Bildes
var copyright = jsonData.copyright; // Bildquelle
var snap = jsonData.snap; // true für Arbeit false für Erstellen
var stapel = jsonData.stapel; // true wenn alle Kärtchen aufeinander liegen
var txt = jsonData.txt; // Koordinaten der Kärtchen
var text = new Array(); // Verschiebbare Kärtchen
var nummer = 0;
/*
Zeichenfläche vorbereiten
*/
var width = window.innerWidth;
var height = window.innerHeight;
var stage = new Konva.Stage({
container: 'container',
width: width,
height: height,
});
var layer = new Konva.Layer();
var layerDrag = new Konva.Layer();
var layerButton = new Konva.Layer();
/*
Button für Copyright-Vermerk
*/
var btnCopyRight = new Konva.Label({
x: width * 0.8 + 5, // y wird später ersetzt
width: width * 0.2,
opacity: 0.75
});
btnCopyRight.add(new Konva.Tag({
fill: 'black',
lineJoin: 'round',
shadowColor: 'black',
shadowBlur: 10,
shadowOffset: 10,
shadowOpacity: 0.5
}));
btnCopyRight.add(new Konva.Text({
text: 'Quellenhinweis',
fontFamily: 'Calibri',
fontSize: 18,
padding: 5,
fill: 'white'
}));
layerButton.add(btnCopyRight);
btnCopyRight.on('click', () => quelleAngeben());
btnCopyRight.on('tap', () => quelleAngeben());
/*
Schaltfläche zum Prüfen des Ergebnisses
*/
var button = new Konva.Label({
x: width * 0.8 + 5,
y: 20,
opacity: 0 // Unsichtbar zu Beginn
});
button.add(new Konva.Tag({
fill: 'black',
lineJoin: 'round',
shadowColor: 'black',
shadowBlur: 10,
shadowOffset: 10,
shadowOpacity: 0.5
}));
button.add(new Konva.Text({
text: 'Prüfen',
fontFamily: 'Calibri',
fontSize: 18,
padding: 5,
fill: 'white'
}));
button.on('click', () => pruefen());
button.on('tap', () => pruefen());
layerButton.add(button);
// Bild laden
var imageObj = new Image();
imageObj.onload = function () {
img = new Konva.Image({
x: 0,
y: 0,
image: imageObj,
});
console.log(img.width() + " " + img.height());
console.log(width + " " + height);
var layerFaktor = layer.width() / layer.height();
console.log(layerFaktor);
// Bild auf 80% der Seitenbreite vergrößern
var w = img.width();
var h = img.height();
var bildFaktor = w / h;
console.log(bildFaktor);
var faktor;
if (bildFaktor > layerFaktor) { // Bild ist breiter als Layer
faktor = layer.width() * 0.8 / w; // Vergrößerungsfaktor
} else {
faktor = layer.height() * 0.8 / h; // Vergrößerungsfaktor
}
img.width(faktor * w);
img.height(faktor * h);
// add the shape to the layer
layer.add(img);
stage.height(img.height()); // Höhe der Zeichenflaeche anpassen
btnCopyRight.y(stage.height() - 60);
btnCopyRight.x(stage.width() - btnCopyRight.width() - 30);
button.x(stage.width() - button.width() - 30);
button.y(30);
var rectRechts = new Konva.Rect({
x: img.width() + 20,
y: 20,
width: stage.width() - img.width() - 40,
height: stage.height() - 40,
cornerRadius: 10,
stroke: "lightgray"
});
layer.add(rectRechts);
rectTxt = new Konva.Text({
x: img.width() + 20,
y: 20,
text: 'Ablage',
fontFamily: 'Calibri',
fontSize: 18,
padding: 5,
fill: 'lightgray'
});
layer.add(rectTxt);
if (!snap) {
document.getElementById("ausgabe").style = "visibility: visible";
var isDrawing = false;
var drawrect;
stage.on("mousedown", mousedownHandler);
stage.on("mousemove", mousemoveHandler);
stage.on("mouseup", mouseupHandler);
function mousedownHandler() {
isDrawing = true;
drawrect = new Konva.Rect({
x: stage.getPointerPosition().x,
y: stage.getPointerPosition().y,
width: 0,
height: 0,
stroke: "red",
});
layerDrag.add(drawrect);
}
function mousemoveHandler() {
if (!isDrawing) return false;
const newWidth = stage.getPointerPosition().x - drawrect.x();
const newHeight = stage.getPointerPosition().y - drawrect.y();
drawrect.width(newWidth);
drawrect.height(newHeight);
layerDrag.batchDraw();
}
function mouseupHandler() {
isDrawing = false;
var img2;
if ((drawrect.width() + drawrect.height()) > 5) {
var x = Math.round(drawrect.x() / img.width() * 1000) / 1000;
var y = Math.round(drawrect.y() / img.height() * 1000) / 1000;
var w = Math.round(drawrect.width() / img.width() * 1000) / 1000;
var h = Math.round(drawrect.height() / img.height() * 1000) / 1000;
var ausg = document.getElementById("txt");
if (ausg.innerHTML.length > 20) { ausg.innerHTML = ausg.innerHTML + ",<br>"; }
ausg.innerHTML = ausg.innerHTML + '{"x":' + x + ' ,"y":' + y + ',"width":' + w + ',"height":' + h + ',"pos":[' + nummer + ']}';
var zahlText = new Konva.Text({
x: drawrect.x(),
y: drawrect.y(),
text: nummer.toString()
});
layerDrag.add(zahlText);
layerDrag.batchDraw();
nummer++;
}
}
} else {
// Kärtchen erzeugen
for (i = 0; i < txt.length; i++) {
var img2 = img.clone();
console.log(img2.width() + " " + img2.height() + " " + img.width() + " " + img.height());
console.log(txt[i].x * img.width() + " " + txt[i].y * img.height() + " " + txt[i].width * img.width() + " " + txt[i].height * img.height());
text[i] = img2.crop({
x: txt[i].x * img.width() / faktor,
y: txt[i].y * img.height() / faktor,
width: txt[i].width * img.width() / faktor,
height: txt[i].height * img.height() / faktor
});
}
// Kärtchen formatieren und darstellen
for (i = 0; i < txt.length; i++) {
// text[i].x(width * 0.8);
text[i].stroke("lightblue");
text[i].width(txt[i].width * img.width());
text[i].height(txt[i].height * img.height());
text[i].draggable(true);
if (stapel) {
text[i].x(rectRechts.x() + rectRechts.width() / 2 - text[i].width() / 2);
text[i].y(rectRechts.y() + rectRechts.height() / 2 - text[i].height() / 2);
} else {
text[i].x(rectRechts.x() + rectRechts.width() / 2 - text[i].width() / 2);
text[i].y(rectRechts.y() + 20+((txt.length-i)*(rectRechts.height()-40)/txt.length));
}
layerDrag.add(text[i]);
/*
Funktion, die beim Ende des Drag-Vorgangs aufgerufen wird
*/
text[i].on('dragend', function (e) {
var obj = e.target;
var x = Math.round(obj.x() / img.width() * 1000) / 1000;
var y = Math.round(obj.y() / img.height() * 1000) / 1000;
if (obj.x() < img.width()) { // Snap-Funktion gilt nicht, wenn die Karte nach rechts gezogen
var n = 9999;
var mindist = layer.width() * layer.width() + layer.height() * layer.height();
for (i = 0; i < txt.length; i++) {
dist = (x - txt[i].x) * (x - txt[i].x) + (y - txt[i].y) * (y - txt[i].y);
if (dist < mindist) {
mindist = dist;
n = i;
}
}
if (n != 9999) { // Hier wird auf die Koordinaten geschoben
this.x(txt[n].x * img.width());
this.y(txt[n].y * img.height());
layer.add(new Konva.Rect({ // Positionspunkte überdecken
x: txt[n].x * img.width(),
y: txt[n].y * img.height(),
width: 5,
height: 5,
fill: "white"
}));
} else {
console.log("Objekt nicht gefunden!");
}
}
var alle = true;
for (i = 0; i < text.length; i++) {
// console.log("i:"+i+" x"+text[i].x()+" width"+img.width());
if (text[i].x() >= img.width()) {
alle = false;
i = 99;
}
}
if (alle) {
console.log("True");
button.opacity(0.75);
}
});
/*
Lücken löschen
*/
layer.add(new Konva.Rect({
x: txt[i].x * img.width(),
y: txt[i].y * img.height(),
width: txt[i].width * img.width(),
height: txt[i].height * img.height(),
fill: "white"
}));
/*
Positionspunkte setzen
*/
layer.add(new Konva.Rect({
x: txt[i].x * img.width(),
y: txt[i].y * img.height(),
width: 5,
height: 5,
fill: "lightgreen"
}));
layer.batchDraw();
}
}
};
imageObj.src = dateiname;
// add the layer to the stage
stage.add(layer);
stage.add(layerDrag);
stage.add(layerButton);
/*
Ausgabe von Text in toast
*/
function ausgabe(title, msg, dauer, type) {
VanillaToasts.create({
title: title,
text: msg,
timeout: dauer,
type: type,
});
}
function quelleAngeben() {
ausgabe("Quellenangabe", "Bildquelle: <br>" + copyright + "<br><br>"
+ "Programmierung: <br>2021 Rainer Hille<br><br>"
+ "Unter Verwendung von <br>"
+ "<a href='https://konvajs.org'>Konva.js</a><br>"
+ "<a href='https://www.cssscript.com/toast-style-web-notifications-in-vanilla-javascript-vanillatoasts/'>VanillaToasts.js</a>"
+ "", 3000, "info");
}
function pruefen() {
if (button.opacity() > 0) { // Button muss sichtbar sein!
var anzGefunden = 0;
console.log("Prüfung");
for (i1 = 0; i1 < text.length; i1++) { // Alle Textbausteine durchlaufen
var t1 = text[i1];
gefunden = false;
var t2 = txt[i1];
for (i3 = 0; i3 < t2.pos.length; i3++) { // Alle alternativen Positionen durchlaufen
var t3 = txt[t2.pos[i3]];
var dx = Math.abs(t1.x() - t3.x * img.width());
var dy = Math.abs(t1.y() - t3.y * img.height());
if (dx < 2 && dy < 2) {
gefunden = true;
t1.stroke('white');
t1.strokeWidth(0);
anzGefunden++;
i3 = t2.pos.length;
i2 = txt.length;
} else if (!gefunden) {
t1.strokeWidth(2);
t1.stroke('red');
}
}
}
}
if (anzGefunden == text.length) {
ausgabe("Gratulation", "Super, du hast die Aufgabe vollständig gelöst!", 3000, "success");
} else {
ausgabe("Hinweis", "Die roten Kärtchen musst du noch korrigieren!", 3000, "error");
}
}
}
var href = window.location.href;
href = href.substring(0, href.lastIndexOf("/"));
xmlhttp.open("GET", href + "/data.json");
xmlhttp.send();