// Main initialization Document. addEventListener ('domainloaded', function (){ // Global variables Var video = document. querySelector ('video '); Var audio, audioType; Var canvas = document. querySelector ('canvas '); Var context = canvas. getContext ('2d '); // Custom video filters Var iFilter = 0; Var filters = [ 'Grayscale ', 'Sepia ', 'Blur ', 'Brightness ', 'Contrast ', 'Hue-rotate ', 'Hue-rotate2 ', 'Hue-rotate3 ', 'Saturate ', 'Invert ', 'None' ]; // Get the video stream from the camera with getUserMedia Navigator. getUserMedia = navigator. getUserMedia | navigator. webkitGetUserMedia | Navigator. mozGetUserMedia | navigator. msGetUserMedia; Window. URL = window. URL | window. webkitURL | window. URLs | window. msURL; If (navigator. getUserMedia ){ // Evke getUserMedia function Navigator. getUserMedia ({video: true, audio: true}, onSuccessCallback, onErrorCallback ); Function onSuccessCallback (stream ){ // Use the stream from the camera as the source of the video element Video. src = window. URL. createObjectURL (stream) | stream; // Autoplay Video. play (); // HTML5 Audio Audio = new Audio (); AudioType = getAudioType (audio ); If (audioType ){ Audio. src = 'Polaroid. '+ audioType; Audio. play (); } } // Display an error Function onErrorCallback (e ){ Var expl = 'an error occurred: [Reason: '+ e. code +'] '; Console. error (expl ); Alert (expl ); Return; } } Else { Document. querySelector ('. iner'). style. visibility = 'den den '; Document. querySelector ('. warning'). style. visibility = 'visable '; Return; } // Draw the video stream at the canvas object Function drawVideoAtCanvas (obj, context ){ Window. setInterval (function (){ Context. drawImage (obj, 0, 0 ); }, 60 ); } // The canPlayType () function doesn't return true or false. In recognition of how complex // Formats are, the function returns a string: 'baby', 'maybe' or an empty string. Function getAudioType (element ){ If (element. canPlayType ){ If (element. canPlayType ('audio/mp4; codecs = "mp4a. 40.5 "')! = ''){ Return ('aac '); } Else if (element. canPlayType ('audio/ogg; codecs = "vorbis "')! = ''){ Return ("ogg "); } } Return false; } // Add event listener for our video's Play function in order to produce video at the canvas Video. addEventListener ('play', function (){ DrawVideoAtCanvas (this, context ); }, False ); // Add event listener for our Button (to switch video filters) Document. querySelector ('button '). addEventListener ('click', function (){ Video. className = ''; Canvas. className = ''; Var effect = filters [iFilter ++ % filters. length]; // Loop through the filters. If (effect ){ Video. classList. add (effect ); Canvas. classList. add (effect ); Document. querySelector ('. iner h3'). innerHTML = 'current filter is: '+ effect; } }, False ); }, False ); |