Skip to content

Instantly share code, notes, and snippets.

@lazykyama
Last active December 16, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazykyama/5477836 to your computer and use it in GitHub Desktop.
Save lazykyama/5477836 to your computer and use it in GitHub Desktop.
発表やプレゼンのタイムキープ用ツール。「start/stop」ボタンを押すとタイムキープを開始。Google Chrome, Safari(ともにMac OSX 10.7.5)でのみ動作確認。jQueryを利用しているため、ネットワークとの接続が必要。
<!--
* @auther kyama http://sukimaprogrammer.blogspot.jp/
* @since 0.0.1
* @version 0.0.3
*
* This source is distributed under the MIT License.
*
* Copyright (c) 2013 lazykyama http://sukimaprogrammer.blogspot.jp/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>ConfTimeKeeper (alpha release)</title>
<style>
body {
margin-top: 50px;
margin-left: 0px;
background-color: #666666;
height: 100%;
width: 100%;
}
#header {
position: absolute;
top: 0px;
left: 0px;
background-color: #333333;
width: 100%;
height: 30px;
margin: 0px 0px 10px 0px;
padding: 5px 0px 5px 0px;
color: #999999;
font-weight: bold;
display: table;
}
#header div {
display: table-cell;
vertical-align: middle;
}
.header-items {
margin-left: 10px;
margin-right: 0px;
padding-left: 10px;
padding-right: 10px;
}
#header_title {
}
#header_form {
text-align: right;
}
#header_btn {
}
#contents {
display: none;
}
#contents_canvas {
background-color: #cccccc;
width: 80%;
padding: 10px;
margin-bottom: 15px;
margin-left: auto;
margin-right: auto;
border: solid 1px #333;
border-radius: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
display: table;
vertical-align: middle;
}
.contents-items {
display: table-row;
vertical-align: middle;
}
.contents-items div {
display: table-cell;
vertical-align: middle;
}
#elapsed_time {
height: 20%;
}
#elapsed_time div {
vertical-align: bottom;
text-align: right;
font-size: 60px;
}
#remaining_time {
height: 80%;
text-align: center;
font-size: 96px;
}
</style>
</head>
<body>
<div id="header">
<div class="header-items" id="header_title">ConfTimeKeeper (alpha)</div>
<div class="header-items" id="header_form">
<label>発表時間:
<input id="conf_time_min" type="number" min="0" value="10"></input>分
<input id="conf_time_sec" type="number" min="0" value="0"></input>秒
</label>
</div>
<div class="header-items" id="header_btn">
<div>
<button id="start_btn">start/stop</button>
<button id="pause_btn">pause</button>
</div>
</div>
</div>
<div id="contents">
<div id="contents_canvas">
<div class="contents-items" id="remaining_time">
<div>ここに残り時間が表示されます。</div>
</div>
<div class="contents-items" id="elapsed_time">
<div>
<span id="elapsed_time_counter">xx:xx.xxx</span> / <span id="elapsed_time_consts">xx:xx</span>
</div>
</div>
</div>
</div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
window.requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame;
var ConfTimeKeeper = {};
ConfTimeKeeper.enabledRequestAnimationFrame = true;
ConfTimeKeeper.style = {};
ConfTimeKeeper.style.consts = {};
if (undefined === window.requestAnimationFrame) {
ConfTimeKeeper.enabledRequestAnimationFrame = false;
}
ConfTimeKeeper.convertPixelStr2Int = function(str, suffix) {
return parseInt(str.replace(suffix, ''), 10);
};
ConfTimeKeeper.resizeContents = function() {
// contents size.
var windowHeight = $(window).height();
var contentsCanvas = $('#contents_canvas');
var offset = contentsCanvas.offset();
var paddingTop = ConfTimeKeeper.convertPixelStr2Int(contentsCanvas.css('padding-top'), 'px');
var paddingBottom = ConfTimeKeeper.convertPixelStr2Int(contentsCanvas.css('padding-bottom'), 'px');
var marginBottom = ConfTimeKeeper.convertPixelStr2Int(contentsCanvas.css('margin-bottom'), 'px');
var canvasHeight = windowHeight - offset.top - paddingTop - paddingBottom - marginBottom;
$('#contents_canvas').height(canvasHeight);
};
ConfTimeKeeper.checkAndGetConfTime = function() {
var confTimeMin = $('#conf_time_min').val();
var confTimeSec = $('#conf_time_sec').val();
var confTimeMinVal = parseInt(confTimeMin, 10);
var confTimeSecVal = parseInt(confTimeSec, 10);
// validate.
if (!isFinite(confTimeMinVal) || 0 > confTimeMinVal) {
throw new Error('invalid value on min.')
}
if (!isFinite(confTimeSecVal) || 0 > confTimeSecVal) {
throw new Error('invalid value on sec.')
}
// update.
$('#conf_time_min').val(confTimeMinVal);
$('#conf_time_sec').val(confTimeSecVal);
return {min: confTimeMinVal, sec: confTimeSecVal};
};
var isNullable = function(obj) {
return (undefined === obj || null === obj);
};
var repeatString = function(str, num) {
// http://d.hatena.ne.jp/hir90/20080620/1213987444
if (0 > num) {
throw new Error('error.');
} else if (0 === num) {
return '';
}
var ans = '';
while (num) {
if (num & 1) {
ans += str;
}
num = num >> 1;
str += str;
}
return ans;
};
var padLeft = function(params) {
// http://rt.air-nifty.com/blog/2009/10/javascript-28d6.html
if (isNullable(params.text) || isNullable(params.length)) {
throw new Error('error.');
}
if (params.text.length >= params.length) {
return params.text;
}
var padText = params.char || ' ';
var paddedText = repeatString(padText, params.length);
return (paddedText + params.text).slice(-params.length);
};
ConfTimeKeeper.convertTime2Text = function(params) {
if (isNullable(params.min) || isNullable(params.sec)) {
throw new Error('error.');
}
var retText = padLeft({text: '' + params.min, length: 2, char: '0'})
+ ':' + padLeft({text: '' + params.sec, length: 2, char: '0'});
if (!isNullable(params.millis)) {
retText += '.' + padLeft({text: '' + params.millis, length: 3, char: '0'});
}
return retText;
};
ConfTimeKeeper.updateConfTime = function() {
var timeConsts = $('#elapsed_time_consts');
var timeCounter = $('#elapsed_time_counter');
try {
ConfTimeKeeper.confTime = ConfTimeKeeper.checkAndGetConfTime();
} catch (e) {
alert('時間を正しく指定して下さい。');
return false;
}
var realSec = ConfTimeKeeper.confTime.sec % 60;
var realMin = ConfTimeKeeper.confTime.min + Math.floor(ConfTimeKeeper.confTime.sec / 60);
var constsText = ConfTimeKeeper.convertTime2Text({
min: realMin, sec: realSec
});
timeConsts.text(constsText);
return true;
};
ConfTimeKeeper.update = function() {
ConfTimeKeeper.currTime = Date.now();
ConfTimeKeeper.updateTimeCounter();
ConfTimeKeeper.updateRemainingTime();
ConfTimeKeeper.prevTime = ConfTimeKeeper.currTime;
if (ConfTimeKeeper.running && ConfTimeKeeper.enabledRequestAnimationFrame) {
requestAnimationFrame(ConfTimeKeeper.update);
} else if (ConfTimeKeeper.running) {
setTimeout(ConfTimeKeeper.update, 30);
}
};
ConfTimeKeeper.updateTimeCounter = function() {
var timeCounter = $('#elapsed_time_counter');
var diffTime = ConfTimeKeeper.currTime - ConfTimeKeeper.startTime;
var diffSec = Math.floor(diffTime / 1000);
var timeText = ConfTimeKeeper.convertTime2Text({
min: Math.floor(diffSec / 60),
sec: diffSec % 60,
millis: (diffTime % 1000)
});
timeCounter.text(timeText);
};
ConfTimeKeeper.updateRemainingTime = function() {
var remainTimeHolder = $('#remaining_time div');
var diffTime = ConfTimeKeeper.willEndTime - ConfTimeKeeper.currTime;
if (0 > diffTime) {
$('#contents_canvas').css('background-color', ConfTimeKeeper.style.consts.ALERT_BG_COLOR);
} else if (60000 > diffTime) {
// 60000msec (= 1min.)
$('#contents_canvas').css('background-color', ConfTimeKeeper.style.consts.WARNING_BG_COLOR);
}
var prefix = 'あと';
var suffix = 'です。';
if (0 > diffTime) {
prefix = '';
suffix = 'オーバー';
diffTime *= -1;
diffTime -= 1000;
}
var diffSec = Math.ceil(diffTime / 1000);
var timeText = ConfTimeKeeper.convertTime2Text({
min: Math.floor(diffSec / 60),
sec: diffSec % 60
});
remainTimeHolder.text(prefix + timeText + suffix);
};
ConfTimeKeeper.start = function() {
ConfTimeKeeper.running = !ConfTimeKeeper.running;
if (!ConfTimeKeeper.running) {
return ;
}
if (!ConfTimeKeeper.updateConfTime()) {
// フォームからの時刻取得に失敗した場合は内部状態を戻す
ConfTimeKeeper.running = !ConfTimeKeeper.running;
return ;
}
ConfTimeKeeper.startTime = Date.now();
ConfTimeKeeper.willEndTime = ConfTimeKeeper.startTime;
ConfTimeKeeper.willEndTime += (ConfTimeKeeper.confTime.min * 60 + ConfTimeKeeper.confTime.sec) * 1000;
$('#contents_canvas').css('background-color', ConfTimeKeeper.style.consts.SAFE_BG_COLOR);
ConfTimeKeeper.update();
};
ConfTimeKeeper.pause = function() {
alert('not implemented, yet.');
};
$(function() {
$('#contents').show();
ConfTimeKeeper.resizeContents();
// init.
ConfTimeKeeper.updateConfTime();
ConfTimeKeeper.running = false;
ConfTimeKeeper.style.consts.DEFAULT_BG_COLOR = $('#contents_canvas').css('background-color');
ConfTimeKeeper.style.consts.SAFE_BG_COLOR = '#ccccff';
ConfTimeKeeper.style.consts.WARNING_BG_COLOR = '#ffffcc';
ConfTimeKeeper.style.consts.ALERT_BG_COLOR = '#ff9999';
// event.
$(window).on('resize', ConfTimeKeeper.resizeContents);
$('#start_btn').on('click', ConfTimeKeeper.start);
$('#pause_btn').on('click', ConfTimeKeeper.pause);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment