-
Notifications
You must be signed in to change notification settings - Fork 5
/
page-visibility.html
223 lines (196 loc) · 4.65 KB
/
page-visibility.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Page Visibility API http://www.alloyteam.com/wp-content/uploads/2012/11/page-visibility.html</title>
<style type="text/css">
html, body, p, ul, ol, li, h1, h2, h3, h4, h5, h6 {
margin: 0;
padding: 0;
}
body {
font-family: 'Lucida Console', Verdana, Arial, Helvetica, sans-serif;
color: teal;
background-color: #000;
}
.main {
width: 1240px;
height: 600px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -620px;
margin-top: -300px;
background-color: #DDD;
-webkit-box-shadow: 0 0 50px 5px rgba(255, 255, 255, 0.3);
-moz-box-shadow: 0 0 50px 5px rgba(255, 255, 255, 0.3);
-ms-box-shadow: 0 0 50px 5px rgba(255, 255, 255, 0.3);
-o-box-shadow: 0 0 50px 5px rgba(255, 255, 255, 0.3);
box-shadow: 0 0 50px 5px rgba(255, 255, 255, 0.3);
}
h1 {
text-align: center;
height: 100px;
line-height: 100px;
}
h1 em {
color: #C3325F;
font-size: 14px;
}
canvas {
display: block;
/*border: 1px solid teal;*/
width: 300px;
height: 300px;
margin: 0 auto;
background-color: #FFF;
cursor: pointer;
-webkit-border-radius: 250px;
-moz-border-radius: 250px;
-ms-border-radius: 250px;
-o-border-radius: 250px;
border-radius: 250px;
}
.tips {
text-align: center;
margin-top: 50px;
opacity: 1;
font-size: 24px;
-webkit-transition: opacity 1s linear;
}
.tips.hidden {
opacity: 0;
}
</style>
</head>
<body>
<div class="main">
<h1>HTML5 Page Visibility demo<em> 切换tab再回来看下^_^</em></h1>
<canvas id="canvas" width="300px" height="300px" title="click to stop.">Your browser is not support canvas!</canvas>
<p id="tips" class="tips"></p>
</div>
</body>
<script>
//see http://www.w3.org/TR/page-visibility/
;(function($Win) {
var canvas,
ctx,
width,
height,
radius,
FOCUSCOLOR = '#44A3AA',
HIDDENCOLOE = '#999',
color = FOCUSCOLOR,
FOCUSFPS = 30,
HIDDENFPS = 10,
fps = FOCUSFPS,
PI = Math.PI,
startAngle = 0,
hiddenProp,
bPrefix,
intervalTimer,
timeoutTimer,
tips,
getHiddenProp = function() {
return 'hidden' in document ? 'hidden' : function() {
var r = null;
['webkit', 'moz', 'ms', 'o'].forEach(function(prefix) {
if((prefix + 'Hidden') in document) {
return r = prefix + 'Hidden';
}
});
return r;
}();
},
isHidden = function() {
return !!hiddenProp && !document[hiddenProp];
},
getVisibilityState = function() {
return hiddenProp ? document[bPrefix + 'VisibilityState'] : hiddenProp;
},
canvasInit = function() {
canvas = document.querySelector('#canvas');
ctx = canvas.getContext('2d');
width = canvas.width;
height = canvas.height;
radius = width / 2;
ctx.translate(width / 2, height / 2);
canvas.addEventListener('click', function onCanvasClick(e) {
if(intervalTimer) {
return stop();
}
return run();
});
},
clear = function() {
ctx.clearRect(-width / 2, -height / 2, width, height);
},
draw = function() {
if(startAngle >= 359) {
clear();
}
ctx.save();
ctx.beginPath();
ctx.arc(0, 0, radius, (startAngle/180)*PI, (++startAngle/180)*PI, false);
ctx.lineTo(0, 0);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
ctx.restore();
startAngle %= 360;
console.log(startAngle)
},
run = function() {
intervalTimer = setInterval(draw, 1000/fps);
},
stop = function() {
intervalTimer && clearInterval(intervalTimer);
intervalTimer = null;
},
onVisibilityChange = function(e) {
var state = getVisibilityState();
//state can be one of 'hidden, visible, prerender, unloaded'
if(state === 'hidden') {
color = HIDDENCOLOE;
fps = HIDDENFPS;
timeoutTimer && clearTimeout(timeoutTimer);
timeoutTimer = null;
}
if(state === 'visible') {
color = FOCUSCOLOR;
fps = FOCUSFPS;
showTips();
}
/*if(!isHidden()) {
color = FOCUSCOLOR;
fps = FOCUSFPS;
} else {
color = HIDDENCOLOE;
fps = HIDDENFPS;
}*/
},
showTips = function() {
tips.innerHTML = 'Welcome back!';
tips.classList.remove('hidden');
timeoutTimer = setTimeout(function() {
tips.classList.add('hidden');
}, 1000);
},
init = function() {
canvasInit();
hiddenProp = getHiddenProp();
tips = document.querySelector('#tips');
if(hiddenProp) {
bPrefix = hiddenProp.substring(0, hiddenProp.length - 6);
document.addEventListener(bPrefix + 'visibilitychange', onVisibilityChange);
run();
//document.addEventListener('click', draw);
} else {
tips.innerHTML = '<em style="color: red;">Your browser is not support Page Visibility API yet!</em>';
}
};
// http://jeff2ma.github.io/Demo/201503/VisibilityViedo.html
$Win.addEventListener('load', init);
}(this));
</script>
</html>