forked from kiriapurv/node-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
188 lines (161 loc) · 5.16 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
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style>
div#container {
width: 640px;
height: 360px;
box-shadow: 1px 1px 100px #777;
}
div#heading {
padding: 10px 20px 10px 20px;
background-color: #111;
text-align: right;
}
button {
background: rgba(0, 0, 0, 0);
color: #FFF;
border-style: none;
}
div#captures {
height: 100%;
right: 0px;
top: 0px;
background-color: #CCC;
width: 300px;
position: absolute;
overflow-y: scroll;
}
div.captured-frame {
width: 250px;
height: 140px;
margin: 10px 25px 10px 25px;
box-shadow: 0px 0px 50px #111;
}
div.captured-frame img {
width: 250px;
height: 140px;
background-color: #444;
border-style: none;
}
div.captured-frame .frame-title {
width: 210px;
height: 20px;
padding: 10px 20px 10px 20px;
background: #111;
position: relative;
bottom: 40px;
color: rgba(0, 0, 0, 0.5);
}
a {
text-decoration: none;
color: #FFF;
font-size: 12px;
}
</style>
</head>
<body>
<div id="container">
<div id="heading">
<button id="capture" onclick="capture();">
✧
</button>
<button id="play" onclick="openCamera();">
►
</button>
<button id="pause" onclick="closeCamera();">
❚❚
</button>
</div>
<canvas id="stream" style="resize:both;"></canvas>
</div>
<div id="captures">
</div>
<a id="acapture" href="" download="" style="display : none"></a>
</body>
<script type="text/javascript">
var ws = null;
var stream = document.getElementById("stream");
var container = document.getElementById("container");
var context = stream.getContext("2d");
var image = null;
var imageEle = "<div id=\"##id\" class=\"captured-frame\"><img class=\"frame-image\" src=\"##src\" /><div class=\"frame-title\"><a href=\"##href\" download=\"##title\">##title</a></div></div>";
function adjustSize(width, height) {
stream.width = width;
stream.height = height;
stream.style.width = width + "px";
stream.style.height = height + "px";
container.style.width = width + "px";
container.style.height = height + "px";
adjustPos();
};
function adjustPos() {
//Adjust player
$("#container").css("margin-top", function (index) {
return ($("window").height() / 2) + ($("#container").height() / 2);
});
// $("#container").css("margin-right", function (index) {
// return ($("document").width() - $("#captures").width()) / 2 + ($("#container").width() / 2);
// });
$("#container").css("margin-left", function (index) {
return ((window.innerWidth) - $("#captures").width() - ($("#container").width())) / 2;
});
};
$(document).ready(function () {
adjustPos();
$("#pause").hide();
});
//FPS Calculation
var currentFps = 0,
fps = 0;
function drawFrame(image) {
var img = new Image();
img.onload = function () {
context.drawImage(img, 0, 0);
};
img.src = image;
};
function closeCamera() {
$("#play").show();
$("#pause").hide();
ws.send("close");
};
function openCamera() {
ws = new WebSocket("ws://" + window.location.host.split(":")[0] + ":##webSocketPort");
var sizeReceived = false;
ws.onopen = function () {
ws.send("open");
};
ws.onerror = function (e) {
console.log(e);
};
ws.onmessage = function (message) {
var data = JSON.parse(message.data);
switch (data.type) {
case "size":
adjustSize(data.width, data.height);
break;
case "frame":
if (!sizeReceived) {
sizeReceived = true;
ws.send("size");
}
image = "data:image/jpg;base64," + data.frame;
drawFrame(image);
break;
}
};
$("#play").hide();
$("#pause").show();
};
function capture() {
var cap = document.getElementById("acapture");
cap.href = image;
cap.download = "" + new Date().getTime() + ".jpg";
$("#acapture").click();
var id = "" + new Date().getTime();
var capImage = imageEle.replace("##id", id).replace("##src", image).replace("##href", image).replace(/##title/g, "" + id + ".jpg");
$("#captures").prepend(capImage);
};
</script>
</html>