sb.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. var DEFAULT_DELAY = 7;
  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. "message": msg,
  103. "titleSize": "3em",
  104. "messageSize": "2em",
  105. "close": false,
  106. "drag": false,
  107. "pauseOnHover": false,
  108. "position": "topCenter"
  109. });
  110. }
  111. cancelPhoto();
  112. return response.json();
  113. }).then(function(json) {
  114. json = json || {};
  115. console.log(json);
  116. if (json && json.success) {
  117. msg = "❤ ❤ ❤ photo sent successfully! ❤ ❤ ❤";
  118. console.log(msg);
  119. iziToast.destroy();
  120. iziToast.success({
  121. "title": msg,
  122. "titleSize": "3em",
  123. "messageSize": "2em",
  124. "close": false,
  125. "drag": false,
  126. "pauseOnHover": false,
  127. "position": "topCenter"
  128. });
  129. } else {
  130. msg = json.message;
  131. console.log(msg);
  132. iziToast.error({
  133. "title": "😭 backend error 😭",
  134. "message": msg,
  135. "titleSize": "3em",
  136. "messageSize": "2em",
  137. "close": false,
  138. "drag": false,
  139. "pauseOnHover": false,
  140. "position": "topCenter"
  141. });
  142. }
  143. }).catch(function(err) {
  144. console.log(msg);
  145. iziToast.error({
  146. "title": "😭 error connecting to the server 😭",
  147. "message": err,
  148. "titleSize": "3em",
  149. "messageSize": "2em",
  150. "close": false,
  151. "drag": false,
  152. "pauseOnHover": false,
  153. "position": "topCenter"
  154. });
  155. cancelPhoto();
  156. });
  157. }
  158. function cancelPhoto() {
  159. console.log("cancel photo");
  160. document.querySelector("#send-photo-btn").classList.add("disabled");
  161. document.querySelector("#cancel-photo-btn").classList.add("disabled");
  162. var canvas = document.querySelector("#sb-canvas");
  163. var context = canvas.getContext("2d");
  164. context.clearRect(0, 0, canvas.width, canvas.height);
  165. Countdown.stop();
  166. }
  167. function isBlank(canvas) {
  168. var blank = document.createElement("canvas");
  169. blank.width = canvas.width;
  170. blank.height = canvas.height;
  171. return canvas.toDataURL() == blank.toDataURL();
  172. }
  173. function sendPhoto() {
  174. console.log("send photo");
  175. Countdown.stop();
  176. var canvas = document.querySelector("#sb-canvas");
  177. if (!canvas || isBlank(canvas)) {
  178. var msg = "I cowardly refuse to send a blank image.";
  179. console.log(msg);
  180. iziToast.warning({
  181. "title": msg,
  182. "titleSize": "3em",
  183. "messageSize": "2em",
  184. "close": false,
  185. "drag": false,
  186. "pauseOnHover": false,
  187. "position": "topCenter"
  188. });
  189. return;
  190. }
  191. return sendData(canvas.toDataURL("image/jpeg"));
  192. }
  193. function _takePhoto(message) {
  194. console.log("take photo");
  195. var video = document.querySelector("#sb-video");
  196. if (!(video.offsetWidth && video.offsetHeight)) {
  197. var msg = "missing video";
  198. console.log(msg);
  199. iziToast.warning({
  200. "title": msg,
  201. "message": "please grant camera permissions",
  202. "titleSize": "3em",
  203. "messageSize": "2em",
  204. "close": false,
  205. "drag": false,
  206. "pauseOnHover": false,
  207. "position": "topCenter"
  208. });
  209. return;
  210. }
  211. var canvas = document.querySelector("#sb-canvas");
  212. var context = canvas.getContext("2d");
  213. canvas.width = video.offsetWidth;
  214. canvas.height = video.offsetHeight;
  215. /*
  216. var tmpCanvas = document.createElement("canvas");
  217. tmpCanvas.width = video.offsetWidth;
  218. tmpCanvas.height = video.offsetHeight;
  219. var tmpContext = tmpCanvas.getContext("2d");
  220. var tmpRatio = (tmpCanvas.height / tmpCanvas.width);
  221. tmpContext.drawImage(video, 0, 0, video.offsetWidth, video.offsetHeight);
  222. canvas.width = canvas.offsetWidth;
  223. canvas.height = canvas.offsetHeight;
  224. canvas.style.height = parseInt(canvas.offsetWidth * tmpRatio);
  225. var scale = canvas.width / tmpCanvas.width;
  226. context.scale(scale, scale);
  227. context.drawImage(tmpCanvas, 0, 0);
  228. */
  229. context.drawImage(video, 0, 0, video.offsetWidth, video.offsetHeight);
  230. document.querySelector("#send-photo-btn").classList.remove("disabled");
  231. document.querySelector("#cancel-photo-btn").classList.remove("disabled");
  232. iziToast.question({
  233. "title": "do you like what you see?",
  234. "message": message || "press \"share photo\" to publish!",
  235. "titleSize": "3em",
  236. "messageSize": "2em",
  237. "close": false,
  238. "drag": false,
  239. "pauseOnHover": false,
  240. "position": "topCenter"
  241. });
  242. Countdown.start(5, cancelPhoto);
  243. }
  244. function takePhoto(msg) {
  245. window.setTimeout(function() { _takePhoto(msg); }, 1000);
  246. }
  247. function initCamera() {
  248. console.log("request camera permission");
  249. document.querySelector("#canvas-container").style.display = "block";
  250. var videoObj = {
  251. "video": {
  252. width: 800,
  253. height: 600
  254. },
  255. "audio": false
  256. };
  257. navigator.mediaDevices.getUserMedia(videoObj).then(function(stream) {
  258. runCamera(stream);
  259. document.querySelector("#init-btn").style.display = "none";
  260. document.querySelector("#take-photo-btn").classList.remove("disabled");
  261. }).catch(function(err) {
  262. console.log("unable to open camera");
  263. console.log(err);
  264. iziToast.error({
  265. "title": "unable to open camera",
  266. "message": "please reload this page: " + err,
  267. "titleSize": "3em",
  268. "messageSize": "2em",
  269. "close": false,
  270. "drag": false,
  271. "pauseOnHover": false,
  272. "position": "topCenter"
  273. });
  274. });
  275. }
  276. function resizeCanvas(el, canvasID) {
  277. var canvas = document.getElementById(canvasID);
  278. canvas.width = el.offsetWidth;
  279. canvas.height = el.offsetHeighth;
  280. }