-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.js
More file actions
205 lines (197 loc) · 7.61 KB
/
graphics.js
File metadata and controls
205 lines (197 loc) · 7.61 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
/* Interactive elements
* Model script
* Copyright 2016 Harmen de Weerd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
showGraphicsWarnings = true;
GraphicsConstants = {
RESIZE_AREA: 5,
ACTION_NONE: 0,
ACTION_DRAG: 1,
ACTION_E_RESIZE: 2,
ACTION_W_RESIZE: 4,
ACTION_S_RESIZE: 8,
ACTION_N_RESIZE: 16,
ACTION_NE_RESIZE: 18,
ACTION_NW_RESIZE: 20,
ACTION_SE_RESIZE: 10,
ACTION_SW_RESIZE: 12
};
function ImageElement(img) {
this.image = img;
this.x = 0;
this.y = 0;
this.rotation = 0;
this.getWidth = function() {
return this.image.width;
};
this.getHeight = function() {
return this.image.height;
};
this.draw = function(ctx, x = this.x, y = this.y) {
var centerX, centerY;
centerX = Math.floor(this.image.width/2);
centerY = Math.floor(this.image.height/2);
ctx.save();
ctx.translate(x + centerX, y + centerY);
ctx.rotate(this.rotation);
ctx.translate(-centerX, -centerY);
ctx.drawImage(this.image, 0, 0);
ctx.restore();
};
}
function makeInteractiveElement(graphicsElem, container) {
graphicsElem.repertoire = GraphicsConstants.ACTION_DRAG;
graphicsElem.isDraggable = function() {
return (this.repertoire & GraphicsConstants.ACTION_DRAG) > 0;
};
graphicsElem.isResizable = function() {
return (this.repertoire & GraphicsConstants.ACTION_RESIZE) > 0;
};
graphicsElem.currentAction = GraphicsConstants.ACTION_NONE;
/* graphicsElem.onResize = null;
graphicsElem.onDrag = null;
graphicsElem.onDragging = null;
graphicsElem.onDrop = null;*/
graphicsElem.startAction = function(event) {
this.reference = {x: event.target.mouse.x, y: event.target.mouse.y};
this.originalDimensions = {x: this.x,
y: this.y,
width: this.getWidth(),
height: this.getHeight()};
this.currentAction = this.getLocalAction(event.target.mouse.x - this.x,
event.target.mouse.y - this.y);
switch (this.currentAction) {
case GraphicsConstants.ACTION_DRAG:
if (this.onDrag && !this.onDrag(this, event)) {
this.currentAction = GraphicsConstants.ACTION_NONE;
}
break;
case GraphicsConstants.ACTION_SE_RESIZE:
case GraphicsConstants.ACTION_SW_RESIZE:
case GraphicsConstants.ACTION_S_RESIZE:
case GraphicsConstants.ACTION_NW_RESIZE:
case GraphicsConstants.ACTION_NE_RESIZE:
case GraphicsConstants.ACTION_W_RESIZE:
case GraphicsConstants.ACTION_S_RESIZE:
case GraphicsConstants.ACTION_N_RESIZE:
case GraphicsConstants.ACTION_E_RESIZE:
if (this.onResize && !this.onResize(this, event, this.currentAction)) {
this.currentAction = GraphicsConstants.ACTION_NONE;
}
break;
}
};
graphicsElem.endAction = function(event) {
if (this.currentAction == GraphicsConstants.ACTION_DRAG && this.onDrop) {
this.onDrop(this, event);
}
this.currentAction = GraphicsConstants.ACTION_NONE;
};
graphicsElem.continueAction = function(event) {
var x, y;
x = event.target.mouse.x;
y = event.target.mouse.y;
if (this.constraints != null) {
x = Math.min(this.constraints.right + this.reference.x,
Math.max(this.constraints.left + this.reference.x, x));
y = Math.min(this.constraints.bottom + this.reference.y,
Math.max(this.constraints.top + this.reference.y, y));
}
if ((this.currentAction & GraphicsConstants.ACTION_DRAG) > 0) {
this.x = this.originalDimensions.x + x - this.reference.x;
this.y = this.originalDimensions.y + y - this.reference.y;
} else {
if ((this.currentAction & GraphicsConstants.ACTION_S_RESIZE) > 0) {
this.elem.setHeight(this.originalDimensions.height + y - this.reference.y);
}
if ((this.currentAction & GraphicsConstants.ACTION_E_RESIZE) > 0) {
this.elem.setWidth(this.originalDimensions.width + x - this.reference.x);
}
if ((this.currentAction & GraphicsConstants.ACTION_N_RESIZE) > 0) {
this.y = this.originalDimensions.y + y - this.reference.y;
this.elem.setHeight(this.originalDimensions.height - y + this.reference.y);
}
if ((this.currentAction & GraphicsConstants.ACTION_W_RESIZE) > 0) {
this.x = this.originalDimensions.x + x - this.reference.x;
this.elem.setWidth(this.originalDimensions.width - x + this.reference.x);
}
}
if (this.onDragging) {
this.onDragging(this, event.target);
}
};
graphicsElem.getActionLabel = function(x, y) {
switch (this.getLocalAction(x,y)) {
case GraphicsConstants.ACTION_W_RESIZE:
case GraphicsConstants.ACTION_E_RESIZE:
return "col-resize";
case GraphicsConstants.ACTION_S_RESIZE:
case GraphicsConstants.ACTION_N_RESIZE:
return "row-resize";
case GraphicsConstants.ACTION_NW_RESIZE:
return "nw-resize";
case GraphicsConstants.ACTION_NE_RESIZE:
return "ne-resize";
case GraphicsConstants.ACTION_SE_RESIZE:
return "se-resize";
case GraphicsConstants.ACTION_SW_RESIZE:
return "sw-resize";
case GraphicsConstants.ACTION_DRAG:
return "pointer";
default:
return undefined;
}
};
graphicsElem.getLocalAction = function(x, y) {
if ((this.repertoire & GraphicsConstants.ACTION_W_RESIZE) > 0 &&
x < GraphicsConstants.RESIZE_AREA) {
if ((this.repertoire & GraphicsConstants.ACTION_N_RESIZE) > 0 &&
y < GraphicsConstants.RESIZE_AREA) {
return GraphicsConstants.ACTION_NW_RESIZE;
} else if ((this.repertoire & GraphicsConstants.ACTION_S_RESIZE) > 0 &&
y > this.getHeight() - GraphicsConstants.RESIZE_AREA) {
return GraphicsConstants.ACTION_SW_RESIZE;
}
return GraphicsConstants.ACTION_W_RESIZE;
} else if ((this.repertoire & GraphicsConstants.ACTION_E_RESIZE) > 0 &&
x > this.getWidth() - GraphicsConstants.RESIZE_AREA) {
if ((this.repertoire & GraphicsConstants.ACTION_N_RESIZE) > 0 &&
y < GraphicsConstants.RESIZE_AREA) {
return GraphicsConstants.ACTION_NE_RESIZE;
} else if ((this.repertoire & GraphicsConstants.ACTION_S_RESIZE) > 0 &&
y > this.getHeight() - GraphicsConstants.RESIZE_AREA) {
return GraphicsConstants.ACTION_SE_RESIZE;
}
return GraphicsConstants.ACTION_E_RESIZE;
} else if ((this.repertoire & GraphicsConstants.ACTION_N_RESIZE) > 0 &&
y < GraphicsConstants.RESIZE_AREA) {
return GraphicsConstants.ACTION_N_RESIZE;
} else if ((this.repertoire & GraphicsConstants.ACTION_S_RESIZE) > 0 &&
y > this.getHeight() - GraphicsConstants.RESIZE_AREA) {
return GraphicsConstants.ACTION_S_RESIZE;
}
if (this.isDraggable) {
return GraphicsConstants.ACTION_DRAG;
}
return undefined;
};
if (container.addInteractiveElement) {
container.addInteractiveElement(graphicsElem);
} else if (showGraphicsWarnings) {
console.warn("Container does not support mouse tracking: element will not be interactive.");
}
return graphicsElem;
}