-
Notifications
You must be signed in to change notification settings - Fork 2
/
15.html
441 lines (375 loc) · 11.4 KB
/
15.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
440
441
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>15、缩放、移动画布</title>
<style>
div.canvas-container{
max-width: 640px;
max-height: 480px;
overflow: hidden;
border: 1px solid red;
}
</style>
</head>
<body>
<div>
<button id="btnZoomIn">ZoomIn</button>
<button id="btnZoomOut">ZoomOut</button>
<button id="btnResetZoom">Reset Zoom</button>
<button id="btnZoomToRight">Canvas: →</button>
<button id="btnZoomToLeft">Canvas: ←</button>
<button id="btnZoomToUp">Canvas: ↑</button>
<button id="btnZoomToDown">Canvas: ↓</button>
<button id="btnZoomInByMouse">mouse: zoom-in</button>
<button id="btnZoomOutByMouse">mouse: zoom-out</button>
<button id="btnPanModeByMouse">mouse: Pan-mode</button>
<span id="zoomByMouseStatus" style="border: 1px solid blue; color: blue; background-color: yellow;">No-Zoom</span>
</div>
<canvas id="c"></canvas>
<script src="./bower_components/jQuery/dist/jquery.js"></script>
<script src="./bower_components/fabric.js/dist/fabric.js"></script>
<script>
var src = "images/qq.png";
var SCALE_FACTOR = 1.3; //on clicking zoom, enlarge canvas in 30%. TBD
var zoomInByMouse = false;
var zoomOutByMouse = false;
var PanModeByMouse = false;
var zoomMax = 23; //TBD
var mouseDown;
var canvasHeight = 480;
var canvasWidth = 640;
// canvas
var canvas = new fabric.Canvas('c', { width: canvasWidth, height: canvasHeight });
var getFabricCanvases = (function () {
var fabricCanvasCollection;
return function getCanvases() {
if (!fabricCanvasCollection) {
fabricCanvasCollection = [];
var fabricCanvas = $('.canvas-container canvas');
fabricCanvas.each(function (index, item) {
fabricCanvasCollection.push($(item));
});
}
console.log(fabricCanvasCollection, 8899)
return fabricCanvasCollection;
}
})();
canvas.on('mouse:down', function (e) {
if (zoomInByMouse) {
zoomByMouseCoords(e, "in");
} else if (zoomOutByMouse) {
zoomByMouseCoords(e, "out");
} else {
mouseDown = true;
}
});
canvas.on('mouse:up', function () {
console.log("canvas.on(mouse:up):: up");
mouseDown = false;
});
//canvas.backgroundColor = "#00fa00";
canvas.setBackgroundImage(
'images/demo.png', canvas.renderAll.bind(
canvas), {
backgroundImageOpacity: 0.5,
backgroundImageStretch: true
});
canvas.on('mouse:move', function (e) {
if (PanModeByMouse && mouseDown) {
console.log("canvas.on(mouse:move):PanModeByMouse:: movementX: " + e.e.movementX + ", " + e.e.movementY);
panToByMouseCoords(e.e.movementX, e.e.movementY);
}
});
// text
var text = new fabric.Text('Fieldbit Note', {
left: 200,
top: 50
});
text.hasRotatingPoint = true;
canvas.add(text);
// circle
var circle = new fabric.Circle({
left: 200,
top: 150,
radius: 50,
fill: "#dde000"
});
circle.hasRotatingPoint = true;
canvas.add(circle);
//image
fabric.util.loadImage(src, function (img) {
var object = new fabric.Image(img);
object.set({
left: 200,
top: 300
});
object.hasRotatingPoint = true;
object.scaleX = object.scaleY = .25;
canvas.add(object);
canvas.renderAll();
});
canvas.renderAll();
// button Zoom In
$("#btnZoomIn").click(function () {
zoomIn();
});
// button Zoom Out
$("#btnZoomOut").click(function () {
zoomOut();
getFabricCanvases().forEach(function (elementValue) {
var currentTop = Number(elementValue.css("top").replace(/[^\d\.\-]/g, ''));
var currentLeft = Number(elementValue.css("left").replace(/[^\d\.\-]/g, ''));
currentTop = zoomCalcYpos(currentTop);
currentLeft = zoomCalcXpos(currentLeft);
elementValue.css("top", currentTop);
elementValue.css("left", currentLeft);
});
});
// button Reset Zoom
$("#btnResetZoom").click(function () {
resetZoom();
});
// button Zoom To Right
$("#btnZoomToRight").click(function () {
zoomToRight();
});
// button Zoom To Left
$("#btnZoomToLeft").click(function () {
zoomToLeft();
});
// button Zoom To Up
$("#btnZoomToUp").click(function () {
zoomToUp();
});
// button Zoom To Up
$("#btnZoomToDown").click(function () {
zoomToDown();
});
// button Zoom To Down
$("#btnZoomInByMouse").click(function () {
zoomInByMouse = !zoomInByMouse;
zoomOutByMouse = PanModeByMouse = false;
document.getElementById('zoomByMouseStatus').innerHTML = "zoomIn: " + zoomInByMouse; //FOR QA ONLY
zoomCanvasSelection(!zoomInByMouse);
console.log("btnZoomInByMouse: " + zoomInByMouse);
});
// button Zoom To Down
$("#btnZoomOutByMouse").click(function () {
zoomOutByMouse = !zoomOutByMouse;
zoomInByMouse = PanModeByMouse = false;
document.getElementById('zoomByMouseStatus').innerHTML = "zoomOut: " + zoomOutByMouse; //FOR QA ONLY
zoomCanvasSelection(!zoomOutByMouse);
console.log("btnZoomOutByMouse: " + zoomOutByMouse);
});
// button Pan
$("#btnPanModeByMouse").click(function () {
PanModeByMouse = !PanModeByMouse;
zoomInByMouse = zoomOutByMouse = false;
document.getElementById('zoomByMouseStatus').innerHTML = "panMode: " + PanModeByMouse; //FOR QA ONLY
zoomCanvasSelection(!PanModeByMouse);
console.log("btnPanModeByMouse: " + PanModeByMouse);
});
createListenersKeyboard();
function createListenersKeyboard() {
document.onkeydown = onKeyDownHandler;
//document.onkeyup = onKeyUpHandler;
}
function onKeyDownHandler(event) {
//event.preventDefault();
var key;
if (window.event) {
key = window.event.keyCode;
} else {
key = event.keyCode;
}
switch (key) {
//////////////
// Shortcuts
//////////////
// Zoom In (Ctrl+"+")
case 187: // Ctrl+"+"
if (ableToShortcut()) {
if (event.ctrlKey) {
event.preventDefault();
zoomIn();
}
}
break;
// Zoom Out (Ctrl+"-")
case 189: // Ctrl+"-"
if (ableToShortcut()) {
if (event.ctrlKey) {
event.preventDefault();
zoomOut();
}
}
break;
// Reset Zoom (Ctrl+"0")
case 48: // Ctrl+"0"
if (ableToShortcut()) {
if (event.ctrlKey) {
event.preventDefault();
resetZoom();
}
}
break;
default:
// TODO
break;
}
}
function ableToShortcut() {
/*
TODO check all cases for this
if($("textarea").is(":focus")){
return false;
}
if($(":text").is(":focus")){
return false;
}
*/
return true;
}
/********************************/
function zoomToRight() {
zoomMoveStepToPosition('left', '+');
}
function zoomToLeft() {
zoomMoveStepToPosition('left', '-');
}
function zoomToDown() {
zoomMoveStepToPosition('top', '+');
}
function zoomToUp() {
zoomMoveStepToPosition('top', '-');
}
/********************************/
//panToByMouseCoords
function panToByMouseCoords(xDelta, yDelta) {
getFabricCanvases().forEach(function (elementValue) {
var currentTop = Number(elementValue.css("top").replace(/[^\d\.\-]/g, ''));
var currentLeft = Number(elementValue.css("left").replace(/[^\d\.\-]/g, ''));
console.log("panToByMouseCoords:: (currentTop, yDelta): (" + currentTop + ", " + yDelta + ")" +
", (currentLeft, xDelta): (" + currentLeft + ", " + xDelta + ")");
currentTop = zoomCalcYpos(currentTop + yDelta);
currentLeft = zoomCalcXpos(currentLeft + xDelta);
elementValue.css("top", currentTop);
elementValue.css("left", currentLeft);
});
}
//Zoom By Mouse Coordinates
function zoomByMouseCoords(event, direction) {
//if selectable element was clicked, don't zoom, just return:
/*if (canvas.getActiveObject()) {
return false;
}*/
if (!direction || !event.e) return;
var pointer = canvas.getPointer(event.e);
console.log("zoomByMouseCoords: direction: " + direction + ", p.x: " + pointer.x + ", p.y:" + pointer.y +
", canvas.width: " + canvas.getWidth());
if (direction.toLowerCase() == "in") {
if (canvas.getZoom().toFixed(5) > zoomMax) {
console.log("zoomByMouseCoords: Error: cannot zoom-in anymore");
return;
}
zoomIn();
}
if (direction.toLowerCase() == "out") {
if (canvas.getZoom().toFixed(5) <= 1) {
console.log("zoomByMouseCoords: Error: cannot zoom-out anymore");
return;
}
zoomOut();
}
zoomToPosition(zoomInCalcXpos(pointer.x), zoomInCalcYpos(pointer.y));
}
// Zoom In
function zoomIn() {
if (canvas.getZoom().toFixed(5) > zoomMax) {
console.log("zoomIn: Error: cannot zoom-in anymore");
return;
}
canvas.setZoom(canvas.getZoom() * SCALE_FACTOR);
canvas.setHeight(canvas.getHeight() * SCALE_FACTOR);
canvas.setWidth(canvas.getWidth() * SCALE_FACTOR);
canvas.renderAll();
}
// Zoom Out
function zoomOut() {
if (canvas.getZoom().toFixed(5) <= 1) {
console.log("zoomOut: Error: cannot zoom-out anymore");
return;
}
canvas.setZoom(canvas.getZoom() / SCALE_FACTOR);
canvas.setHeight(canvas.getHeight() / SCALE_FACTOR);
canvas.setWidth(canvas.getWidth() / SCALE_FACTOR);
canvas.renderAll();
}
// Reset Zoom
function resetZoom() {
canvas.setHeight(canvas.getHeight() / canvas.getZoom());
canvas.setWidth(canvas.getWidth() / canvas.getZoom());
canvas.setZoom(1);
canvas.renderAll();
getFabricCanvases().forEach(function (item) {
item.css('left', 0);
item.css('top', 0);
});
}
function zoomCanvasSelection(isSelectable) {
isSelectable = (isSelectable == true);
canvas.selection = isSelectable;
canvas.forEachObject(function (o) {
o.selectable = isSelectable;
});
}
function zoomInCalcXpos(xPos) {
xPos *= canvas.getZoom();
return zoomCalcXpos((canvasWidth / 2) - xPos);
}
function zoomCalcXpos(xPos) {
if (xPos > 0) {
return 0;
}
if (xPos + canvas.getWidth() < canvasWidth) {
return canvasWidth - canvas.getWidth();
}
return xPos;
}
function zoomInCalcYpos(yPos) {
yPos *= canvas.getZoom();
return zoomCalcYpos((canvasHeight / 2) - yPos);
}
function zoomCalcYpos(yPos) {
if (yPos > 0) {
return 0;
}
if (yPos + canvas.getHeight() < canvasHeight) {
return canvasHeight - canvas.getHeight();
}
return yPos;
}
function zoomToPosition(x, y) {
zoomMoveToPosition('top', y);
zoomMoveToPosition('left', x);
}
function zoomMoveToPosition(position, numericValue) {
getFabricCanvases().forEach(function (elementValue) {
elementValue.css(position, numericValue);
});
}
function zoomMoveStepToPosition(position, operator) {
getFabricCanvases().forEach(function (elementValue) {
var current = Number(elementValue.css(position).replace(/[^\d\.\-]/g, ''));
current = eval(current + operator + 20);
if (position == "left" || position == "right") current = zoomCalcXpos(current);
if (position == "top" || position == "bottom") current = zoomCalcYpos(current);
elementValue.css(position, current);
});
}
</script>
</body>
</html>