-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcanvas.js
More file actions
executable file
·291 lines (270 loc) · 10.4 KB
/
canvas.js
File metadata and controls
executable file
·291 lines (270 loc) · 10.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
////////////////////////////
//Global Vars
///////////////////////////
var canvas = document.getElementById("canvas");//canvas//decleared in media.js
var ctx = canvas.getContext("2d");//drawing object
//mouse coordinates
var mouse = {
x: 0, //x coord
y: 0, //y coord
radius: 6, //mouse detection radius
inCanvas: false, //hovering over canvas
clickedCanvas: false //clicked on canvas
};
//active canvas tool
var activeTool = 'none';
//user Custom Shape
userShape = {
points: [], //points that connect shape
finished: false, //boolean of shape completion
color: '#f00' //shape line color
};
////////////////////////////
//Canvas
///////////////////////////
//Animate Canvas
function animate(){
//remove previous polygon
if(newMedia){
userShape.points = [];//reset user points
userShape.finished = false;
newMedia = false;
}
//background
ctx.clearRect(0, 0, canvas.width, canvas.height);//clear background
strokeShape(userShape);//draw user's shape
activeToolLogic();//script for active tool
//animationloop
requestAnimationFrame(animate);
}
animate();
////////////////////////////
//Event Listeners
///////////////////////////
//move tool button click event
createActiveTool('move-points-tool', 'pointer');
//draw tool button click event
createActiveTool('draw-points-tool', 'crosshair');
//delete tool button click event
createActiveTool('delete-points-tool', 'not-allowed');
//On Cavnas Mouse Move
canvas.addEventListener('mousemove', function (event) {
var rect = canvas.getBoundingClientRect();//canvas dimensions
mouse.x = event.clientX - rect.left;//update mouseX
mouse.y = event.clientY - rect.top;//update mouseY
});
//On mouse in canvas
canvas.onmouseover = function (e) {
mouse.inCanvas = true;
};
//On mouse leave canvas
canvas.onmouseout = function (e) {
mouse.inCanvas = false;
};
//On mouse click in canvas
canvas.addEventListener('click', function(){
mouse.clickedCanvas = true;//simulate canvas click event
});
//coordinate tool button
document.getElementById('coord-tool').addEventListener('click', function(){
var result = ''+userShape.points.length+'<br>';//outputed to user
//loop through each point
for(i=0; i < userShape.points.length ; i++){
//save each point
result+= Math.trunc(userShape.points[i].x) + ' ' + Math.trunc(userShape.points[i].y) + '<br>';
}
//Insert result to html overlay
console.log(result);
document.getElementById('shape-coordinates-result').innerHTML = result;//add result to overlay
showPopup('shape-coordinates-overlay');//show overlay
});
// - Keyboard Shortcuts
document.addEventListener("keydown", function (event) {
//ctrl-z or meta-z
if((event.ctrlKey && event.code === "KeyZ") || (event.metaKey && event.code === "KeyZ") && activeTool === 'draw-points-tool') {
//cancel finished shape
if(userShape.finished)
userShape.finished = false;
//remove last point
else{
userShape.points.pop();
console.log(userShape);
}
}
});
////////////////////////////
//Functions
///////////////////////////
// - Runs function for tool in use
function activeToolLogic(){
if(activeTool === 'none'){
mouse.clickedCanvas = false;//set mouse as unclicked
return;//exit
}
else if (activeTool === 'draw-points-tool'){
drawToolLogic(mouse.clickedCanvas);
}
else if (activeTool === 'move-points-tool'){
moveToolLogic(mouse.clickedCanvas);
}
else if (activeTool === 'delete-points-tool'){
deleteToolLogic(mouse.clickedCanvas);
}
mouse.clickedCanvas = false;//set mouse as unclicked
}
// - Adds button click event & cursor change & sets active tool
function createActiveTool(id, cursor){
var buttonElement = document.getElementById(id);//html button element
//on button click
buttonElement.addEventListener('click', function(){
//Unselect last tool
if(activeTool !== 'none'){
document.getElementById(activeTool).style.backgroundColor = 'rgba(0, 0, 0, 0.49)';//revert active tool background
canvas.style.cursor = 'default';//revert to old cursor
}
//Select tool if new
if(activeTool !== id){
activeTool = id;//change active tool
canvas.style.cursor = cursor;//change to tool cursor
buttonElement.style.backgroundColor = 'white';//set active background
}
//Confirm to unselect last tool
else{
activeTool = 'none';//update active tool
}
});
}
// - Logic for drawing tool
function drawToolLogic(canvasClicked){
if(!userShape.finished){
//on canvas clicked
if(canvasClicked){
//finish shape if clicked on first point
if(userShape.points.length > 1 && mouseOnPoint(userShape.points[0]))
userShape.finished = true;
//user wants to add a point
else{
//adjust if media resize
if(mediaIsShrunk){
userShape.points.push({
x: mouse.x * (originalMediaWidth/canvas.width),
y: mouse.y * (originalMediaHeight/canvas.height)
});
}
//add point normally
else
userShape.points.push({x:mouse.x,y:mouse.y});//add new shape point
}
console.log(userShape.points);//log new shape array
}
//if over first shape point and ready to close shape
if(userShape.points.length > 1 && mouseOnPoint(userShape.points[0])){
console.log('over finisher point');
drawCircle(userShape.points[0].x*(canvas.width/originalMediaWidth), userShape.points[0].y*(canvas.height/originalMediaHeight), 7, 2, '#00ff00', 'fill');
drawCircle(userShape.points[0].x*(canvas.width/originalMediaWidth), userShape.points[0].y*(canvas.height/originalMediaHeight), 8, 2, '#fff', 'stroke');
}
//preview next shape line
if(userShape.points.length > 0 && mouse.inCanvas){
previewStroke(userShape.points[userShape.points.length-1]);//preview from last point in shape array
}
}
}
// - Logic for delete tool
function deleteToolLogic(canvasClicked){
//preview movable points & which point mouse is over
highlightPoints(userShape.points, 'yellow', function(focusedPoint){
//if user wants to edit point being focused
if(focusedPoint !== undefined && canvasClicked){
userShape.points.splice(focusedPoint, 1);//delete selected point
//if shape is not fully connected
if(userShape.finished && userShape.points.length < 3)
userShape.finished = false;//set shape as unfinished
}
});
}
// - Logic for move tool
var editingPoint;//point user is moving
function moveToolLogic(canvasClicked){
//preview movable points & which point mouse is over
highlightPoints(userShape.points, '#0000ff', function(focusedPoint){
//if user clicks while editing point or leaves canvas
if((editingPoint!== undefined && canvasClicked) || !mouse.inCanvas){
editingPoint = undefined;//stop editing point
}
//if user wants to edit point being focused
else if(focusedPoint !== undefined && canvasClicked){
editingPoint = focusedPoint;//update point to edit
}
});
//if editing point is set
if(editingPoint !== undefined){
userShape.points[editingPoint].x = mouse.x*(originalMediaWidth/canvas.width);//set point x to mouse.x
userShape.points[editingPoint].y = mouse.y*(originalMediaHeight/canvas.height);//set point y to mouse.y
}
}
// - Draws temporary line from last point
function previewStroke(point){
ctx.beginPath();//start
ctx.lineWidth = 2;
ctx.strokeStyle = userShape.color;
ctx.moveTo(point.x*(canvas.width/originalMediaWidth),point.y*(canvas.height/originalMediaHeight));//last point
ctx.lineTo(mouse.x,mouse.y);//mouse pos
ctx.stroke();//draw
}
// - Draws a shape from an array of coordinate objetcs
function strokeShape(shape){
//more than 2+ points
if(shape.points.length > 1){
ctx.beginPath();//start
ctx.lineWidth = 2;
ctx.strokeStyle = userShape.color;
ctx.lineJoin = 'round';
//draw first point
ctx.moveTo(shape.points[0].x*(canvas.width/originalMediaWidth), shape.points[0].y*(canvas.height/originalMediaHeight));
//draw lines to other points
for(var i = 1; i < shape.points.length; i++){
ctx.lineTo(shape.points[i].x*(canvas.width/originalMediaWidth), shape.points[i].y*(canvas.height/originalMediaHeight));
}
//if shape finished
if(shape.finished){
ctx.closePath();//connect last points
ctx.stroke();//draw
}
//shape not finished
else
ctx.stroke();//finish drawing
}
}
//Check if over point
function mouseOnPoint(point){
//check if mouse is over point
if(Math.pow(mouse.x-point.x*(canvas.width/originalMediaWidth),2) + Math.pow(mouse.y-point.y*(canvas.height/originalMediaHeight),2) < Math.pow(mouse.radius,2)){
return true;
}
}
function highlightPoints(points, color, callback){
var pointIndex;//point sent to callback
//check every point
for(var i = 0; i < points.length; i++){
//draw blue circle over point
drawCircle(userShape.points[i].x*(canvas.width/originalMediaWidth), userShape.points[i].y*(canvas.height/originalMediaHeight), mouse.radius, 2, color, 'fill');
//check if mouse is over a point
if(mouseOnPoint(points[i])){
drawCircle(userShape.points[i].x*(canvas.width/originalMediaWidth), userShape.points[i].y*(canvas.height/originalMediaHeight), mouse.radius+1, 2, '#0000ff', 'stroke');//draw outline
pointIndex = i;//return array index
}
}
callback(pointIndex);//pass data to callback
}
// - Draw Circle Outline
function drawCircle(x,y,radius,lineWidth, color, drawType){
ctx.beginPath();
ctx.lineWidth = lineWidth;
ctx.style = color;
ctx.fillStyle = color;
ctx.arc(x,y,radius,0,2*Math.PI);//(x, y, radius, startAngle, endAngle
if(drawType === 'stroke')
ctx.stroke();
else if(drawType === 'fill')
ctx.fill();
}