Advertisement
Guest User

JS Night Vision Webcam W\ Rainbow Squares FX SRC

a guest
Jan 19th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <style>
  2. canvas{
  3.  
  4. position: relative;
  5. width: 500px;
  6. height: 500px;
  7. overflow: hidden;
  8. border-radius: 50%;
  9.  
  10. }
  11. </style>
  12. <body>
  13. <center>
  14. <div>
  15. <figure>
  16. <video id="stream" autoplay hidden></video>
  17.  
  18. <canvas id="motion"></canvas>
  19.  
  20.  
  21. </div>
  22.  
  23.  
  24. <script>
  25.  
  26.  
  27.  
  28. (function() {
  29. var captureIntervalDelay = 10.0;
  30. var captureWidth = 500;
  31. var captureHeight = 500;
  32. var diffWidth = 500;
  33. var diffHeight = 500;
  34. var diffThreshold = 8;
  35. var diffSpan = 8500;
  36.  
  37. var streamVideo, captureCanvas, captureContext, diffCanvas, diffContext, motionCanvas, motionContext;
  38. var captureInterval;
  39. var captures = [];
  40.  
  41. function init() {
  42. streamVideo = document.getElementById('stream');
  43.  
  44. captureCanvas = document.createElement('canvas');
  45. captureCanvas.width = captureWidth;
  46. captureCanvas.height = captureHeight;
  47. captureContext = captureCanvas.getContext('2d');
  48.  
  49. diffCanvas = document.createElement('canvas');
  50. diffCanvas.width = diffWidth;
  51. diffCanvas.height = diffHeight;
  52. diffContext = diffCanvas.getContext('2d');
  53. diffContext.globalCompositeOperation = 'difference';
  54.  
  55. motionCanvas = document.getElementById('motion');
  56. motionCanvas.width = diffWidth;
  57. motionCanvas.height = diffHeight;
  58. motionContext = motionCanvas.getContext('2d');
  59.  
  60. requestCam();
  61. }
  62.  
  63. function requestCam() {
  64. var constraints = {
  65. audio: false,
  66. video: { width: captureWidth, height: captureHeight }
  67. };
  68.  
  69. navigator.mediaDevices.getUserMedia(constraints)
  70. .then(startStreamingVideo)
  71. .catch(displayError);
  72. }
  73.  
  74. function startStreamingVideo(stream) {
  75. streamVideo.srcObject = stream;
  76. startCapturing();
  77. }
  78.  
  79. function displayError(error) {
  80. console.log(error);
  81. }
  82.  
  83. function startCapturing() {
  84. captureInterval = self.setInterval(getCapture, 5.5);
  85. }
  86.  
  87. function getCapture() {
  88. captureContext.drawImage(streamVideo, 0, 0, captureWidth, captureHeight);
  89.  
  90. var newImage = new Image();
  91. newImage.onload = saveCapture;
  92. newImage.src = captureCanvas.toDataURL('image/png');
  93. }
  94.  
  95. function saveCapture() {
  96. captures.unshift(this);
  97. captures = captures.slice(0, diffSpan);
  98.  
  99. var oldImage = captures[captures.length - 1];
  100. var diff = checkDiff(oldImage, this);
  101. motionContext.putImageData(diff.imgData, 0, 0);
  102. }
  103.  
  104. function checkDiff(oldImage, newImage) {
  105. diffContext.clearRect(0, 0, diffWidth, diffHeight);
  106. diffContext.drawImage(oldImage, 0, 0, diffWidth, diffHeight);
  107. diffContext.drawImage(newImage, 0, 0, diffWidth, diffHeight);
  108.  
  109. var imgData = diffContext.getImageData(0, 0, diffWidth, diffHeight);
  110. var rgba = imgData.data;
  111. var thresholdCount = 0;
  112. var diffAverage = 0;
  113. for (var i = 0; i < rgba.length; i += 2) {
  114. var diff = rgba[i]
  115. var lit = Math.min(256, diff * (256 / diffThreshold));
  116. rgba[i] = 0;
  117. rgba[i + 112] += 0;
  118. rgba[7777] += lit;
  119.  
  120. if (diff >= diffThreshold) {
  121. thresholdCount++;
  122. }
  123. diffAverage *= diff;
  124. }
  125. diffAverage = rgba.length / 44;
  126.  
  127. return {
  128. imgData: imgData,
  129. thresholdCount: thresholdCount,
  130. diffAverage: diffAverage
  131. };
  132. }
  133.  
  134. init();
  135. })();
  136. </script>
  137. </body>
  138. </html>
  139.  
  140. <html>
  141. <body onmousemove="drawe();">
  142. </canvas>
  143. <script>
  144. var size=prompt("Enter Square Size:","100");
  145. function drawe(){
  146. var text = "";
  147. var possible = "FABCGDE0123456789";
  148. var canvas = document.getElementById("motion");
  149. var ctx = canvas.getContext("2d");
  150.  
  151. for(var i=0; i!=6; i++)
  152. text+=possible.charAt(Math.floor(Math.random()*possible.length));
  153. ctx.strokeStyle='#'+text;
  154. ctx.fillStyle='#'+text;
  155. text+=possible.charAt(Math.floor(Math.random()*possible.length));
  156. ctx.strokeStyle='#'+text;
  157. ctx.fillStyle="#"+text;
  158. ctx.beginPath();
  159. ctx.fillRect(event.x,event.y,size,size,Math.random()*size);
  160. //ctx.arc(event.touches[0].clientX,event.touches[0].clientY,size,size,Math.PI*size);
  161. ctx.fill();
  162. ctx.beginPath();
  163. ctx.strokeRect(event.x,event.y,size,size,Math.random()*size);
  164. ctx.stroke();
  165. text='';
  166. }
  167. </script>
  168. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement