-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
162 lines (140 loc) · 4.96 KB
/
main.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
import { makeSimpleUi, setupDomForVariant, UIVariant } from '@andreaskundig/looper-ui';
import { io, urlUtils, makeLooper } from '@andreaskundig/looper';
import miraMakeExportAndInfoUi from './mira-export-info-ui.js'; // ??
import paper from 'paper/dist/paper-core';
console.log(`bouboucle mirabilia version ${__APP_VERSION__}`);
async function loadImage(url) {
return new Promise((resolve, reject) => {
const image = new Image();
image.onload = () => resolve(image);
image.onerror = reject;
image.src = url;
});
}
const config = {
variant: "default",
newTiming: false,
backgroundColor: "#ffffff",
};
// integration on the mirabilia size entails the handling of a
// mirabilia specific header. This is handled here
function addTopPadding(paddingPx, titleHeight){
document.querySelector('.menu').style.paddingTop = `${paddingPx}px`;
const applyCssPatch = element => {
element.style.top = `${paddingPx + titleHeight}px`;
}
let itemsClasses = [
'#overlay',
'#color-submenu',
'#timing-submenu',
'#stroke-submenu',
'#info-submenu',
'#export-submenu',
'#dialog-submenu',
'#canvas-parent',
];
const elements = itemsClasses.map(n => document.querySelector(n));
for(const element of elements){
applyCssPatch(element);
}
}
function extractForegroundUrl(){
return new URL(location.href).searchParams.get('fg-url');
}
// returns a dimension -> { width: number, height: number }
async function computeDimensionFromFgUrl(foregroundUrl, titleHeight){
if(foregroundUrl){
try {
const foregroundImage = await loadImage(foregroundUrl);
const targetHeight = window.innerHeight - titleHeight;
const scale = targetHeight / foregroundImage.naturalHeight;
return {
foregroundUrlIsValid: true,
dimension: {
width: foregroundImage.naturalWidth * scale,
height: foregroundImage.naturalHeight * scale,
},
};
}catch(e){
console.error(`unable to load fg-url ${foregroundUrl}`, e);
}
}
return {
foregroundUrlIsValid: false,
dimension:{
width: window.innerWidth,
height: window.innerHeight - titleHeight
},
};
}
async function main(){
const variant = config.variant || UIVariant.default;
// 1 choose ui variant and setup dom accordingly
const mirabiliaButtonOrder = [
"info-button",
"color-button",
"stroke-button",
"timing-button",
"clear-button",
"undo-button",
"redo-button",
"pause-button",
"export-button",
];
setupDomForVariant(variant, undefined, mirabiliaButtonOrder);
const mql = window.matchMedia("only screen and (min-width: 320px) and (max-width: 850px)");
const isMobile = mql.matches;
// 2 setup looper
const urlParams = urlUtils.getUrlParams(location.href);
const newTiming = 'new-timing' in urlParams || config.newTiming;
const ratio = urlParams.ratio || config.ratio;
const backgroundColor =
urlParams['background-color'] || config.backgroundColor || '#ffffff';
const showGallery = !!urlParams.gallery;
const titleHeight = 79.67; // ui button row height (px)
// height of external mirabilia header height (px)
const mirabiliaHeaderHeight = isMobile ? 0 : 79;
const fullSizeGif = !!urlParams['big-gif'];
const foregroundUrl = extractForegroundUrl() || null;
const { dimension, foregroundUrlIsValid } =
await computeDimensionFromFgUrl(foregroundUrl, (titleHeight + mirabiliaHeaderHeight));
const graphics = {
canvas: document.getElementById('main-canvas'),
paper: paper,
};
const looperConfig = Object.assign({
graphics: graphics,
backgroundColor: backgroundColor,
foregroundUrl: foregroundUrlIsValid ? foregroundUrl : null,
}, dimension);
if (ratio) {
looperConfig.ratio = eval(ratio);
}
const looper = makeLooper(looperConfig);
looper.setLineColor('#E1BEE7');
looper.start();
if (urlParams.gist) {
io.gists.load(urlParams.gist, looper.importData);
}
function makeExportAndInfoUi(menu, looper) {
miraMakeExportAndInfoUi(menu, looper, undefined, fullSizeGif)
}
makeSimpleUi(looper, makeExportAndInfoUi, newTiming, dimension,
showGallery);
window.addEventListener('resize', () => {
looper.scale({
width: window.innerWidth,
height: window.innerHeight - (titleHeight + mirabiliaHeaderHeight),
ratio,
});
});
addTopPadding(mirabiliaHeaderHeight, titleHeight);
// TODO: improve
// force scale to setup correct first frame display
looper.scale({
width: window.innerWidth,
height: window.innerHeight - (titleHeight + mirabiliaHeaderHeight),
ratio,
});
}
main();