
window.URL = window.URL || window.webkitURL; navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; //... if (navigator.getUserMedia) { navigator.getUserMedia({audio: true, video: true}, function(stream) { video.src = window.URL.createObjectURL(stream); }, onFailSoHard); } else { video.src = 'somevideo.webm'; // fallback. } navigator.getUserMedia function is defined and execute the appropriate code. Otherwise, we perform some fallback function. All this can be read more on the above site. I want to talk about some things that are not obvious and not described there.stream object is different in different browsers. In chrome, in order to pass it to the src parameter of the video tag, you must convert it to objectURL using window.URL.createObjectURL . In opera, as I understand it, the stream object is automatically converted, and for example the window.URL properties — I just did not find it. Therefore, we write directly: video.src = stream . Well, firefox has a window.URL property and a createObjectURL method. However, such code window.URL.createObjectURL(stream) - gives an error, but here’s how video.src = stream works. navigator.getUserMedia && navigator.getUserMedia({video: true}, function(stream) { try { video.src = window.URL.createObjectURL(stream); //for webkit } catch (e) { video.src = stream; //for opera and firefox } }, function() { alert('- '); }); // play/pause - video.addEventListener('click', function() { if (video.paused) { video.play(); } else { $(video).data('data-url', getSnapshotDataUrl()); // data-url video.pause(); } }); ... var getDataUrl = function() { var data_url = video.paused ? $video.data('data-url') : getSnapshotDataUrl(); return data_url; } getSnapshotDataUrl is a function that throws the current frame of the video to the canvas, element canvas, and then the toDataURL method is called on the canvas.html5WebCam . The second is for cropping photos html5Crop . Yes, they can be used separately.button . Now the element listens to the click event and starts the procedure for creating an avatar. When the avatar is ready, we will get it in the onCrop , or onsnapshot callback function. $(document).ready(function() { $("#create_snapshot").html5WebCam({ oncrop: function(cropped_url) { // cropped_url - base64 image var $img = $("<img/>"); $img.attr('src', cropped_url); $('body').append($img); }, }); }); //html5WebCam NOT_SUPPORT_FEATURE: ' ', CAMERA_NOT_FOUND: ' ', CLICK_TO_PAUSE: ' /', TAKE_SNAPSHOT: ' ', CANCEL: '', max_video_size: 600, modal_class: 'html5-webcam-avatar-modal', use_native_modal: true, use_native_button: true, onDomCreated: function($html) { }, onsnapshot: function(snapshot) {}, use_crop: true, oncrop: function(cropped_url) {}, oncancel: function() {}, alertFn: function(msg) { alert(msg); } //html5Crop CROP_NAME: '', CANCEL: '', MIN_IMG_SIDE_ERROR: ' ', CANVAS_NOT_SUPPORTED: 'canvas not supported in this browser', square_mode: true, max_crop_side: 400, min_crop_side: 50, max_img_side: 600, min_img_side: 100, init_crop_side: 100, dot_side: 10, use_native_modal: true, use_native_button: true, onDomCreated: function($ui) {}, oncancel: function() {}, oncrop: function(cropped_url) {}, alertFn: function(msg) { alert(msg); }, modal_class: 'html5-webcam-avatar-modal' use_crop: true (the default is exactly this way).Source: https://habr.com/ru/post/170983/
All Articles