Skip to content

Commit

Permalink
Countdown before taking snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmw committed Sep 11, 2015
1 parent 3bef2df commit e3a5ee6
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 2 deletions.
10 changes: 9 additions & 1 deletion app/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ const snapshot = (image_data) => {
});
};

const initiateCountdown = () => {
console.log('Countdown initiated');
AppDispatcher.dispatch({
type: 'initiate-countdown'
});
};

export default {
snapshot
snapshot,
initiateCountdown
};

31 changes: 31 additions & 0 deletions app/components/countdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import Actions from '../actions';
import CountdownStore from '../stores/countdown-store';

let countdownTimer;

class Countdown extends React.Component {
constructor(props) {
super(props);

this.state = {
timeLeft: null
};
}

componentDidMount() {
CountdownStore.addChangeListener(this.updateCountdown.bind(this));
}

updateCountdown() {
this.setState({ timeLeft: CountdownStore.getTimeLeft() })
}

render() {
return (
<div className='countdown'>{this.state.timeLeft}</div>
);
}
}

export default Countdown;
2 changes: 2 additions & 0 deletions app/components/photobooth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import Video from './video';
import Countdown from './countdown';
import SnapshotList from './snapshot-list';

class Photobooth extends React.Component {
Expand All @@ -8,6 +9,7 @@ class Photobooth extends React.Component {
<div className="photobooth">
<h1>React Photobooth</h1>
<Video />
<Countdown />
<SnapshotList />
</div>
);
Expand Down
24 changes: 23 additions & 1 deletion app/components/video.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import React from 'react';
import Actions from '../actions';
import CountdownStore from '../stores/countdown-store';

class Video extends React.Component {
constructor(props) {
super(props);

this.state = {
snapshotButtonDisabled: false
};
}

componentDidMount() {
let mediaOptions = { audio: false, video: true };
let video = this.video();
Expand All @@ -17,13 +26,19 @@ class Video extends React.Component {
};

navigator.getUserMedia(mediaOptions, success, error);

CountdownStore.addChangeListener(this.takeSnapshot.bind(this));
}

video() {
return React.findDOMNode(this.refs.video)
}

takeSnapshot() {
if (CountdownStore.getTimeLeft() != 0) {
return;
}

let canvas = document.createElement('canvas');
const width = 200;
const height = 150;
Expand All @@ -40,13 +55,20 @@ class Video extends React.Component {
var data = canvas.toDataURL('image/png');

Actions.snapshot(data);

this.setState({ snapshotButtonDisabled: false });
}

initiateCountdown() {
this.setState({ snapshotButtonDisabled: true });
Actions.initiateCountdown();
}

render() {
return (
<div className='video'>
<video ref="video" autoPlay="true"></video>
<button onClick={this.takeSnapshot.bind(this)}>Take snapshot</button>
<button disabled={this.state.snapshotButtonDisabled} onClick={this.initiateCountdown.bind(this)}>Take snapshot</button>
</div>
);
}
Expand Down
46 changes: 46 additions & 0 deletions app/stores/countdown-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import AppDispatcher from '../app-dispatcher.js';
import BaseStore from './base-store.js';

const TIMEOUT_LENGTH = 3;

let timeLeft;
let timer;

class _CountdownStore extends BaseStore {
getTimeLeft() {
return timeLeft;
}
}

const CountdownStore = new _CountdownStore();

let initiateCountdown = () => {
clearTimeout(timer);
timeLeft = TIMEOUT_LENGTH;

timer = setInterval(() => {
timeLeft = timeLeft - 1;

if (timeLeft == 0) {
clearTimeout(timer);
CountdownStore.emitChange();
timeLeft = null;
}

CountdownStore.emitChange();
}, 1000);
}

AppDispatcher.register((payload) => {
switch(payload.type) {
case 'initiate-countdown':
initiateCountdown();
CountdownStore.emitChange();
break;
default:
break;
}
return true;
});

export default CountdownStore;
3 changes: 3 additions & 0 deletions css/countdown.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.countdown {
font-size: 48px;
}
1 change: 1 addition & 0 deletions css/main.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@import 'countdown';
@import 'snapshot-list';
@import 'video';

0 comments on commit e3a5ee6

Please sign in to comment.