sb.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. var DEFAULT_DELAY = 20;
  2. var Countdown = {
  3. _timeout: null,
  4. _stepCb: null,
  5. _timeoutCb: null,
  6. running: false,
  7. seconds: DEFAULT_DELAY,
  8. _initial_seconds: DEFAULT_DELAY,
  9. start: function(seconds, timeoutCb, stepCb) {
  10. Countdown.stop();
  11. Countdown.seconds = Countdown._initial_seconds = seconds || DEFAULT_DELAY;
  12. Countdown._timeoutCb = timeoutCb || Countdown._timeoutCb;
  13. Countdown._stepCb = stepCb || Countdown._stepCb;
  14. Countdown.running = true;
  15. Countdown._step();
  16. },
  17. stop: function() {
  18. if (Countdown._timeout) {
  19. window.clearTimeout(Countdown._timeout);
  20. }
  21. Countdown.running = false;
  22. },
  23. restart: function() {
  24. Countdown.start(Countdown._initial_seconds);
  25. },
  26. _step: function() {
  27. if (Countdown._stepCb) {
  28. Countdown._stepCb();
  29. }
  30. if (Countdown.seconds === 0) {
  31. if (Countdown._timeoutCb) {
  32. Countdown._timeoutCb();
  33. }
  34. Countdown.stop();
  35. } else {
  36. Countdown._decrement();
  37. }
  38. },
  39. _decrement: function() {
  40. Countdown.seconds = Countdown.seconds - 1;
  41. Countdown._timeout = window.setTimeout(function() {
  42. Countdown._step();
  43. }, 1000);
  44. }
  45. };
  46. function uuidv4() {
  47. return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
  48. (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  49. );
  50. }
  51. var UUID = uuidv4();
  52. function getWSPath() {
  53. var loc = window.location, new_uri;
  54. if (loc.protocol === "https:") {
  55. new_uri = "wss:";
  56. } else {
  57. new_uri = "ws:";
  58. }
  59. new_uri += "//" + loc.host + "/ws?uuid=" + UUID;
  60. return new_uri;
  61. }
  62. var WS = new WebSocket(getWSPath());
  63. WS.onerror = function(error) {
  64. console.log('WebSocket Error ' + error);
  65. };
  66. WS.onmessage = function(e) {
  67. console.log("received message on websocket: " + e.data);
  68. var jdata = JSON.parse(e.data);
  69. if (!(jdata && jdata.source == "button" && jdata.action == "clicked")) {
  70. return;
  71. }
  72. if (!Countdown.running) {
  73. takePhoto("press again to publish!");
  74. } else {
  75. sendPhoto();
  76. }
  77. };
  78. function runCamera(stream) {
  79. console.log("initialize the camera");
  80. var video = document.querySelector("#sb-video");
  81. var container = document.querySelector("#canvas-container");
  82. container.width = video.videoWidth;
  83. video.onloadedmetadata = function() {
  84. video.play();
  85. };
  86. video.srcObject = stream;
  87. resizeCanvas(video, 'sb-canvas');
  88. }
  89. function sendData(data) {
  90. var formData = new FormData();
  91. var msg = "";
  92. formData.append("selfie", new Blob([data]), "selfie.jpeg");
  93. fetch("/publish/", {
  94. method: "POST",
  95. body: formData
  96. }).then(function(response) {
  97. if (response.status !== 200) {
  98. msg = response.status;
  99. console.log(msg);
  100. iziToast.error({
  101. "title": "😭 something wrong sending the data 😭",
  102. "target": "#izi-container",
  103. "message": msg,
  104. "titleSize": "3em",
  105. "messageSize": "2em",
  106. "close": false,
  107. "drag": false,
  108. "pauseOnHover": false,
  109. "progressBarColor": "red",
  110. "position": "topCenter"
  111. });
  112. }
  113. cancelPhoto();
  114. return response.json();
  115. }).then(function(json) {
  116. json = json || {};
  117. console.log(json);
  118. if (json && json.success) {
  119. msg = "❤ ❤ ❤ photo sent successfully! ❤ ❤ ❤";
  120. console.log(msg);
  121. iziToast.destroy();
  122. iziToast.success({
  123. "title": msg,
  124. "target": "#izi-container",
  125. "titleSize": "3em",
  126. "messageSize": "2em",
  127. "close": false,
  128. "drag": false,
  129. "pauseOnHover": false,
  130. "progressBarColor": "red",
  131. "position": "topCenter"
  132. });
  133. } else {
  134. msg = json.message;
  135. console.log(msg);
  136. iziToast.error({
  137. "title": "😭 backend error 😭",
  138. "target": "#izi-container",
  139. "message": msg,
  140. "titleSize": "3em",
  141. "messageSize": "2em",
  142. "close": false,
  143. "drag": false,
  144. "pauseOnHover": false,
  145. "progressBarColor": "red",
  146. "position": "topCenter"
  147. });
  148. }
  149. }).catch(function(err) {
  150. console.log(msg);
  151. iziToast.error({
  152. "title": "😭 error connecting to the server 😭",
  153. "target": "#izi-container",
  154. "message": err,
  155. "titleSize": "3em",
  156. "messageSize": "2em",
  157. "close": false,
  158. "drag": false,
  159. "pauseOnHover": false,
  160. "progressBarColor": "red",
  161. "position": "topCenter"
  162. });
  163. cancelPhoto();
  164. });
  165. }
  166. function cancelPhoto() {
  167. console.log("cancel photo");
  168. document.querySelector("#send-photo-btn").classList.add("disabled");
  169. document.querySelector("#cancel-photo-btn").classList.add("disabled");
  170. var canvas = document.querySelector("#sb-canvas");
  171. var context = canvas.getContext("2d");
  172. context.clearRect(0, 0, canvas.width, canvas.height);
  173. Countdown.stop();
  174. iziToast.destroy();
  175. }
  176. function isBlank(canvas) {
  177. var blank = document.createElement("canvas");
  178. blank.width = canvas.width;
  179. blank.height = canvas.height;
  180. return canvas.toDataURL() == blank.toDataURL();
  181. }
  182. function sendPhoto() {
  183. console.log("send photo");
  184. Countdown.stop();
  185. var canvas = document.querySelector("#sb-canvas");
  186. if (!canvas || isBlank(canvas)) {
  187. var msg = "I cowardly refuse to send a blank image.";
  188. console.log(msg);
  189. iziToast.warning({
  190. "title": msg,
  191. "target": "#izi-container",
  192. "titleSize": "3em",
  193. "messageSize": "2em",
  194. "close": false,
  195. "drag": false,
  196. "pauseOnHover": false,
  197. "progressBarColor": "red",
  198. "position": "topCenter"
  199. });
  200. return;
  201. }
  202. return sendData(canvas.toDataURL("image/jpeg"));
  203. }
  204. function _takePhoto(message) {
  205. console.log("take photo");
  206. var video = document.querySelector("#sb-video");
  207. if (!(video.offsetWidth && video.offsetHeight)) {
  208. var msg = "missing video";
  209. console.log(msg);
  210. iziToast.warning({
  211. "title": msg,
  212. "target": "#izi-container",
  213. "message": "please grant camera permissions",
  214. "titleSize": "3em",
  215. "messageSize": "2em",
  216. "close": false,
  217. "drag": false,
  218. "pauseOnHover": false,
  219. "progressBarColor": "red",
  220. "position": "topCenter"
  221. });
  222. return;
  223. }
  224. var canvas = document.querySelector("#sb-canvas");
  225. var context = canvas.getContext("2d");
  226. canvas.width = video.offsetWidth;
  227. canvas.height = video.offsetHeight;
  228. /*
  229. var tmpCanvas = document.createElement("canvas");
  230. tmpCanvas.width = video.offsetWidth;
  231. tmpCanvas.height = video.offsetHeight;
  232. var tmpContext = tmpCanvas.getContext("2d");
  233. var tmpRatio = (tmpCanvas.height / tmpCanvas.width);
  234. tmpContext.drawImage(video, 0, 0, video.offsetWidth, video.offsetHeight);
  235. canvas.width = canvas.offsetWidth;
  236. canvas.height = canvas.offsetHeight;
  237. canvas.style.height = parseInt(canvas.offsetWidth * tmpRatio);
  238. var scale = canvas.width / tmpCanvas.width;
  239. context.scale(scale, scale);
  240. context.drawImage(tmpCanvas, 0, 0);
  241. */
  242. context.drawImage(video, 0, 0, video.offsetWidth, video.offsetHeight);
  243. document.querySelector("#send-photo-btn").classList.remove("disabled");
  244. document.querySelector("#cancel-photo-btn").classList.remove("disabled");
  245. iziToast.question({
  246. "title": "do you like it?",
  247. "timeout": DEFAULT_DELAY * 1000,
  248. "target": "#izi-container",
  249. "message": message || "press \"share photo\" to publish!",
  250. "titleSize": "3em",
  251. "messageSize": "2em",
  252. "close": false,
  253. "drag": false,
  254. "pauseOnHover": false,
  255. "progressBarColor": "red",
  256. "position": "topCenter"
  257. });
  258. Countdown.start(DEFAULT_DELAY, cancelPhoto);
  259. }
  260. function takePhoto(msg) {
  261. window.setTimeout(function() { _takePhoto(msg); }, 1000);
  262. }
  263. function initCamera() {
  264. console.log("request camera permission");
  265. document.querySelector("#canvas-container").style.display = "block";
  266. var videoObj = {
  267. "video": {
  268. width: 800,
  269. height: 600
  270. },
  271. "audio": false
  272. };
  273. navigator.mediaDevices.getUserMedia(videoObj).then(function(stream) {
  274. runCamera(stream);
  275. document.querySelector("#init-btn").style.display = "none";
  276. document.querySelector("#take-photo-btn").classList.remove("disabled");
  277. }).catch(function(err) {
  278. console.log("unable to open camera");
  279. console.log(err);
  280. iziToast.error({
  281. "title": "unable to open camera",
  282. "message": "please reload this page: " + err,
  283. "titleSize": "3em",
  284. "messageSize": "2em",
  285. "close": false,
  286. "drag": false,
  287. "pauseOnHover": false,
  288. "progressBarColor": "red",
  289. "position": "topCenter"
  290. });
  291. });
  292. }
  293. function resizeCanvas(el, canvasID) {
  294. var canvas = document.getElementById(canvasID);
  295. canvas.width = el.offsetWidth;
  296. canvas.height = el.offsetHeighth;
  297. }