Guest User

Untitled

a guest
Nov 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. // jquery and underdscore and backbone.js for now anyway
  2.  
  3. function(){
  4.  
  5. Camera = function(opt){
  6. opt = opt || {};
  7.  
  8. this.x = opt.x || 640;
  9. this.y = opt.y || 480;
  10.  
  11. //use a real thing or make a document fragment
  12. this.canvas = opt.canvas || $('<canvas width="' + this.x +'" height="'+this.y+'"></canvas>').get(0);
  13. this.video = opt.video || $('<video autoplay></video>').get(0);
  14. this.ctx = this.canvas.getContext('2d');
  15. this.ready = false;
  16. // stupid w3c vendor prefix garbage
  17. navigator.getUserMedia = navigator.getUserMedia ||
  18. navigator.webkitGetUserMedia ||
  19. navigator.mozGetUserMedia ||
  20. navigator.msGetUserMedia;
  21.  
  22. if(!navigator.getUserMedia){
  23. this.initFlash();
  24. }else{
  25. this.initHTML5();
  26. }
  27. };
  28.  
  29. _.extend(Camera.prototype, Backbone.Events);
  30.  
  31. Camera.prototype.initHTML5 = function(){
  32. var that = this;
  33. navigator.getUserMedia({video: true}, function(stream) {
  34. if (navigator.webkitGetUserMedia){
  35. that.video.src = window.webkitURL.createObjectURL(stream);
  36. }else{
  37. that.video.src = stream; // Opera
  38. }
  39. that.trigger('ready');
  40. that.ready = true;
  41. }, this.noCamera);
  42. };
  43.  
  44. Camera.prototype.initFlash = function(){
  45. window.alert('Lets play with Chrome Canary!');
  46. throw Error('Flash Fallback Missing and no getUserMedia!');
  47. };
  48.  
  49. Camera.prototype.noCamera = function(e) {
  50. console.log('Failed to acquire camera!', e);
  51. window.alert('no camera!');
  52. };
  53.  
  54. Camera.prototype.snapshot = function() {
  55. this.ctx.drawImage(this.video, 0, 0, this.x, this.y);
  56. return this.canvas.toDataURL('image/jpeg');
  57. };
  58.  
  59. return Camera;
  60. }
Add Comment
Please, Sign In to add comment