-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
341 lines (341 loc) · 10.4 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
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
var canvas= $("#cvs").get(0);
$("#vid").toggle();
var video;
var tr = 0.5;
var mr = 0.5;
var fps = 60;
var frames = [];
window.duration = $("#vid").get(0).duration;
console.log("window duration is " + window.duration);
for (var i = 0; i < $("#vid").get(0).duration*fps; i++) {
frames.push([]);
}
window.objects = [];
var unSet = 0;
var clicked = 0;
canvas.width = $(window).width()*0.5;
canvas.height = $(window).height()*0.5;
console.log(canvas.width + " " + canvas.height);
var diva = $("#main").get(0);
console.log(diva.offsetWidth + " " + diva.offsetHeight);
var timeline = $("#timeline").get(0);
timeline.width=$(window).width()*2;
timeline.height = $(window).height()*0.5;
console.log("! " + timeline.width + " "+ timeline.height);
window.tl = [-1,-1];
window.move = [-1,-1];
var initialize = function() {
setTimeout(testDraw, 200);
$("#vid").on("seeked", drawBG);
$("#timeline").click(timelineClick);
$("#cvs").mousedown(md);
$("#cvs").mouseup(mu);
$("#cvs").mousemove(mv);
$("#addNew").click(addNew);
console.log("init");
};
var addNew = function() {
var objects = window.objects;
console.log(objects);
var name = prompt("Name your new annotation", "default");
var newtrack = track(name, objects.length);//, '#'+Math.floor(Math.random()*16777215).toString(16));
console.log("newtrack: " + newtrack);
console.log(newtrack.color);
objects.push(newtrack);
createDiv(newtrack);
}
var mu = function(e) {
var mp = mousePos($(this), e);
if (mp.Y > canvas.height*0.9) {
var result = prompt("Skip to frame:", Math.floor(video.currentTime*60))
video.currentTime = result/60;
}
var tl = window.tl;
move = [mp.X-tl.X, mp.Y-tl.Y];
if (move[0] < 0) {
tl.X += move[0];
move[0] *= -1;
}
if (move[1] < 0) {
tl.Y += move[1];
move[1] *= -1;
}
window.unSet = 1;
window.clicked = 0;
};
var md = function(e) {
var mp = mousePos($(this), e);
window.tl = mp;
window.clicked = 1;
};
var mv = function(e) {
console.log(window.clicked);
if (window.clicked == undefined || window.clicked == 0) {
return;
}
var mp = mousePos($(this), e);
drawBG();
console.log(window.tl);
var tl = window.tl;
console.log(tl);
var ctx = $("#cvs").get(0).getContext('2d');
ctx.beginPath();
ctx.globalAlpha = 0.2;
ctx.fillStyle = '#FF0000';
ctx.fillRect(tl.X, tl.Y, mp.X-tl.X, mp.Y-tl.Y);
ctx.globalAlpha = 1.0;
console.log(tl.X + " " + tl.Y + " " + mp.X + " " + mp.Y);
};
var drawVideo = function() {
var ctx = $("#cvs").get(0).getContext('2d');
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(video, 0,0, canvas.width, canvas.height*0.9);
ctx.fillStyle = '#000000';
ctx.fillText("Frame: " + Math.floor(video.currentTime*60), 0, canvas.height*0.9+10);
}
var drawBG = function() {
// Take the time and draw the image first
var ctx = $("#cvs").get(0).getContext('2d');
drawVideo();
var objects = window.objects;
// For all images, find the location and draw
var time = video.currentTime;
console.log("time is: " + time);
for (var i = 0; i < objects.length; i++) {
console.log(objects[i].annotations.length);
var count = 0;
while (count < objects[i].annotations.length && objects[i].annotations[count].time <= time) {
console.log("Times: " + objects[i].annotations[count].time);
count++;
}
count--;
var toDraw = [0,0,0,0];
if (count >= 0 && count == objects[i].annotations.length-1) {
toDraw = [objects[i].annotations[count].topleft.X, objects[i].annotations[count].topleft.Y, objects[i].annotations[count].width, objects[i].annotations[count].height]
} else if (count >= 0 && count < objects[i].annotations.length-1) {
//interpolate
var obj1 = objects[i].annotations[count];
var obj2 = objects[i].annotations[count+1];
var timeDiff = obj2.time-obj1.time;
var curTime = time-obj1.time;
var ratio = curTime/timeDiff;
toDraw[0] = obj1.topleft.X+(obj2.topleft.X-obj1.topleft.X)*ratio;
toDraw[1] = obj1.topleft.Y+(obj2.topleft.Y-obj1.topleft.Y)*ratio;
toDraw[2] = obj1.width+(obj2.width-obj1.width)*ratio;
toDraw[3] = obj1.height+(obj2.height-obj1.height)*ratio;
}
if (count >= 0) {
console.log(toDraw);
ctx.beginPath();
//ctx.globalAlpha = 0.2;
ctx.fillStyle = objects[i].color;
ctx.strokeStyle= objects[i].color;
console.log("Color: " + objects[i].color);
ctx.rect(toDraw[0], toDraw[1], toDraw[2], toDraw[3]);
ctx.globalAlpha = 1.0;
ctx.stroke();
console.log('drawing');
}
}
}
var createDiv = function(annot) {
var objects = window.objects;
var build = "<div class = \'annot\' id = id_" + annot.name + ">";
build += "<canvas id=cvs_"+annot.name+"></canvas>";
build += annot.name;
build += "</div>";
$("#annotationHolder").prepend(build);
var but = $('<button/>', {
text: annot.name,
click: function() {
if (window.unSet == 1) {
window.unSet = 0;
var tl = window.tl;
var move = window.move;
console.log(objects);
console.log(tl);
console.log(move);
var a = annot.index;
objects[annot.index].annotations;
addInArray(objects[annot.index].annotations, tl, move, $("#vid").get(0).currentTime);
var cvs = $("#cvs_"+annot.name).get(0);
cvs.getContext('2d').clearRect(0,0, cvs.width, cvs.height);
drawVideo();
cvs.getContext('2d').putImageData(canvas.getContext('2d').getImageData(tl.X,tl.Y,move[0],move[1]),0,0);
addToTimeline(annot, $("#vid").get(0).currentTime);
console.log(objects[annot.index].annotations);
alert(annot.name);
drawBG();
}
}
});
$("#id_"+annot.name).append(but);
$("#id_"+annot.name).css('background-color', '');
$("#id_"+annot.name).css('background-color', annot.color);
// console.log($("#annotationHolder").html());
// console.log($("#annotationHolder").html());
// console.log('appending');
}
var addToTimeline = function(annot, time) {
console.log("we should be drawing to the timeline now");
var ctx = $("#timeline").get(0).getContext('2d');
ctx.beginPath();
ctx.fillStyle = annot.color;
ctx.strokeStyle = annot.color;
console.log(annot.color);
var timeline = $("#timeline").get(0);
var duration = $("#vid").get(0).duration;
console.log(timeline.width + " " + time + " " + duration);
console.log(timeline.width*time/duration + " " + timeline.height);
ctx.fillRect(time/duration*timeline.width,timeline.height/2, 5, 5);
ctx.stroke();
}
var addInArray = function(arr, tl, move, time) {
var i = 0;
while (i < arr.length && arr[i].time < time) {
i++;
}
arr.splice(i, 0, annotation(time, tl, move[0], move[1], canvas.getContext('2d').getImageData(tl.X,tl.Y,move[0],move[1])));
for (var a = 0; a < arr.length; a++) {
console.log("ARRAY: " + arr[a].time);
}
console.log("ARRAY: " + arr.join());
}
var track = function(name, num) {
return {
'name':name.replace(" ", "_"),
'index': num,
'color': '#'+('000000'+Math.floor(Math.random()*16777215).toString(16)).slice(-6),
'annotations':[]
};
}
var annotation = function(time, topleft, width, height, img) {
return {
'time':time,
'topleft':topleft,
'width':width,
'height':height,
'image':img
};
}
var testDraw = function() {
video = $("#vid").get(0);
var ctx = canvas.getContext("2d");
//ctx.drawImage(video, 0,0, canvas.width, canvas.height);
drawVideo();
console.log(canvas.width + " " + canvas.height);
var ctx2 = timeline.getContext("2d");
ctx2.fillStyle = '#FF0000';
ctx2.fillRect(0,0, timeline.width, timeline.height);
console.log(timeline.width + " " + timeline.height);
ctx2.fillStyle = '#000000';
for (var i = 0; i < video.duration; i += 10) {
ctx2.beginPath();
ctx2.moveTo(timeline.width/video.duration*i, 0);
ctx2.lineTo(timeline.width/video.duration*i, timeline.height);
ctx2.stroke();
}
};
var timelineClick = function(e) {
video = $("#vid").get(0);
var mp = mousePos($(this), e);
console.log("scrollbar: " + $("#casing").get(0).scrollLeft)
var loc = mp.X/timeline.width*video.duration
+ $("#casing").get(0).scrollLeft/$(window).width()/2*video.duration;
video.currentTime = loc;
video.play();
video.pause();
console.log("time: " + video.currentTime);
// testDraw();
// var ctx = canvas.getContext("2d");
// ctx.drawImage(video, 0,0, canvas.width, canvas.height);
}
var mousePos = function(context,e) {
var parentOffset = context.parent().offset();
return {
'X': e.pageX-parentOffset.left,
'Y': e.pageY-parentOffset.top
};
}
// var object = function(name, time, imagedata) {
// return {
// 'name':name;
// 'times':[time];
// 'imagedata':imagedata;
// };
// };
// var rect = function(X, Y, W, H) {
// return {
// 'X':X;
// 'Y':Y;
// 'W':W;
// 'H':H;
// };
// };
$(initialize);
});
</script>
<style>
* { margin: 0; padding: 0;}
#main {
display: block;
width: 100vw;
height: 50vh;
overflow: hidden;
white-space: nowrap;
}
#annotationHolder {
float:right;
position:static;
display: inline;
width:50vw;
height:50vh;
overflow-y: scroll;
overflow-x:hidden;
}
#cvs {
display: inline;
float:left;
width:50vw;
height:50vh;
}
#objects {
width:50vw;
height:50vh;
}
#casing {
width:100vw;
height:50vh;
overflow-y:hidden;
overflow-x: scroll;
}
nav {
display: inline-block;
}
nav ul{height:200px; width:18%;}
nav ul{overflow:hidden; overflow-y:scroll;}
</style>
</head>
<body>
<!--
<button id="capture">Capture</button><br> -->
<video id="vid" src="test.mp4" controls>
</video>
<div id="main">
<canvas id="cvs">
</canvas>
<div id="annotationHolder">
<button id="addNew"> Add New Object </button>
</div>
</div>
<div id="casing">
<canvas id="timeline">
</canvas>
</div>
</body>
</html>