-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
180 lines (160 loc) · 5.13 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
<script src="p5.sound.js"></script>
<script>
var pomodoromode=false;
var modes=["60x60","25x25","45x10","1x1","0.1x1","90x90"];//add modes here
var buttons=[];
var work,rest;
var working=true;//false=resting
var currentButtonsAmount=0;
var buttonWidth,buttonHeight;//global
var startTime;
var endTime;
var doOnce=false;//for pomodoro screen
var doOnce2=false;//for start screen
var theText;//for pomodoro screen - global
var timeMode=1;//0=1hours,60minutes,3600seconds / 1=1hours,0minutes,0seconds
var timeLeft;
var bg;//global bg color
var _reset=false;
var soundEffect;
function setup() {
buttonWidth=windowWidth/5;
buttonHeight=windowHeight/10;
createCanvas(windowWidth, windowHeight);
soundEffect = loadSound('sound effect.mp3');
{//setup screen
background(200);
textAlign(CENTER);
var buttonsAmount=modes.length;
//create starting buttons
for(var i=0;i<buttonsAmount;i++){
buttons[i] = createButton(modes[i]);
buttons[i].position(windowWidth/2-buttonWidth/2, (windowHeight/2)+(i*buttonHeight)-(buttonsAmount*buttonHeight/2));
buttons[i].size(buttonWidth,buttonHeight);
buttons[i].mousePressed((function(index){
return function(){
setMode(modes[index]);
}
})(i));//IIFE
}
}
}
function draw() {
if(_reset){//executes once to reset program
//console.log("asldkas");
buttons.forEach((b)=>{b.remove()});
pomodoromode=false;
timeMode=1;
clear();
{//setup screen
background(200);
textAlign(CENTER);
var buttonsAmount=modes.length;
//create starting buttons
for(var i=0;i<buttonsAmount;i++){
buttons[i] = createButton(modes[i]);
buttons[i].position(windowWidth/2-buttonWidth/2, (windowHeight/2)+(i*buttonHeight)-(buttonsAmount*buttonHeight/2));
buttons[i].size(buttonWidth,buttonHeight);
buttons[i].mousePressed((function(index){
return function(){
setMode(modes[index]);
}
})(i));//IIFE
}
}
_reset=false;
}
if(bg=="grey"){
background(color(30));
}
if(pomodoromode){
pomodoro(work,rest);
}else{
}
}
function mouseClicked(){
if(pomodoromode){
timeMode=!timeMode;
}
}
function setMode(_mode){
m=1;
work=_mode.substring(0,_mode.indexOf("x"));
rest=_mode.substring(_mode.indexOf("x") + 1);
//console.log(work+"+"+rest);
buttons.forEach((b)=>{b.remove()});
currentButtonsAmount=1;
buttons[0]=makeButton("start",1,()=>{doOnce=true;pomodoromode=true});
}
function pomodoro(_work,_rest){//int,int
textSize((windowWidth+windowHeight)/2/25);
if(doOnce){
buttons.forEach((b)=>{b.remove()});
startTime=getTime();
endTime=getTargetTime((working==true)?_work:_rest);
currentButtonsAmount=6;
buttons[0]=makeButton("Reset",6,()=>{_reset=true;});
soundEffect.play();
doOnce=false;
}
background((working==true)?color(0,255,0):color(0,0,255));
timeLeft=getTimeDifference(millis()/1000,endTime);
if(timeLeft[2]<=0){//negative seconds left
working=!working;//switch modes
doOnce=true;//recalculate time to finish
}
if(timeMode==0){//set text based on display mode
theText=text(((working==true)?"Working":"Resting")+" for "+((working==true)?_work:_rest)+" minutes. \n"
+timeLeft[0]+" hours remaining. \n"
+timeLeft[1]+" minutes remaining. \n"
+timeLeft[2]+" seconds remaining. \n"
,windowWidth/2,windowHeight/2);
}else{//=1
var secondsfixed=(timeLeft[2]>=60)?timeLeft[2]%60:timeLeft[2];
secondsfixed=parseFloat(secondsfixed).toFixed(2);
theText=text(((working==true)?"Working":"Resting")+" for "+((working==true)?_work:_rest)+" minutes. \n"
+timeLeft[0]+" hours and\n"
+((timeLeft[1]>=60)?timeLeft[1]%60:timeLeft[1])+" minutes and\n"
+secondsfixed+" seconds remaining. \n"
,windowWidth/2,windowHeight/2);
}
}
function makeButton(txt,totalButtons,mPressed){//string,int,function
//totalButtons=intended total number of buttons
//currentButtonsAmount=the current number of buttons on screen before creation of this one
var tmp;
tmp = createButton(txt);
tmp.position(windowWidth/2-buttonWidth/2, (windowHeight/2)+(currentButtonsAmount*buttonHeight)-(totalButtons*buttonHeight/2));
tmp.size(buttonWidth,buttonHeight);
tmp.mousePressed(mPressed);//function
currentButtonsAmount++;
return tmp;
}
function getTime(){//get time in seconds since start of program
return millis()/1000;
}
function getTargetTime(mins){//mins=minutes in the future
return millis()/1000+(mins*60);//returns time in seconds from setup() to target time
}
function getTimeDifference(current,target){//params are seconds
var temp=[];//0=hours,1=minutes,2=seconds
var diff=(target-current)//difference in seconds
temp[0] = Math.floor(diff/60/60);//hours
temp[1] = Math.floor(diff/60);//minutes
temp[2] = diff;//seconds
temp[2] = temp[2].toFixed(2);//seconds to two decimal places
return temp;
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
</script>
</body>
</html>