Skip to content

Instantly share code, notes, and snippets.

@xl1
Created November 12, 2012 12:45
Show Gist options
  • Save xl1/4059205 to your computer and use it in GitHub Desktop.
Save xl1/4059205 to your computer and use it in GitHub Desktop.
カメラから映像とるやつ
class window.WebCam
constructor: ->
@video = document.createElement('video')
init: (playCallback, failCallback) ->
userMedia = navigator.getUserMedia or navigator.webkitGetUserMedia
if not userMedia
return failCallback?()
@video.addEventListener('canplay', => @video.play())
@video.addEventListener('timeupdate', =>
@width = @video.videoWidth
@height = @video.videoHeight
if @width or @height
@video.removeEventListener('timeupdate', arguments.callee, false)
playCallback?()
, false)
setSourceURL = (src) =>
url = window.URL or window.webkitURL
@video.src = if url then url.createObjectURL(src) else src
userMedia.call(navigator,
{ video: true, toString: -> 'video' }, setSourceURL, failCallback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment