1+ function showScreen ( screenId ) {
2+ document . querySelectorAll ( "div" ) . forEach ( div => div . style . display = "none" ) ;
3+ document . getElementById ( screenId ) . style . display = "block" ;
4+ }
5+
6+ let photoCount , capturedPhotos = [ ] ;
7+
8+ function startPhotoBooth ( count ) {
9+ photoCount = count ;
10+ capturedPhotos = [ ] ;
11+ showScreen ( 'photoCapture' ) ;
12+ startCamera ( ) ;
13+ }
14+
15+ function startCamera ( ) {
16+ navigator . mediaDevices . getUserMedia ( { video : true } )
17+ . then ( stream => {
18+ document . getElementById ( 'camera' ) . srcObject = stream ;
19+ } )
20+ . catch ( error => console . error ( "Camera access denied" , error ) ) ;
21+ }
22+
23+ function startPhotoSequence ( index = 0 ) {
24+ if ( index >= photoCount ) {
25+ showScreen ( 'outputOptions' ) ;
26+ displayPhotos ( ) ;
27+ return ;
28+ }
29+
30+ const countdownText = document . getElementById ( 'countdownText' ) ;
31+ countdownText . innerText = "Get ready..." ;
32+
33+ setTimeout ( ( ) => {
34+ countdownText . innerText = "3..." ;
35+ setTimeout ( ( ) => {
36+ countdownText . innerText = "2..." ;
37+ setTimeout ( ( ) => {
38+ countdownText . innerText = "1..." ;
39+ setTimeout ( ( ) => {
40+ takePhoto ( index ) ;
41+ setTimeout ( ( ) => startPhotoSequence ( index + 1 ) , 3000 ) ;
42+ } , 1000 ) ;
43+ } , 1000 ) ;
44+ } , 1000 ) ;
45+ } , 1000 ) ;
46+ }
47+
48+ function takePhoto ( index ) {
49+ const video = document . getElementById ( 'camera' ) ;
50+ const canvas = document . createElement ( 'canvas' ) ;
51+ const context = canvas . getContext ( '2d' ) ;
52+ canvas . width = video . videoWidth ;
53+ canvas . height = video . videoHeight ;
54+ context . drawImage ( video , 0 , 0 , canvas . width , canvas . height ) ;
55+
56+ capturedPhotos . push ( canvas . toDataURL ( "image/png" ) ) ;
57+ }
58+
59+ function displayPhotos ( ) {
60+ const canvas = document . getElementById ( 'photoCanvas' ) ;
61+ const context = canvas . getContext ( '2d' ) ;
62+
63+ const img = new Image ( ) ;
64+ img . src = capturedPhotos [ 0 ] ;
65+ img . onload = ( ) => {
66+ const aspectRatio = img . width / img . height ;
67+ const photoWidth = 400 ;
68+ const photoHeight = photoWidth / aspectRatio ;
69+ const spacing = 10 ;
70+ const borderSize = 5 ;
71+ const textHeight = 50 ;
72+
73+ canvas . width = photoWidth + borderSize * 2 ;
74+ canvas . height = photoCount * ( photoHeight + spacing ) - spacing + borderSize * 2 + textHeight ;
75+
76+ context . fillStyle = "#ffffff" ;
77+ context . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
78+
79+ capturedPhotos . forEach ( ( photo , index ) => {
80+ const img = new Image ( ) ;
81+ img . src = photo ;
82+ img . onload = ( ) => {
83+ const x = borderSize ;
84+ const y = borderSize + index * ( photoHeight + spacing ) ;
85+
86+ context . fillStyle = "#ffffff" ;
87+ context . fillRect ( x - borderSize , y - borderSize , photoWidth + borderSize * 2 , photoHeight + borderSize * 2 ) ;
88+ context . drawImage ( img , x , y , photoWidth , photoHeight ) ;
89+ } ;
90+ } ) ;
91+ } ;
92+ }
93+
94+ function applyText ( ) {
95+ const canvas = document . getElementById ( 'photoCanvas' ) ;
96+ const context = canvas . getContext ( '2d' ) ;
97+ const text = document . getElementById ( 'caption' ) . value ;
98+
99+ context . fillStyle = "#000000" ;
100+ context . font = "24px Arial" ;
101+ context . textAlign = "center" ;
102+ context . fillText ( text , canvas . width / 2 , canvas . height - 20 ) ;
103+ }
104+
105+ function returnToStart ( ) {
106+ photoCount = 0 ;
107+ capturedPhotos = [ ] ;
108+
109+ const video = document . getElementById ( 'camera' ) ;
110+ if ( video . srcObject ) {
111+ video . srcObject . getTracks ( ) . forEach ( track => track . stop ( ) ) ;
112+ }
113+
114+ showScreen ( 'welcomeScreen' ) ;
115+ }
116+
117+ function downloadPhoto ( ) {
118+ const canvas = document . getElementById ( 'photoCanvas' ) ;
119+ const link = document . createElement ( 'a' ) ;
120+ link . download = "photostrip.png" ;
121+ link . href = canvas . toDataURL ( "image/png" ) ;
122+ link . click ( ) ;
123+ }
124+
125+ function printPhoto ( ) {
126+ const canvas = document . getElementById ( 'photoCanvas' ) ;
127+ const newWindow = window . open ( ) ;
128+ newWindow . document . write ( '<img src="' + canvas . toDataURL ( "image/png" ) + '">' ) ;
129+ newWindow . print ( ) ;
130+ }
0 commit comments