sb.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. var countdown = {
  2. _timeout: null,
  3. _stepCb: null,
  4. _timeoutCb: null,
  5. running: false,
  6. seconds: 5,
  7. _initial_seconds: 5,
  8. start: function(seconds, timeoutCb, stepCb) {
  9. countdown.stop();
  10. countdown.seconds = countdown._initial_seconds = seconds || 5;
  11. countdown._timeoutCb = timeoutCb || countdown._timeoutCb;
  12. countdown._stepCb = stepCb || countdown._stepCb;
  13. countdown.running = true;
  14. countdown._step();
  15. },
  16. stop: function() {
  17. if (countdown._timeout) {
  18. window.clearTimeout(countdown._timeout);
  19. }
  20. countdown.running = false;
  21. },
  22. restart: function() {
  23. countdown.start(countdown._initial_seconds);
  24. },
  25. _step: function() {
  26. if (countdown._stepCb) {
  27. countdown._stepCb();
  28. }
  29. if (countdown.seconds === 0) {
  30. if (countdown._timeoutCb) {
  31. countdown._timeoutCb();
  32. }
  33. countdown.stop();
  34. } else {
  35. countdown._decrement();
  36. }
  37. },
  38. _decrement: function() {
  39. countdown.seconds = countdown.seconds - 1;
  40. countdown._timeout = window.setTimeout(function() {
  41. countdown._step();
  42. }, 1000);
  43. }
  44. };
  45. function runCamera(stream) {
  46. console.log("initialize the camera");
  47. var video = document.querySelector("video");
  48. video.width = video.offsetWidth;
  49. video.onloadedmetadata = function() {
  50. video.play();
  51. };
  52. video.srcObject = stream;
  53. }
  54. function sendData(data) {
  55. var xhr = new XMLHttpRequest();
  56. var boundary = "youarenotsupposedtolookatthis";
  57. var formData = new FormData();
  58. formData.append("selfie", new Blob([data]), "selfie.jpeg");
  59. fetch("/publish/", {
  60. method: "POST",
  61. body: formData
  62. }).then(function(response) {
  63. if (response.status !== 200) {
  64. console.log("something went wrong sending the data: " + response.status);
  65. } else {
  66. console.log("photo was sent successfully");
  67. }
  68. cancelPhoto();
  69. }).catch(function(err) {
  70. console.log("something went wrong connecting to server: " + err);
  71. cancelPhoto();
  72. });
  73. }
  74. function cancelPhoto() {
  75. console.log("cancel photo");
  76. var canvas = document.querySelector("canvas");
  77. var context = canvas.getContext("2d");
  78. context.clearRect(0, 0, canvas.width, canvas.height);
  79. countdown.stop();
  80. }
  81. function updateSendCountdown() {
  82. console.log("deleting photo in " + countdown.seconds + " seconds");
  83. }
  84. function isBlank(canvas) {
  85. var blank = document.createElement("canvas");
  86. blank.width = canvas.width;
  87. blank.height = canvas.height;
  88. return canvas.toDataURL() == blank.toDataURL();
  89. }
  90. function sendPhoto() {
  91. console.log("send photo");
  92. countdown.stop();
  93. var canvas = document.querySelector("canvas");
  94. if (isBlank(canvas)) {
  95. console.log("cowardly refuse to send a blank image.")
  96. return;
  97. }
  98. return sendData(canvas.toDataURL("image/jpeg"));
  99. }
  100. function takePhoto() {
  101. console.log("take photo");
  102. var video = document.querySelector("video");
  103. var canvas = document.querySelector("canvas");
  104. var tmpCanvas = document.createElement("canvas");
  105. tmpCanvas.width = video.offsetWidth;
  106. tmpCanvas.height = video.offsetHeight;
  107. var tmpContext = tmpCanvas.getContext("2d");
  108. var tmpRatio = (tmpCanvas.height / tmpCanvas.width);
  109. tmpContext.drawImage(video, 0, 0, video.offsetWidth, video.offsetHeight);
  110. canvas.width = canvas.offsetWidth;
  111. canvas.height = canvas.offsetHeight;
  112. canvas.style.height = parseInt(canvas.offsetWidth * tmpRatio);
  113. var context = canvas.getContext("2d");
  114. var scale = canvas.width / tmpCanvas.width;
  115. context.scale(scale, scale);
  116. context.drawImage(tmpCanvas, 0, 0);
  117. countdown.start(5, cancelPhoto, updateSendCountdown);
  118. }
  119. function initCamera() {
  120. console.log("request camera permission");
  121. var videoObj = {
  122. "video": {
  123. width: 800,
  124. height: 600
  125. },
  126. "audio": false
  127. };
  128. navigator.mediaDevices.getUserMedia(videoObj).then(function(stream) {
  129. runCamera(stream);
  130. }).catch(function(err) {
  131. console.log("unable to open camera");
  132. console.log(err);
  133. });
  134. }