-
Notifications
You must be signed in to change notification settings - Fork 1
/
stretchsite.js
195 lines (159 loc) · 4.78 KB
/
stretchsite.js
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
/*
* decaffeinate suggestions:
* DS207: Consider shorter variations of null checks
* DS208: Avoid top-level this
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const MAX_SCALE_FACTOR = 4;
const FADE_OUT_AFTER_MILLISECONDS = 3*1000;
const sliderForScaleFactor = scaleFactor => Math.log(scaleFactor) / Math.log(MAX_SCALE_FACTOR);
const scaleFactorForSlider = sliderVal => Math.pow(MAX_SCALE_FACTOR, sliderVal);
if (Meteor.isClient) {
this.fadeTimeout = null;
//this function is called by the API
onYouTubeIframeAPIReady = () => {
console.log('onYouTubeIframeAPIReady');
//creates the player object
window.player = new YT.Player('thevideo');
//subscribe to events
player.addEventListener("onReady", "onYouTubePlayerReady");
player.addEventListener("onStateChange", "onYouTubePlayerStateChange");
}
onYouTubePlayerReady = (event) => {
console.log('onYouTubePlayerReady');
// event.target.playVideo();
}
onYouTubePlayerStateChange = (event) => {
// console.log('onYouTubePlayerStateChange');
}
togglePlayPause = ()=> {
if (player.getPlayerState() == YT.PlayerState.PLAYING) {
player.pauseVideo();
} else {
player.playVideo();
}
}
seek = (seconds) => {
player.seekTo(player.getCurrentTime()+seconds, true);
}
toggleMute = () => {
if (player.isMuted()) {
player.unMute();
} else {
player.mute()
}
}
// keypress would be better so we get repeating, but it doesnt work for arrow keys for some odd reason
$('body').keyup(function(event) {
switch (event.key) {
case " ":
togglePlayPause();
break
case "k":
togglePlayPause();
break
case "ArrowLeft":
seek(-5);
break
case "ArrowRight":
seek(5);
break
case "j":
seek(-10);
break
case "l":
seek(10);
break
case "m":
toggleMute();
break
case "ArrowUp":
player.setVolume(player.getVolume()+5)
break
case "ArrowDown":
player.setVolume(player.getVolume()-5)
break
}
})
var resetFade = function(){
clearTimeout(window.fadeTimeout);
$('.content').stop(true);
$('.content').animate({opacity:1},50);
window.fadeTimeout = setTimeout(function(){
if ($('#videourl').val()) {
$('.content').animate({opacity:0},1000);
} else {
resetFade();
}
}
, FADE_OUT_AFTER_MILLISECONDS);
};
Meteor.startup(function(){
const vurl = getParameterByName('videoUrl');
const scale = getParameterByName('scaleFactor');
const zoom = getParameterByName('zoomFactor');
resetFade();
$(window).focus(resetFade);
if (vurl) {
$('#videourl').val(vurl);
updateForVideoUrl();
}
if (zoom) {
$('#zoom').val(sliderForScaleFactor(zoom));
updateForRange(false);
}
if (scale) {
$('#scale').val(sliderForScaleFactor(scale));
updateForRange(false);
}
});
const getId = function(url) {
const regExp = /youtu(?:.*\/v\/|.*v\=|\.be\/)([A-Za-z0-9_\-]{11})/;
const match = url.match(regExp);
if (match && (match[1].length === 11)) {
return match[1];
} else {
return 'error';
}
};
let updateVideoTimeout = null;
var updateForRange = function(changeUrl){
if (changeUrl == null) { changeUrl = true; }
let scale = $('#scale').val();
scale = scaleFactorForSlider(scale);
let zoom = $('#zoom').val();
zoom = scaleFactorForSlider(zoom);
clearTimeout(updateVideoTimeout);
if (changeUrl) {
updateVideoTimeout = setTimeout((function(){
updateQueryStringParameter('scaleFactor',scale.toFixed(3));
updateQueryStringParameter('zoomFactor',zoom.toFixed(3));}), 300);
}
const vid = $('#thevideo');
const transform = `scaleX(${zoom*scale}) scaleY(${zoom})`; //" translateY(#{(zoom-1) * vid.height()/zoom/2}px)"
vid.css({'transform': transform, '-webkit-transform': transform});
};
var updateForVideoUrl = function(){
const val = $('#videourl').val();
updateQueryStringParameter('videoUrl',val);
const myId = getId(val);
$('#ad').hide();
$('iframe').attr('src','//www.youtube.com/embed/' + myId + '?autoplay=1&modestbranding=1&enablejsapi=1&fs=0'); // &origin=stretch.site
};
Template.main.events({
'input #scale, input #zoom': updateForRange,
'mousemove': resetFade,
'focus': resetFade,
'input #videourl': updateForVideoUrl,
'click button'(b){
const v = $(b.currentTarget).data('scale');
$('#scale').val(sliderForScaleFactor(v));
updateForRange();
}
});
}
if (Meteor.isServer) {
Meteor.startup(function() {
// code to run on server at startup
});
}