-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (140 loc) · 4.9 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
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
const basicCode = `import * as THREE from "three"
import { OrbitControls } from "three/addons/controls/OrbitControls.js"
const { innerWidth, innerHeight } = window;
const scene = new THREE.Scene();
const aspect = innerWidth / innerHeight;
const camera = new THREE.PerspectiveCamera(50, aspect, 0.1, 2000);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(innerWidth, innerHeight);
renderer.setAnimationLoop(animate);
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial();
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
function animate() {
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.01;
render();
}
const render = () => renderer.render(scene, camera);
window.addEventListener("resize", () => {
const { innerWidth, innerHeight } = window;
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});`;
const createHtml = ({ importMap, javascript }) =>
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>playground preview</title>
<style>body { overflow: hidden; margin: 0; background: #000; }</style>
<script>
const errors = [];
window.addEventListener("error", (error) => {
errors.push(error.message);
top.window.postMessage({ type: "error", data: errors }, "*");
});
<\/script>
</head>
<body>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@latest/dist/es-module-shims.js"><\/script>
<script type="importmap">
${JSON.stringify(importMap, null, 4)}
<\/script>
<script type="module">
${javascript}
<\/script>
</body>
</html>`;
const iframe = document.querySelector("iframe");
const container = document.querySelector(".error-container");
const versions = await getVersions();
const latest = versions[0];
createRevision(versions);
const latestImportMap = createImportMap(latest);
let selectImportMap = latestImportMap;
const editor = monaco.editor.create(document.getElementById("monaco-editor"), {
value: [`${basicCode}`].join("\n"),
language: "javascript",
theme: "vs-dark",
});
preview({ javascript: editor.getValue(), importMap: latestImportMap });
function createImportMap(number) {
const version = `0.${number}.0`;
return {
"imports": {
"three": `https://unpkg.com/three@${version}/build/three.module.js`,
"three/addons/": `https://unpkg.com/three@${version}/examples/jsm/`,
"three/": `https://unpkg.com/three@${version}/`,
},
};
}
async function getVersions() {
return await fetch("https://api.github.com/repos/mrdoob/three.js/releases")
.then((res) => res.json())
.then((item) => item.map(({ tag_name }) => tag_name.split("r")[1]));
}
let timer;
editor.onDidChangeModelContent(function (e) {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
container.classList.add("hidden");
preview({ javascript: editor.getValue(), importMap: selectImportMap });
}, 500);
});
async function createRevision(versions) {
const select = document.createElement("select");
select.onchange = (e) => {
const number = e.target.value;
selectImportMap = createImportMap(number);
preview({ javascript: editor.getValue(), importMap: selectImportMap });
};
versions.forEach((item) => {
const number = item.replace("r", "");
const option = document.createElement("option");
option.value = number;
option.innerText = `r${number}`;
select.appendChild(option);
});
const revision = document.createElement("div");
revision.innerText = "REVISION: ";
revision.classList.add("revision");
revision.appendChild(select);
document.body.appendChild(revision);
}
function preview({ javascript, importMap }) {
const html = createHtml({ javascript, importMap });
const blob = new Blob([html], { "type": "text/html" });
iframe.src = URL.createObjectURL(blob);
}
window.addEventListener("resize", () => editor.layout());
function createError(data) {
const fragment = document.createDocumentFragment();
if (Array.isArray(data)) {
data.forEach((error) => appendChild(error));
} else {
appendChild(data);
}
function appendChild(data) {
const div = document.createElement("div");
div.innerText = data;
fragment.appendChild(div);
}
container.innerHTML = "";
container.classList.remove("hidden");
container.appendChild(fragment);
}
window.addEventListener("message", (target) => {
const { data, type } = target.data;
if (type === "error") createError(data);
});