codemotion-studio 0.0.0-stage → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -2
- package/dist/assets/index-BwhV_v57.css +1 -0
- package/dist/assets/index-DTOLp_VG.js +763 -0
- package/dist/index.html +13 -0
- package/dist/vendor/babel.min.js +4 -0
- package/dist/vendor/react-dom.development.js +29924 -0
- package/dist/vendor/react-dom.production.min.js +267 -0
- package/dist/vendor/react.development.js +3343 -0
- package/dist/vendor/react.production.min.js +31 -0
- package/index.html +12 -0
- package/package.json +55 -4
- package/public/vendor/babel.min.js +4 -0
- package/public/vendor/react-dom.development.js +29924 -0
- package/public/vendor/react-dom.production.min.js +267 -0
- package/public/vendor/react.development.js +3343 -0
- package/public/vendor/react.production.min.js +31 -0
- package/scripts/copy-vendor.mjs +25 -0
- package/server/cli.js +41 -0
- package/server/index.js +263 -0
- package/server/renderer.js +180 -0
- package/src/lib/constants.js +23 -0
- package/src/lib/renderClient.js +52 -0
- package/src/lib/sceneDocument.js +250 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// Builds a fully standalone HTML document for one scene.
|
|
2
|
+
// Used by the studio iframe (srcDoc) AND by the server renderer, so the
|
|
3
|
+
// rendered video matches the preview pixel for pixel.
|
|
4
|
+
|
|
5
|
+
const VENDOR = {
|
|
6
|
+
reactDev: '/vendor/react.development.js',
|
|
7
|
+
reactProd: '/vendor/react.production.min.js',
|
|
8
|
+
reactDomDev: '/vendor/react-dom.development.js',
|
|
9
|
+
reactDomProd: '/vendor/react-dom.production.min.js',
|
|
10
|
+
babel: '/vendor/babel.min.js'
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function buildSceneDocument(scene, options = {}) {
|
|
14
|
+
const {
|
|
15
|
+
width = 1920,
|
|
16
|
+
height = 1080,
|
|
17
|
+
fps = 30,
|
|
18
|
+
assets = [],
|
|
19
|
+
mode = 'preview',
|
|
20
|
+
baseUrl = ''
|
|
21
|
+
} = options;
|
|
22
|
+
|
|
23
|
+
const isReact = scene.mode === 'react';
|
|
24
|
+
const prod = mode === 'render';
|
|
25
|
+
|
|
26
|
+
const applyAssets = (text = '') =>
|
|
27
|
+
assets.reduce((acc, asset) => acc.split(`asset://${asset.name}`).join(asset.url), text);
|
|
28
|
+
|
|
29
|
+
const css = applyAssets(scene.css || '');
|
|
30
|
+
const js = applyAssets(scene.js || '');
|
|
31
|
+
const jsx = applyAssets(scene.jsx || '');
|
|
32
|
+
const html = applyAssets(scene.html || '');
|
|
33
|
+
|
|
34
|
+
const fontFaces = assets
|
|
35
|
+
.filter((a) => a.category === 'font')
|
|
36
|
+
.map((f) => `@font-face { font-family: '${f.name.replace(/\.[^/.]+$/, '')}'; src: url('${f.url}'); }`)
|
|
37
|
+
.join('\n');
|
|
38
|
+
|
|
39
|
+
const durationInFrames = Math.max(1, Math.round((scene.duration || 1) * fps));
|
|
40
|
+
const react = (path) => `${baseUrl}${path}`;
|
|
41
|
+
|
|
42
|
+
const vendorTags = isReact
|
|
43
|
+
? `<script src="${react(prod ? VENDOR.reactProd : VENDOR.reactDev)}"></script>
|
|
44
|
+
<script src="${react(prod ? VENDOR.reactDomProd : VENDOR.reactDomDev)}"></script>
|
|
45
|
+
<script src="${react(VENDOR.babel)}"></script>`
|
|
46
|
+
: '';
|
|
47
|
+
|
|
48
|
+
const runtime = `
|
|
49
|
+
(function () {
|
|
50
|
+
var state = {
|
|
51
|
+
frame: 0,
|
|
52
|
+
fps: ${fps},
|
|
53
|
+
width: ${width},
|
|
54
|
+
height: ${height},
|
|
55
|
+
durationInFrames: ${durationInFrames}
|
|
56
|
+
};
|
|
57
|
+
window.__FC_STATE = state;
|
|
58
|
+
window.__FC_READY = false;
|
|
59
|
+
|
|
60
|
+
window.useCurrentFrame = function () { return state.frame; };
|
|
61
|
+
window.useVideoConfig = function () { return state; };
|
|
62
|
+
|
|
63
|
+
window.interpolate = function (val, inputRange, outputRange, options) {
|
|
64
|
+
options = options || {};
|
|
65
|
+
var n = inputRange.length - 1;
|
|
66
|
+
var out;
|
|
67
|
+
if (val <= inputRange[0]) {
|
|
68
|
+
out = outputRange[0];
|
|
69
|
+
if (options.extrapolateLeft !== 'clamp' && n > 0) {
|
|
70
|
+
out = outputRange[0] + ((val - inputRange[0]) / (inputRange[1] - inputRange[0] || 1)) * (outputRange[1] - outputRange[0]);
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
if (val >= inputRange[n]) {
|
|
75
|
+
out = outputRange[n];
|
|
76
|
+
if (options.extrapolateRight !== 'clamp' && n > 0) {
|
|
77
|
+
out = outputRange[n] + ((val - inputRange[n]) / (inputRange[n] - inputRange[n - 1] || 1)) * (outputRange[n] - outputRange[n - 1]);
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
for (var i = 0; i < n; i++) {
|
|
82
|
+
if (val >= inputRange[i] && val <= inputRange[i + 1]) {
|
|
83
|
+
var p = (val - inputRange[i]) / (inputRange[i + 1] - inputRange[i] || 1);
|
|
84
|
+
return outputRange[i] + p * (outputRange[i + 1] - outputRange[i]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return outputRange[0];
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
window.spring = function (params) {
|
|
91
|
+
params = params || {};
|
|
92
|
+
var frame = params.frame || 0;
|
|
93
|
+
var fpsVal = params.fps || state.fps;
|
|
94
|
+
var config = params.config || {};
|
|
95
|
+
if (frame <= 0) return 0;
|
|
96
|
+
var damping = config.damping || 10;
|
|
97
|
+
var t = frame / fpsVal;
|
|
98
|
+
var value = 1 - Math.exp(-damping * t) * Math.cos(10 * t);
|
|
99
|
+
return value < 0 ? 0 : value;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
if (window.React) {
|
|
103
|
+
window.AbsoluteFill = function (props) {
|
|
104
|
+
return React.createElement('div', {
|
|
105
|
+
style: Object.assign({
|
|
106
|
+
position: 'absolute', top: 0, left: 0, right: 0, bottom: 0,
|
|
107
|
+
display: 'flex', flexDirection: 'column'
|
|
108
|
+
}, props.style || {})
|
|
109
|
+
}, props.children);
|
|
110
|
+
};
|
|
111
|
+
window.Sequence = function (props) {
|
|
112
|
+
var from = props.from || 0;
|
|
113
|
+
var duration = props.durationInFrames || Infinity;
|
|
114
|
+
var current = state.frame;
|
|
115
|
+
if (current < from || current >= from + duration) return null;
|
|
116
|
+
return props.children;
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
window.__FC_report = function (message) {
|
|
121
|
+
try { window.parent.postMessage({ type: 'IFRAME_ERROR', message: String(message) }, '*'); } catch (e) {}
|
|
122
|
+
};
|
|
123
|
+
window.addEventListener('error', function (e) {
|
|
124
|
+
window.__FC_report(e.message || 'Script error');
|
|
125
|
+
});
|
|
126
|
+
window.addEventListener('unhandledrejection', function (e) {
|
|
127
|
+
window.__FC_report((e.reason && e.reason.message) || e.reason || 'Unhandled rejection');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
window.__FC_markReady = function () {
|
|
131
|
+
if (window.__FC_READY) return;
|
|
132
|
+
window.__FC_READY = true;
|
|
133
|
+
try { window.parent.postMessage({ type: 'IFRAME_READY' }, '*'); } catch (e) {}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
window.__FC_setFrame = function (localTime, progress, frame, transitionStyle) {
|
|
137
|
+
state.frame = frame;
|
|
138
|
+
document.documentElement.style.setProperty('--time', String(localTime));
|
|
139
|
+
document.documentElement.style.setProperty('--progress', String(progress));
|
|
140
|
+
document.documentElement.style.setProperty('--frame', String(frame));
|
|
141
|
+
if (transitionStyle) {
|
|
142
|
+
document.body.style.opacity = String(transitionStyle.opacity);
|
|
143
|
+
document.body.style.transform = transitionStyle.transform;
|
|
144
|
+
}
|
|
145
|
+
if (typeof window.__FC_renderReact === 'function') {
|
|
146
|
+
try { window.__FC_renderReact(); } catch (err) { window.__FC_report(err.message); }
|
|
147
|
+
}
|
|
148
|
+
if (typeof window.onFrameUpdate === 'function') {
|
|
149
|
+
try { window.onFrameUpdate(localTime, progress, frame, state.fps); } catch (err) { window.__FC_report(err.message); }
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
window.addEventListener('message', function (event) {
|
|
154
|
+
if (!event.data || event.data.type !== 'SYNC_TIME') return;
|
|
155
|
+
window.__FC_setFrame(event.data.localTime, event.data.progress, event.data.frame, event.data.transitionStyle);
|
|
156
|
+
});
|
|
157
|
+
})();
|
|
158
|
+
`;
|
|
159
|
+
|
|
160
|
+
const reactBoot = `
|
|
161
|
+
try {
|
|
162
|
+
${jsx}
|
|
163
|
+
|
|
164
|
+
var __fcRootEl = document.getElementById('root');
|
|
165
|
+
var __fcRoot = ReactDOM.createRoot(__fcRootEl);
|
|
166
|
+
var __fcComp =
|
|
167
|
+
(typeof MyComposition !== 'undefined') ? MyComposition :
|
|
168
|
+
(typeof CardsScene !== 'undefined') ? CardsScene :
|
|
169
|
+
(typeof VerticalReel !== 'undefined') ? VerticalReel :
|
|
170
|
+
null;
|
|
171
|
+
|
|
172
|
+
window.__FC_renderReact = function () {
|
|
173
|
+
if (!__fcComp) return;
|
|
174
|
+
var tree = React.createElement(__fcComp);
|
|
175
|
+
if (ReactDOM.flushSync) {
|
|
176
|
+
ReactDOM.flushSync(function () { __fcRoot.render(tree); });
|
|
177
|
+
} else {
|
|
178
|
+
__fcRoot.render(tree);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
if (!__fcComp) {
|
|
183
|
+
document.body.innerHTML = '<div style="color:#f87171;font:600 28px monospace;padding:60px">No React component found. Export a component named MyComposition.</div>';
|
|
184
|
+
throw new Error('No React component found in this scene. Name it MyComposition.');
|
|
185
|
+
}
|
|
186
|
+
window.__FC_renderReact();
|
|
187
|
+
(document.fonts ? document.fonts.ready : Promise.resolve()).then(window.__FC_markReady);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
window.__FC_report(err && err.message ? err.message : err);
|
|
190
|
+
(document.fonts ? document.fonts.ready : Promise.resolve()).then(window.__FC_markReady);
|
|
191
|
+
}
|
|
192
|
+
`;
|
|
193
|
+
|
|
194
|
+
const htmlBoot = `
|
|
195
|
+
try {
|
|
196
|
+
${js}
|
|
197
|
+
} catch (err) {
|
|
198
|
+
window.__FC_report(err && err.message ? err.message : err);
|
|
199
|
+
}
|
|
200
|
+
(document.fonts ? document.fonts.ready : Promise.resolve()).then(window.__FC_markReady);
|
|
201
|
+
`;
|
|
202
|
+
|
|
203
|
+
return `<!DOCTYPE html>
|
|
204
|
+
<html>
|
|
205
|
+
<head>
|
|
206
|
+
<meta charset="utf-8" />
|
|
207
|
+
${vendorTags}
|
|
208
|
+
<style>
|
|
209
|
+
${fontFaces}
|
|
210
|
+
html, body {
|
|
211
|
+
width: ${width}px;
|
|
212
|
+
height: ${height}px;
|
|
213
|
+
margin: 0;
|
|
214
|
+
padding: 0;
|
|
215
|
+
overflow: hidden;
|
|
216
|
+
background: #000;
|
|
217
|
+
}
|
|
218
|
+
${css}
|
|
219
|
+
</style>
|
|
220
|
+
</head>
|
|
221
|
+
<body>
|
|
222
|
+
${isReact ? '<div id="root"></div>' : html}
|
|
223
|
+
<script>${runtime}</script>
|
|
224
|
+
<script${isReact ? ' type="text/babel"' : ''}>${isReact ? reactBoot : htmlBoot}</script>
|
|
225
|
+
</body>
|
|
226
|
+
</html>`;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Transition entrance state for a scene, shared by preview and renderer.
|
|
230
|
+
export function transitionAt(scene, localTime) {
|
|
231
|
+
const duration = scene.transitionDuration || 0.5;
|
|
232
|
+
let opacity = 1;
|
|
233
|
+
let transform = 'none';
|
|
234
|
+
if (localTime < duration && scene.transition && scene.transition !== 'none') {
|
|
235
|
+
const t = Math.max(0, localTime / duration);
|
|
236
|
+
if (scene.transition === 'fade') {
|
|
237
|
+
opacity = t;
|
|
238
|
+
} else if (scene.transition === 'slide-left') {
|
|
239
|
+
opacity = t;
|
|
240
|
+
transform = `translateX(${(1 - t) * 120}px)`;
|
|
241
|
+
} else if (scene.transition === 'slide-up') {
|
|
242
|
+
opacity = t;
|
|
243
|
+
transform = `translateY(${(1 - t) * 120}px)`;
|
|
244
|
+
} else if (scene.transition === 'zoom') {
|
|
245
|
+
opacity = t;
|
|
246
|
+
transform = `scale(${0.88 + t * 0.12})`;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return { opacity, transform };
|
|
250
|
+
}
|