kdf
Version:
178 lines (135 loc) • 4.4 kB
text/coffeescript
KDView = require './../../core/view.coffee'
KDCustomHTMLView = require './../../core/customhtmlview.coffee'
KDButtonView = require './../buttons/buttonview.coffee'
module.exports = class KDWebcamView extends KDView
constructor: (options={}, data)->
options.cssClass or= "kdwebcamview"
options.screenFlash ?= yes
options.hideControls ?= no
options.snapTitle or= "Snap Photo"
options.resnapTitle or= "Resnap"
options.saveTitle or= "Save"
options.countdown ?= 3
super options, data
= new KDCustomHTMLView
tagName : "video"
attributes:
autoplay: yes
= new KDCustomHTMLView
tagName : "canvas"
= if options.hideControls then new KDView cssClass: "hidden"
else new KDButtonView
title : options.snapTitle
cssClass : "snap-button hidden"
callback : 'countDown'
= if options.hideControls then new KDView cssClass: "hidden"
else new KDButtonView
title : options.resnapTitle
cssClass : "snap-button retake hidden"
callback : =>
= if options.hideControls then new KDView cssClass: "hidden"
else new KDButtonView
title : options.saveTitle
cssClass : "snap-button save hidden"
callback : =>
.setClass "invisible"
.hide()
"save"
attachEvents: ->
{snapTitle} =
"KDObjectWillBeDestroyed", =>
"viewAppended", =>
= .getElement().getContext "2d"
"error", (error)-> "forbidden"
"snap", => .setClass "invisible"
"countDownEnd", =>
.hide()
.show()
.show()
.setTitle snapTitle
resetView: ->
.show()
.hide()
.hide()
reset: ->
.unsetClass "invisible"
countDown: ->
{countdown} =
if countdown > 0
counter = =>
.setTitle countdown
countdown--
count = .repeat 1000, counter
do counter
timer = .wait (countdown+1)*1000, =>
.killRepeat count
.killWait timer
"countDownEnd"
else
"countDownEnd"
autoResize: ->
video = .getElement()
size =
width : video.clientWidth
height: video.clientHeight
.setAttributes size
size
unsetVideoStream: ->
video = .getElement()
video.pause()
KDWebcamView.setVideoStreamVendor video, ""
?.stop()
setVideoStream: (stream)->
video = .getElement()
KDWebcamView.setVideoStreamVendor video, stream
video.play()
video.addEventListener "playing", =>
.show()
"allowed"
: (video, stream)->
if video.mozSrcObject isnt undefined
then video.mozSrcObject = stream
else video.src = stream
: ->
navigator.getUserMedia or\
navigator.webkitGetUserMedia or\
navigator.mozGetUserMedia
: ->
window.URL or\
window.webkitURL or\
window.mozURL
getUserMedia: ->
_onError = (error)=> "error", error
navigator.getUserMedia = KDWebcamView.getUserMediaVendor()
window.URL = KDWebcamView.getURLVendor()
if navigator.getUserMedia
navigator.getUserMedia video: yes, (stream)=>
= stream
(window.URL and window.URL.createObjectURL stream) or stream
, _onError
else _onError notSupported: yes
flash: ->
flash = new KDView
cssClass: "kdwebcamview-flash"
flash.appendToDomBody()
KD.utils.defer ->
flash.setClass "flashed"
KD.utils.wait 500, -> flash.destroy()
takePicture: ->
video = .getElement()
picture = .getElement()
{screenFlash} =
if screenFlash
.drawImage video, 0, 0, video.clientWidth, video.clientHeight
"snap", picture.toDataURL(), picture
viewAppended: ->
super()
view for view in [ , , , , ]