forked from lettucegoblin/AnimRef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
186 lines (160 loc) · 6.3 KB
/
renderer.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
const sliderElement = document.querySelector('#slider')
noUiSlider.create(sliderElement, {
start: [0, 100],
connect: true,
behaviour: 'unconstrained-tap',
range: {
'min': 0,
'max': 100
}
});
slider.noUiSlider.on('start', function () {
});
let scalingWithTouch = false
let lastDelta = 0
let initTouches = []
//ontouchstart
document.addEventListener('touchstart', function (e) {
if (e.touches.length > 1) {
scalingWithTouch = true
initTouches = e.touches
}
}, true);
document.addEventListener('touchend', function (e) {
if (e.touches.length < 2) {
scalingWithTouch = false
}
}, true);
document.addEventListener('touchmove', function (e) {
if (scalingWithTouch) {
//zoom
// delta between e.touches[1], e.touches[0] and initTouches[1], initTouches[0]
let delta = Math.sqrt(Math.pow(e.touches[0].clientX - e.touches[1].clientX, 2) + Math.pow(e.touches[0].clientY - e.touches[1].clientY, 2)) - Math.sqrt(Math.pow(initTouches[0].clientX - initTouches[1].clientX, 2) + Math.pow(initTouches[0].clientY - initTouches[1].clientY, 2))
let deltaDiff = delta - lastDelta
deltaDiff = deltaDiff / 100
currentScale = parseFloat(document.body.dataset.currentScale) || 1
const nextScale = Math.max(currentScale + deltaDiff * (currentScale / 2), 0.01)
console.log("touchmove", deltaDiff, currentScale, nextScale)
e.clientX = (initTouches[0].clientX + initTouches[0].clientX) / 2
e.clientY = (initTouches[0].clientY + initTouches[0].clientY) / 2
//zoom(nextScale, e) // this is not working
lastDelta = delta
}
}, true);
const factor = 0.1
document.documentElement.addEventListener("wheel", (e) => {
let delta = e.wheelDelta / 120
if (e.ctrlKey) { // is pinch zoom on touchpad(idk why it's ctrlKey but it is)
delta = e.deltaY * factor
}
if (document.querySelector('.editVideo')) return;
currentScale = parseFloat(document.body.dataset.currentScale) || 1
const nextScale = Math.max(currentScale + delta * (currentScale / 2), 0.01)
console.log("wheel", delta, currentScale, nextScale)
zoom(nextScale, e)
})
const zoom = (nextScale, event) => {
currentScale = parseFloat(document.body.dataset.currentScale) || 1
const ratio = 1 - nextScale / currentScale
const {
clientX,
clientY
} = event
let translateX = (parseFloat(document.body.dataset.translateX) || 0);
let translateY = (parseFloat(document.body.dataset.translateY) || 0);
translateX += (clientX - translateX) * ratio
translateY += (clientY - translateY) * ratio
console.log('zoom')
//console.log(translateX, translateY, nextScale, ratio, currentScale)
currentScale = nextScale
myAPI.updateScaleAndTranslate(currentScale, { translateX, translateY })
}
/**
* Youtube Embed Section
*/
var player;
function onYouTubeIframeAPIReady() { } // idk why it breaks without this
function onPlayerReady(event) {
event.target.setVolume(0);
event.target.mute();
// lifts youtube video to last element at the root of body
event.target.getIframe().contentDocument.body.appendChild(player.getIframe().contentDocument.querySelector('video'))
event.target.playVideo();
}
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
//setTimeout(stopVideo, 6000);
let videoElement = event.target.getIframe().contentDocument.querySelector('video')
videoElement.loop = true
/*
videoElement.addEventListener('timeupdate', function(){
//if im the editvideo
if(document.querySelector(`.editVideo[data-idcode="${this.dataset.idcode}"]`)){
onPlayerProgress.apply(this, arguments)
}
})*/
let videoWidth = videoElement.videoWidth
let videoHeight = videoElement.videoHeight
//console.log('inside onPlayerStateChange', videoWidth, videoHeight)
let idcode = event.target.getIframe().parentElement.dataset.idcode
videoElement.dataset.idcode = idcode
//console.log('after onPlayerStateChange', videoElement.dataset.idcode, videoWidth, videoHeight)
myAPI.updateYoutubeOriginalSize(idcode, videoWidth, videoHeight)
//event.target.getIframe().parentElement.style.width = videoWidth + 'px'
//event.target.getIframe().parentElement.style.height = videoHeight + 'px'
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
const targetNode = document.getElementById('eventTrigger');
const config = { attributes: true };
const callback = function (mutationsList, observer) {
// Use traditional 'for loops' for IE 11
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
console.log('A child node has been added or removed.');
}
else if (mutation.type === 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
if (mutation.attributeName == 'data-youtubetrigger') {
embedYoutubeVideo()
} else if (mutation.attributeName == 'data-changesliders') {
//console.log(mutation)
//
let changesliders = JSON.parse(mutation.target.dataset.changesliders)
slider.noUiSlider.set(changesliders);
}
}
}
};
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
function embedYoutubeVideo() {
var playerNeedsSetup = document.querySelector('.playerNeedsSetup');
if (!playerNeedsSetup) {
console.error('embedYoutubeVideo called without player div to setup')
};
playerNeedsSetup.classList.remove('playerNeedsSetup')
var iframediv = playerNeedsSetup.querySelector('.iframeDiv')
var code = playerNeedsSetup.dataset.idcode
//document.querySelector()
player = new YT.Player(iframediv, {
videoId: code,
playerVars: {
//'playsinline': 1
'controls': 0,
'disablekb': 1,
'enablejsapi': 1,
'fs': 0,
'loop': 1
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}