This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (80 loc) · 2.39 KB
/
index.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
require('document-register-element');
require('@parametric-svg/element');
const mousetrap = require('mousetrap');
const display = document.body;
const viewBox = (slideNumber) => (
`0 ${slideNumber * 2000} 1920 1080`
);
fetch('slides.svg').then(
response => response.text()
).then((slides) => {
// VIEW
const parametricSvg = document.createElement('parametric-svg');
parametricSvg.innerHTML = slides;
display.appendChild(parametricSvg);
const svg = display.querySelector('svg');
const refreshView = {
set(target, key, value) {
if (key === 'slide' && target.slide !== value)
svg.setAttribute('viewBox', viewBox(value));
if (key === 'layer' && target.layer !== value)
parametricSvg.setAttribute('layer', value);
target[key] = value; // eslint-disable-line no-param-reassign
return true;
},
};
// MODEL
const state = new Proxy({}, refreshView);
setTimeout(() => {
state.slide =
0;
state.layer =
0;
});
// UPDATE
const increment = (what) => {
state[what] += 1;
};
const decrement = (what) => {
state[what] = Math.max(
state[what] - 1,
0
);
};
// Change slide
const changeSlide = (mutation) => (event) => {
mutation('slide');
state.layer = 0;
if (!!event) event.preventDefault();
};
const nextSlide = changeSlide(increment);
const previousSlide = changeSlide(decrement);
display.addEventListener('wheel', (event) => {
(event.deltaY > 0 ?
nextSlide :
previousSlide
)();
});
mousetrap.bind(['right', 'space', 'pagedown'], nextSlide);
mousetrap.bind(['left', 'shift+space', 'pageup'], previousSlide);
// Change layer
const nextLayer = (event) => {
if (!!event && event.type === 'click' && event.which !== 1) return;
if (!!event) event.preventDefault();
increment('layer');
};
display.addEventListener('click', nextLayer);
mousetrap.bind('down', nextLayer);
const previousLayer = (event) => {
if (!!event) event.preventDefault();
decrement('layer');
};
display.addEventListener('contextmenu', previousLayer);
mousetrap.bind('up', previousLayer);
// Fullscreen
mousetrap.bind('f', () => {
if (display.requestFullscreen) display.requestFullscreen();
else if (display.webkitRequestFullscreen) display.webkitRequestFullscreen();
else if (display.mozRequestFullScreen) display.mozRequestFullScreen();
});
});