-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
172 lines (146 loc) · 3.83 KB
/
index.html
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
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Automaton Playground</title>
<style>
@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900');
body {
background: #000;
color: #fff;
font: 300 16px/1.2 "Roboto", sans-serif;
}
a {
color: #9df;
}
#divHeader {
position: fixed;
left: 8px;
top: 8px;
}
#divAutomatonContainer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
height: 240px;
}
#divCanvasContainer {
position: fixed;
left: 0;
top: 0;
width: 50%;
height: calc( 100% - 240px );
display: flex;
justify-content: center;
align-items: center;
}
#selectDescription {
margin: 0 0.5em;
}
#divDropdown {
position: fixed;
width: 50%;
height: 32px;
right: 0;
top: 0;
background: #222;
display: flex;
align-items: center;
}
#editor {
position: fixed;
width: 50%;
height: calc( 100% - 272px );
right: 0;
top: 32px;
}
</style>
<body>
<div id="container">
<div id="divCanvasContainer">
<canvas id="canvas"></canvas>
</div>
<div id="divAutomatonContainer">
</div>
<div id="divDropdown">
<span id="selectDescription">Select an example:</span>
<select id="select">
<option value="demo">Demo</option>
<option value="getting-started">Getting started</option>
<option value="gui">Interact with GUI</option>
<option value="fxs">How to use FXs</option>
<option value="fx-definition">Define your own FX definition</option>
<option value="event">Event system</option>
</select>
</div>
<div id="divHeader">
Automaton Playground -
<a href="https://github.com/0b5vr/automaton">
GitHub
</a>
</div>
<div id="editor">// loading...</div>
</div>
</body>
<script src="https://unpkg.com/ace-builds@1.4.11/src-min-noconflict/ace.js"></script>
<script src="../automaton-fxs/dist/automaton-fxs.min.js"></script>
<script src="./dist/automaton-with-gui.js"></script>
<script>
// editor
const editor = ace.edit( document.getElementById( 'editor' ) );
editor.setTheme( 'ace/theme/monokai' );
editor.setShowPrintMargin( false );
editor.getSession().setMode( 'ace/mode/javascript' );
editor.getSession().setTabSize( 2 );
editor.getSession().setUseSoftTabs( true );
// playground
let playground = {};
function run() {
const source = editor.getValue();
playground.unload && playground.unload();
try {
const func = eval( `{ ${ source } }` );
} catch ( error ) {
console.error( error );
alert( 'An error has occured! See your browser console (Ctrl+Shift+J in chrome).' );
}
};
function update() {
requestAnimationFrame( update );
playground.update && playground.update();
}
update();
editor.commands.addCommand( {
name: 'run',
bindKey: { win: 'Ctrl-S', mac: 'Command-S' },
exec: () => run()
} );
// fetch and run
async function fetchAndRun( name ) {
const code = await ( await fetch( `./playground-examples/${ name }.js` ) ).text();
editor.setValue( code, -1 );
editor.session.getUndoManager().markClean();
run();
select.value = name;
}
// see the hash
const initialExample = location.hash.substring( 1 ) || 'demo';
history.replaceState( { example: select.value }, '', `#${ initialExample }` );
fetchAndRun( initialExample );
// listen to dropdown changes
select.addEventListener( 'change', ( event ) => {
history.pushState( { example: select.value }, '', `#${ select.value }` );
fetchAndRun( select.value );
} );
// listen to change of history
window.addEventListener( 'popstate', ( { state } ) => {
fetchAndRun( state.example );
} );
// prevent terrible consequence
window.addEventListener( 'beforeunload', ( event ) => {
if ( !editor.session.getUndoManager().isClean() ) {
const confirmationMessage = 'You will lose all of your changes on the editor!';
event.returnValue = confirmationMessage;
return confirmationMessage;
}
} );
</script>