-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.html
362 lines (288 loc) · 11.9 KB
/
edit.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" >
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, width=device-width">
<title>HTML canvas video player</title>
<link rel="stylesheet" href="skyblue.css">
<link rel="stylesheet" href="demo/demo.css">
<link href="node_modules/video.js/dist/video-js.css" rel="stylesheet" />
<style>
</style>
</head>
<body>
<div class="cover" id="cover">
<div class="paint-pad" id="paint-pad"></div>
<div class="button-div" ><label class="label label-txt" id="label-text">Label Text:
<input class="form-control label-txt" id="label-txt-input" type="text" value="Demo Text"></label>
<hr class="label-txt">
<button class="btn btn-primary confirm-edit" id="confirm-edit" >Confirm</button>
<button class="btn btn-light cancel-edit" id="cancel-edit">Cancel</button></div>
</div>
<div class="header">
<div class="content">
<a href="https://github.com/wangchengyu/video_in_canvas" class="padding-right-20">GitHub</a>
<a href="https://github.com/wangchengyu/video_in_canvas/archive/master.zip">Download</a>
</div>
</div>
<div class="content">
<video id="video-1" class="video-js"
controls
preload="auto"
width="800"
height="450"
data-setup="{}" muted>
<source src="./w.mp4" type=video/mp4>
<!-- <source src=http://techslides.com/demos/sample-videos/small.ogv type=video/ogg>
<source src=http://techslides.com/demos/sample-videos/small.mp4 type=video/mp4>
<source src=http://techslides.com/demos/sample-videos/small.3gp type=video/3gp> -->
</video>
</div><!--div id="canvas-container" class="graph">123</div-->
<hr>
<div class="content" style="margin-bottom: 5px; padding-bottom: 0;">
<div class="keyframebar" id="kf_container" style="width: 100%;height:130px">
</div>
</div>
<div class="content elementbars" id="elementbars">
</div>
<div class="content">
<div class="row">
<button class="btn btn-primary dropbtn" style="font-size: 30px; min-width: 30px; margin-right: 20px;" onclick="myFunction()">
+
</button>
<div id="myDropdown" class="dropdown-content">
<a href="#" id="el-rect" class="el-menu" data-type="rect">Rect ☐</a>
<a href="#" id="el-arrows" class="el-menu" data-type="arrow">Arrow ⇨</a>
<a href="#" id="el-text" class="el-menu" data-type="label">Text</a>
</div>
</div>
</div>
<div class="content">
<p class="margin-top-50">
Released under MIT licence.
</p>
</div>
<script src="js/jquery.min.js" ></script>
<script src="js/canvas-video-player.js"></script>
<script src="js/jmgraph.js"></script>
<script src="node_modules/video.js/dist/video.js"></script>
<script src="js/keyframe.js"> </script>
<script src="js/el-edit-event.js"></script>
<!--script src="node_modules/videojs-overlay/dist/videojs-overlay.js"></script-->
<script >
const player = videojs('video-1', {controls: true});
const video = document.getElementById("video-1_html5_api");
const kfContainer = document.getElementById("kf_container");
const jmKeyFrame = jmGraph.create('kf_container', {
width: kfContainer.clientWidth,
height: kfContainer.clientHeight,
});
const kfCanvas = jmKeyFrame.canvas;
let jmPlayerCtx = null;
let jmPlayer = null;
const initPlayer = () => {
video.classList.add('vjs-hidden');
let container = document.getElementsByClassName("vjs-text-track-display")[0];
container.id = "vjs-text-track-display";
container.style.bottom = 0;
container.removeChild(container.firstChild);
jmPlayer = jmGraph.create('vjs-text-track-display', {
width: container.clientWidth,
height: container.clientHeight,
});
jmPlayerCtx = jmPlayer.canvas.getContext("2d");
jmPlayer.redraw(); //init paint pad
setOffset100ms(video.duration);
video.addEventListener("timeupdate", () => {
jmPlayer.needUpdate = true;
});
//start render
rafUpdate();
}
const kf = new KeyFrame(kfCanvas, video);
const elementList = [];
const paint = jmGraph.create('paint-pad', {
width: 800,
height: 450,
});
let elementIdIndex = 0;
const addNewElement = (type) => {
const id = elementIdIndex ++;
const elObject = buildShapeObject(type);
const elementDomId = "el_" + id;
const elHtmlDom = document.createElement("div");
elHtmlDom.id = elementDomId;
elHtmlDom.classList.add("elementbar");
elHtmlDom.innerHTML =
"<input type='checkbox' class='el_checkbox' alt='Visible or not' checked />" +
"<span class='icon-trash el-trash' title='delete'></span>" +
"<span class='icon-edit el-edit' title='edit'></span>";
document.getElementById("elementbars").appendChild(elHtmlDom);
let start = video.currentTime;
let end = video.currentTime + 2 > video.duration ? video.duration: video.currentTime + 2;
let left = start / video.duration * (VIDEO_WIDTH - 2);
let right = end / video.duration * (VIDEO_WIDTH -2);
const elJm = jmGraph.create(elementDomId, {
width: elHtmlDom.clientWidth, // this is the size of video
height: elHtmlDom.clientHeight,
});
let el = {
index: id, //unique id
type: type, //type: rect arrow
object: elObject, //shape obj
start_time: start, //start
end_time: end, //end
left: left,
right: right,
dom: elHtmlDom,
jm: elJm,
updated: true, //TODO: no modi, no update
check_visible: true
};
elObject.el = el;
elJm.canvas.el = el;
elJm.canvas.addEventListener("mousemove", el_mousemove);
elJm.canvas.addEventListener("mouseup", el_mouseup);
elJm.canvas.addEventListener("mousedown", el_mousedown);
jmPlayer.children.add(elObject);
elementList.push(el);
// visible
elHtmlDom.getElementsByTagName("input")[0].addEventListener("change", (e) => {
el.check_visible = e.target.checked;
});
// deleted
elHtmlDom.getElementsByClassName("el-trash")[0].addEventListener("click", (e) => {
// remove the dom node
el.object.remove();
el.dom.parentNode.removeChild(el.dom);
el.object = null;
el.jm = null;
el.dom = null;
for (let i = 0; i < elementList.length; i++) {
if (elementList[i].index === el.index) {
elementList.splice(i, 1);
break;
}
}
});
// editable
elHtmlDom.getElementsByClassName("el-edit")[0].addEventListener("click", (e) => {
video.pause();
// give el properties to rect
updateEditMode(el);
let body = document.getElementsByTagName("body")[0];
let cover = document.getElementById("cover");
let label = document.getElementById("label-text");
let labelTxt = document.getElementById("label-txt-input");
cover.style.height = body.clientHeight + "px";
cover.style.display = "block";
if (el.type === "label") {
label.style.display = "block";
labelTxt.value = el.object.text;
} else
label.style.display = "none";
});
};
const updateElement = (u_el) => {
let el_object = u_el.object;
if (u_el.type === 'rect') {
el_object.position.x = resizeRect.position.x;
el_object.position.y = resizeRect.position.y;
el_object.width = resizeRect.width;
el_object.height = resizeRect.height;
}
if (u_el.type === 'arrow') {
el_object.start.x = arrow.start.x;
el_object.start.y = arrow.start.y;
el_object.end.x = arrow.end.x;
el_object.end.y = arrow.end.y;
}
if (u_el.type === "label") {
el_object.text = label.text;
el_object.center = Object.assign({}, label.center);
el_object.style = Object.assign({}, label.style);
}
}
const exitEditMode = (state, type, u_el) => {
document.getElementById("cover").style.display = "none";
if (state === "add")
addNewElement(type);
if (state === "update") {
updateElement(u_el);
}
}
document.getElementById("confirm-edit").addEventListener("click", buttonConfirmClick);
document.getElementById("cancel-edit").addEventListener("click", buttonCancelClick);
document.getElementById("label-txt-input").addEventListener("keyup", labelTextChange);
setJmObject(paint, video, exitEditMode);
let menu = document.getElementsByClassName("el-menu");
for (let i = 0; i < menu.length; i++) {
menu[i].addEventListener("click", (event)=>{
// go to edit mode
let body = document.getElementsByTagName("body")[0];
let cover = document.getElementById("cover");
cover.style.height = body.clientHeight + "px";
cover.style.display = "block";
let label = document.getElementById("label-text");
let labelTxt = document.getElementById("label-txt-input");
let type = event.target.getAttribute("data-type");
if (type === "label") {
label.style.display = "block";
labelTxt.value = "Demo Text";
} else
label.style.display = "none";
initEditMode(type);
});
}
// animation loop
const rafUpdate = function() {
if (jmPlayerCtx && jmPlayer.needUpdate)
jmPlayerCtx.drawImage(video, 0,0, jmPlayerCtx.canvas.width, jmPlayerCtx.canvas.height);
if (jmPlayer && jmPlayer.children && jmPlayer.children.length) {
jmPlayer.children.each((i, item) => {
item.visible = item.el.check_visible && (video.currentTime >= item.el.start_time && video.currentTime <= item.el.end_time);
if (item.visible)
item.paint();
})
}
if (!video.paused)
kf.updated = true;
if (kf.updated) {
kf.refreshBackground();
kf.fillAllTimeLabel();
kf.fillAllPreviewImages();
kf.drawTimeLine();
kf.drawFloatLine();
kf.setFloatPreviewPosition();
kf.updated = false;
}
if (elementList.length > 0) {
for (var i = 0; i < elementList.length; i++) {
let el = elementList[i];
if (!el.updated)
continue;
let el_ctx = drawElement(el);
kf.drawFloatLine(el_ctx, true);
}
}
requestAnimationFrame(rafUpdate);
};
/* When the user clicks on the button, toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").style.display = "inline";
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
openDropdown.style.display = "none";
}
}
}
</script>
</body>
</html>