-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
257 lines (200 loc) · 8.51 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
<!doctype html>
<html ng-app="meanchat">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.4/angular.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<link rel="stylesheet" href="/styles/index.css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
<title>Meanchat</title>
<script type="text/javascript">
//camera stuff
window.addEventListener("DOMContentLoaded", function() {
// Grab elements, create settings, etc.
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
video = document.getElementById("video"),
videoObj = { "video": true },
errBack = function(error) {
console.log("Video capture error: ", error.code);
};
// Put video listeners into place
if(navigator.getUserMedia) { // Standard
navigator.getUserMedia(videoObj, function(stream) {
video.src = stream;
video.play();
}, errBack);
} else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
navigator.webkitGetUserMedia(videoObj, function(stream){
video.src = window.webkitURL.createObjectURL(stream);
video.play();
}, errBack);
}
else if(navigator.mozGetUserMedia) { // Firefox-prefixed
navigator.mozGetUserMedia(videoObj, function(stream){
video.src = window.URL.createObjectURL(stream);
video.play();
}, errBack);
}
}, false);
</script>
</head>
<body>
<nav id="top-bar" class="navbar navbar-fixed-top" role="navigation">
<ul class="nav navbar-nav navbar-left">
<li><a class="navbar-brand" href="#">meanChat</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><input id="username" class="well-sm" type="text" placeholder="Anonymous" autocomplete="off"></li>
</ul>
</nav>
<ul class="messages"></ul>
<div class="col-lg-12" id="bottom-bar">
<span>
<video id="video" autoplay></video>
<button id="shutter">Take Photo</button>
<canvas id="canvas" style="display:none" width="160" height="120"></canvas>
</span>
<form class="input-group" id="msg-form" action="">
<input type="text" class="form-control well well-sm" id="msg-text" autocomplete="off" placeholder="Say something!">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Send</button>
</span>
</form><!-- /input-group -->
</div><!-- /.col-lg-12 -->
<!--
<video id="video" width="160" height="120" autoplay></video>
<button id="shutter">Snap Photo</button>
<button id="videoShutter">Take GIF</button>
<canvas id="canvas" style="display:none" width="160" height="120"></canvas>
<video id="vid" controls loop>
<source src=video.webm type=video/webm>
<source src=video.ogg type=video/ogg>
<source src=video.mp4 type=video/mp4>
</video>
-->
<script>
var socket = io();
var scrollHeight = 0; //keeps track of how tall total messages are to know where to scroll to
//adding margin so messages don't go behind bottom bar
$('.messages').css("margin-bottom",$('#video').height());
// Send chat message
$('div').submit(function(){
var message = {
'sender': $('#username').val(),
'text': $('#msg-text').val()
};
if (message['sender'] === "")
message['sender'] = "Anonymous";
socket.emit('chat message', message);
$('#msg-text').val('');
return false;
});
// Send photo message
$('#shutter').click(function() {
var canvas = document.getElementById("canvas"), context = canvas.getContext("2d");
context.drawImage(video, 0, 0, 160, 120);
var image = convertCanvasToImage(canvas);
image.crossOrigin = "Anonymous";
var message = {
'sender': $('#username').val(),
'image': image['src'].toString('base64')
};
if (message['sender'] === "")
message['sender'] = "Anonymous";
socket.emit('image message', message);
return false;
});
// Add new chat messages to the screen
socket.on('chat message', function(message){
var sender = message['sender'].concat(': ');
var msgString = message['text'];
$('.messages').append($('<li class="text" style="font-weight:bold">').text(sender));
$('.messages > li:last-child').append($('<span style="font-weight:normal">').text(msgString));
$('.messages > li:last-child').append($('<style="float: right" class="timestamp">').text(message['time']));
//make height match message (10px is padding on message)
$('.messages > li:last-child').css("height", ($('.messages > li:last-child').height()));
//moving body to scrollHeight
scrollHeight+=($('.messages > li:last-child').height());
$('body').scrollTop(scrollHeight);
});
// Add new image messages to the screen
socket.on('image message', function(message){
var canvas = document.getElementById("canvas"), context = canvas.getContext("2d");
var sender = message['sender'].concat(': ');
var img = new Image();
img.src = message['image'];
context.drawImage(img, 200, 200);
$('.messages').append($('<li class="text" style="font-weight:bold">').text(sender));
$('.messages > li:last-child').append(img);
//$('.messages > li:last-child').append($('<span style="font-weight:normal">').text(img.src));
$('.messages > li:last-child').append($('<style="float: right" class="timestamp">').text(message['time']));
//make height match message (10px is padding on message)
$('.messages > li:last-child').css("height", ($('.messages > li:last-child').height()));
scrollHeight+=($('.messages > li').last().height());
$('body').scrollTop(scrollHeight);
});
//db stuff
// mongo.connect('mongodb://127.0.0.1:27017/mydb', function (err, db) {
// var collection = db.collection('meanchat');
// collection.insert({ content: msg }, function (err, o) {
// if (err) { console.warn(err.message); }
// else { console.log("chat message inserted into db: " + msg); }
// });
// });
//video stuff
// $('#videoShutter').click(function(){
// var v = document.getElementById('vid');
// var canvas = document.getElementById('canvas');
// var context = canvas.getContext('2d');
// var cw = Math.floor(canvas.clientWidth / 100);
// var ch = Math.floor(canvas.clientHeight / 100);
// canvas.width = cw;
// canvas.height = ch;
// v.addEventListener('play', function(){
// draw(this,context,cw,ch);
// },false);
// });
// function draw(v,c,w,h) {
// if(v.paused || v.ended) return false;
// v.drawImage(c,0,0,w,h);
// setTimeout(draw,20,v,c,w,h);
// }
// Converts canvas to an image
function convertCanvasToImage(canvas) {
var image = new Image();
image.src = canvas.toDataURL("image/png");
return image;
}
// var shots = [];
// var numShots = 10;
// var rate = 100; //in milliseconds
// var count = 0;
// function getGIF() {
// for(var i=0; i<shots.length; i++) {
// }
// }
// var canvas = document.getElementById("canvas");
// var shots = [];
// var grabLimit = 10; // Number of screenshots to take
// var grabRate = 100; // Miliseconds. 500 = half a second
// var count = 0;
// function showResults() {
// //console.log(shots);
// for (var i=0; i<shots.length; i++) {
// document.write('<img src="' + shots[i] + '"/>\n');
// }
// }
// var grabber = setInterval(function(){
// count++;
// if (count>grabLimit) {
// clearInterval(grabber);
// showResults();
// }
// var img = canvas.toDataURL("image/png");
// shots.push(img);
// }, grabRate);
</script>
</body>
</html>