@@ -44,11 +44,12 @@ function capture (successCallback, errorCallback, opts) {
44
44
const customCaptureButton = opts [ 13 ] ;
45
45
const customCancelButton = opts [ 14 ] ;
46
46
47
+ const customElements = { customCameraContainer, customCaptureButton, customCancelButton} ;
48
+
47
49
let parent = customCameraContainer ? document . getElementById ( customCameraContainer ) : createCameraContainer ( ) ;
48
50
let video = createVideoStreamContainer ( parent , targetWidth , targetHeight ) ;
49
51
let captureButton = customCaptureButton ? document . getElementById ( customCaptureButton ) : createButton ( parent , 'Capture' ) ;
50
52
let cancelButton = customCancelButton ? document . getElementById ( customCancelButton ) : createButton ( parent , 'Cancel' ) ;
51
-
52
53
// start video stream
53
54
startLocalMediaStream ( errorCallback , video ) ;
54
55
@@ -59,8 +60,8 @@ function capture (successCallback, errorCallback, opts) {
59
60
}
60
61
61
62
// handle button click events
62
- handleCaptureButton ( successCallback , errorCallback , captureButton , video , customCameraContainer ) ;
63
- handleCancelButton ( cancelButton , video , customCameraContainer ) ;
63
+ handleCaptureButton ( successCallback , errorCallback , captureButton , video , customElements ) ;
64
+ handleCancelButton ( cancelButton , video , customElements ) ;
64
65
}
65
66
66
67
function createCameraContainer ( ) {
@@ -94,7 +95,7 @@ function createButton (parent, innerText) {
94
95
return button ;
95
96
}
96
97
97
- function handleCaptureButton ( successCallback , errorCallback , captureButton , video , customCameraContainer ) {
98
+ function handleCaptureButton ( successCallback , errorCallback , captureButton , video , customElements ) {
98
99
captureButton . onclick = function ( ) {
99
100
// create a canvas and capture a frame from video stream
100
101
let canvas = document . createElement ( 'canvas' ) ;
@@ -107,16 +108,16 @@ function handleCaptureButton (successCallback, errorCallback, captureButton, vid
107
108
imageData = imageData . replace ( 'data:image/png;base64,' , '' ) ;
108
109
109
110
// stop video stream
110
- stopLocalMediaStream ( video , customCameraContainer ) ;
111
+ stopLocalMediaStream ( video , customElements ) ;
111
112
112
113
return successCallback ( imageData ) ;
113
114
} ;
114
115
}
115
116
116
- function handleCancelButton ( cancelButton , video , customCameraContainer ) {
117
+ function handleCancelButton ( cancelButton , video , customElements ) {
117
118
cancelButton . onclick = function ( ) {
118
119
// stop video stream
119
- stopLocalMediaStream ( video , customCameraContainer ) ;
120
+ stopLocalMediaStream ( video , customElements ) ;
120
121
} ;
121
122
}
122
123
@@ -140,7 +141,7 @@ function startLocalMediaStream (errorCallback, video) {
140
141
}
141
142
}
142
143
143
- function stopLocalMediaStream ( video , customCameraContainer ) {
144
+ function stopLocalMediaStream ( video , customElements ) {
144
145
// stop video stream, remove video and captureButton.
145
146
// note: MediaStream.stop() is deprecated as of Chrome 47.
146
147
if ( localMediaStream . stop ) {
@@ -152,13 +153,25 @@ function stopLocalMediaStream (video, customCameraContainer) {
152
153
}
153
154
154
155
// remove newly created elements
155
- removeAppendedCameraElements ( video , customCameraContainer ) ;
156
+ removeAppendedCameraElements ( video , customElements ) ;
156
157
}
157
158
158
- function removeAppendedCameraElements ( video , customCameraContainer ) {
159
+ function removeAppendedCameraElements ( video , customElements ) {
160
+
159
161
const parent = video . parentNode ;
160
- if ( ! customCameraContainer ) {
162
+
163
+ if ( ! customElements . customCameraContainer ) {
161
164
parent . parentNode . removeChild ( parent ) ;
165
+ } else if ( ! customElements . customCaptureButton && ! customElements . customCancelButton ) {
166
+ while ( parent . hasChildNodes ( ) ) {
167
+ parent . removeChild ( parent . lastChild ) ;
168
+ }
169
+ } else if ( parent . hasChildNodes ( ) && ! customElements . customCaptureButton ) {
170
+ parent . removeChild ( video ) ;
171
+ parent . removeChild ( parent . lastChild ) ;
172
+ } else if ( parent . hasChildNodes ( ) && ! customElements . customCancelButton ) {
173
+ parent . removeChild ( video ) ;
174
+ parent . removeChild ( parent . lastChild ) ;
162
175
} else {
163
176
parent . removeChild ( video ) ;
164
177
}
0 commit comments