-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
439 lines (400 loc) · 11.8 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html>
<head></head>
<body>
<p>
<label>Drawing tool: <select id="dtool">
<option value="rectangle">Rectangle</option>
<option value="circle">Circle</option>
<option value="line">Line</option>
</select>
</label>
</p>
<p>
<label>Fill Color: <select id="fill">
<option value="">No Filled Color</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="white">White</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
<option value="black">Black</option>
<option value="purple">Purple</option>
</select>
</label>
</p>
<p>
<label>Stroke Color: <select id="stroke">
<option value="">No stroke color</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="white">White</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
<option value="black">Black</option>
<option value="purple">Purple</option>
</select>
</label>
</p>
<p>
<label>Stroke Width: <select id="width">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</label>
</p>
<p>
<label>Mode: <select id="mode">
<option value="draw">Drawing</option>
<option value="edit">Editing</option>
</select>
</label>
</p>
<div id="tools">Current tool: rectangle</div>
<div id="colors">Current filled colour:</div>
<div id="strokes">Current stroke colour: black</div>
<div id="widths">Current stroke width: 1</div>
<div id="modes">Current mode: Drawing</div>
<div id="container">
</div>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.3.min.js"></script>
<script defer="defer">
// Create a stage
var stage = new Kinetic.Stage({
container: 'container',
width: 500,
height: 500
});
// Create the related layers
//var backgroundLayer = new Kinetic.Layer(); // store the background shape
var messageLayer = new Kinetic.Layer();
var shapeLayer = new Kinetic.Layer();
var backgroundLayer = new Kinetic.Layer();
// Create a background shape to track the mouse coordinates, same size as the stage
var background = new Kinetic.Rect({
x: 0,
y: 0,
width: stage.getWidth(),
height: stage.getHeight(),
stroke: 'black',
strokeWidth: 1
});
backgroundLayer.add(background);
//background.moveToTop();
// The mouse related variables
var start = false; // Test the mouse is down or not
var mouse = {clickX: 0, clickY: 0, currentX: 0, currentY: 0}; // Store the mouse position
var shapes = []; // Store all the shapes
var shapeModified;
// The tool variable
var tool;
var tool_default = 'rectangle';
var tool_select = document.getElementById('dtool');
tool_select.addEventListener('change', ev_tool_change, false);
var toolWrite = document.getElementById('tools');
// The filled color variable
var color;
var color_default = null;
var fill_color = document.getElementById('fill');
fill_color.addEventListener('change', ev_color_change, false);
var colorWrite = document.getElementById('colors');
// The stroke color variable
var stroke;
var stroke_default = 'black';
var stroke_color = document.getElementById('stroke');
stroke_color.addEventListener('change', ev_stroke_change, false);
var strokeWrite = document.getElementById('strokes');
// The stroke width variables
var width;
var width_track = document.getElementById('width');
width_track.addEventListener('change', ev_width, false);
var widthWrite = document.getElementById('widths');
// The mode variables
var mode;
var mode_track = document.getElementById('mode');
mode_track.addEventListener('change', ev_mode, false);
var modeWrite = document.getElementById('modes');
// Set the default values
function setDefault() {
if (!tool){
tool = tool_default;
}
if (!stroke) {
stroke = stroke_default;
}
};
// Keep Track of the tools
function ev_tool_change (ev) {
tool = this.value;
toolWrite.innerHTML = "Current tool: " + tool;
};
// Keep track of the colors
function ev_color_change (ev) {
color = this.value;
colorWrite.innerHTML = "Current filled colour: " + color;
if (mode == 'edit') {
if (shapeModified) {
shapeModified.setAttr('fill', color);
shapeLayer.draw();
}
}
};
// Keep track of the stroke colors
function ev_stroke_change (ev) {
stroke = this.value;
strokeWrite.innerHTML = "Current stroke colour: " + stroke;
if (mode == 'edit'){
if (shapeModified) {
shapeModified.setAttr('stroke', stroke);
shapeLayer.draw();
}
}
};
var status = true;
// Keep track of the stroke widht
function ev_width(ev) {
width = this.value;
widthWrite.innerHTML = "Current stroke width: " + width;
if (mode == 'edit'){
if (shapeModified) {
shapeModified.setAttr('strokeWidth', width);
shapeLayer.draw();
}
}
};
function ev_mode(ev) {
mode = this.value;
if (mode == 'draw') {
status = true;
modeWrite.innerHTML = "Current mode: Drawing";
backgroundLayer.moveToTop();
shapeModified = null;
} else {
status = false;
modeWrite.innerHTML = "Current mode: Editing";
backgroundLayer.moveToBottom();
}
};
// Keep track of the mouse status mousedown, mousemove, mouseup
background.on('mousedown', function() {
// Set the start variable equal to true
start = true;
var mousePos = stage.getMousePosition();
if (status) {
mouse.clickX = mousePos.x;
mouse.clickY = mousePos.y;
mouse.currentX = mousePos.x;
mouse.currentY = mousePos.y;
setDefault();
shape=drawShape(mouse.clickX, mouse.clickY, mouse.currentX, mouse.currentY, tool, color, stroke, width);
}
});
background.on('mousemove', function(){
if (status) {
if (start) {
var mousePos = stage.getMousePosition(); // Get the current mouse position
mouse.currentX = mousePos.x;
mouse.currentY = mousePos.y;
writeMessage(messageLayer, 'x:' + mouse.currentX + ', y: ' + mouse.currentY);
if (tool == 'rectangle') {
shape.setAttr('height', mouse.currentY - mouse.clickY);
shape.setAttr('width', mouse.currentX - mouse.clickX);
} else if (tool == 'circle') {
shape.setAttr('x', (mouse.clickX + mouse.currentX) / 2);
shape.setAttr('y', (mouse.clickY + mouse.currentY) / 2);
shape.setAttr('radius', Math.sqrt(Math.pow((mouse.currentX - mouse.clickX), 2) + Math.pow((mouse.currentY - mouse.clickY), 2)) / 2);
} else if (tool == 'line') {
shape.setAttr('points' , [mouse.clickX,mouse.clickY,mouse.currentX,mouse.currentY]);
}
shapeLayer.draw();
}
}
});
background.on('mouseup', function() {
start = false; // User is not pressing the mouse
if (mouse.currentX == mouse.clickX && mouse.currentY == mouse.clickY) {
shape.remove();
shapeLayer.draw();
} else {
shapes.push(shape);
}
});
// Draw the shapes
function drawShape(cX, cY, x, y, tool, fillColor, strokeColor, strokeWidth) {
if (tool == 'rectangle') {
var shape = new Kinetic.Rect({
x: cX,
y: cY,
width: x - cX,
height: y - cY,
stroke: strokeColor,
fill: fillColor,
strokeWidth: strokeWidth,
draggable: true
});
} else if (tool == 'circle') {
var shape = new Kinetic.Circle({
x: (cX + x) / 2,
y: (cY + y) / 2,
radius: Math.sqrt(Math.pow((x - cX), 2) + Math.pow((y - cY), 2)) / 2,
fill: fillColor,
stroke: strokeColor,
strokeWidth: strokeWidth,
draggable: true
});
} else if (tool == 'line') {
var shape = new Kinetic.Line({
points: [cX, cY, x, y],
stroke: strokeColor,
strokeWidth: strokeWidth,
fill: fillColor,
lineJoin: 'round',
draggable: true
});
}
shape.on('mouseover', function() {
document.body.style.cursor = 'pointer';
});
shape.on('mouseout', function() {
document.body.style.cursor = 'default';
});
shape.on('mousedown', function() {
this.moveToTop();
shapeModified = this;
});
shape.on('dblclick', function() {
this.remove();
shapeLayer.draw();
});
shapeLayer.add(shape);
shapeLayer.draw();
return shape;
};
// Display message
function writeMessage(messageLayer, message) {
var context = messageLayer.getContext();
messageLayer.clear();
context.font = '18pt Calibri';
context.fillStyle = 'black';
context.fillText(message, 50, 25); // The position to be displayed on the stage
};
// Clear the stage
function clearStages() {
shapes = [];
shapeModified = null;
shapeLayer.removeChildren();
shapeLayer.draw();
};
// Copy the shape
function copyShape() {
if (shapeModified) {
var x = shapeModified.getAttr('x');
var y = shapeModified.getAttr('y');
var clone = shapeModified.clone({
x: x + 3,
y: y + 3
});
shapeModified = clone;
shapeLayer.add(clone);
shapes.push(clone);
shapeLayer.draw();
}
};
// Resizing functions
function update(activeAnchor) {
var group = activeAnchor.getParent();
var topLeft = group.get('.topLeft')[0];
var topRight = group.get('.topRight')[0];
var bottomRight = group.get('.bottomRight')[0];
var bottomLeft = group.get('.bottomLeft')[0];
var image = group.get('.image')[0];
var anchorX = activeAnchor.getX();
var anchorY = activeAnchor.getY();
// update anchor positions
switch (activeAnchor.getName()) {
case 'topLeft':
topRight.setY(anchorY);
bottomLeft.setX(anchorX);
break;
case 'topRight':
topLeft.setY(anchorY);
bottomRight.setX(anchorX);
break;
case 'bottomRight':
bottomLeft.setY(anchorY);
topRight.setX(anchorX);
break;
case 'bottomLeft':
bottomRight.setY(anchorY);
topLeft.setX(anchorX);
break;
}
image.setPosition(topLeft.getPosition());
var width = topRight.getX() - topLeft.getX();
var height = bottomLeft.getY() - topLeft.getY();
if(width && height) {
image.setSize(width, height);
}
}
function addAnchor(group, x, y, name) {
var stage = group.getStage();
var layer = group.getLayer();
var anchor = new Kinetic.Circle({
x: x,
y: y,
stroke: '#666',
fill: '#ddd',
strokeWidth: 2,
radius: 8,
name: name,
draggable: true,
dragOnTop: false
});
anchor.on('dragmove', function() {
update(this);
layer.draw();
});
anchor.on('mousedown touchstart', function() {
group.setDraggable(false);
this.moveToTop();
});
anchor.on('dragend', function() {
group.setDraggable(true);
layer.draw();
});
// add hover styling
anchor.on('mouseover', function() {
var layer = this.getLayer();
document.body.style.cursor = 'pointer';
this.setStrokeWidth(4);
layer.draw();
});
anchor.on('mouseout', function() {
var layer = this.getLayer();
document.body.style.cursor = 'default';
this.setStrokeWidth(2);
layer.draw();
});
group.add(anchor);
}
// the background layer is at the top, shape layer at the top
//stage.add(backgroundLayer);
stage.add(shapeLayer);
stage.add(messageLayer);
stage.add(backgroundLayer);
</script>
<div>
<button onclick="clearStages()">Clear</button>
<button onclick="copyShape()">Copy</button>
</div>
</body>
</html>