threejs-voxel-geometry-culled-faces-ui.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Voxel Geometry - UI</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. #ui {
  18. position: absolute;
  19. left: 10px;
  20. top: 10px;
  21. background: rgba(0, 0, 0, 0.8);
  22. padding: 5px;
  23. }
  24. #ui input[type=radio] {
  25. width: 0;
  26. height: 0;
  27. display: none;
  28. }
  29. #ui input[type=radio] + label {
  30. background-image: url('resources/images/minecraft/flourish-cc-by-nc-sa.png');
  31. background-size: 1600% 400%;
  32. image-rendering: pixelated;
  33. width: 64px;
  34. height: 64px;
  35. display: inline-block;
  36. }
  37. #ui input[type=radio]:checked + label {
  38. outline: 3px solid red;
  39. }
  40. @media (max-width: 600px), (max-height: 600px) {
  41. #ui input[type=radio] + label {
  42. width: 32px;
  43. height: 32px;
  44. }
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <canvas id="c"></canvas>
  50. <div id="ui">
  51. <div class="tiles">
  52. <input type="radio" name="voxel" id="voxel1" value="1"><label for="voxel1" style="background-position: -0% -0%"></label>
  53. <input type="radio" name="voxel" id="voxel2" value="2"><label for="voxel2" style="background-position: -100% -0%"></label>
  54. <input type="radio" name="voxel" id="voxel3" value="3"><label for="voxel3" style="background-position: -200% -0%"></label>
  55. <input type="radio" name="voxel" id="voxel4" value="4"><label for="voxel4" style="background-position: -300% -0%"></label>
  56. <input type="radio" name="voxel" id="voxel5" value="5"><label for="voxel5" style="background-position: -400% -0%"></label>
  57. <input type="radio" name="voxel" id="voxel6" value="6"><label for="voxel6" style="background-position: -500% -0%"></label>
  58. <input type="radio" name="voxel" id="voxel7" value="7"><label for="voxel7" style="background-position: -600% -0%"></label>
  59. <input type="radio" name="voxel" id="voxel8" value="8"><label for="voxel8" style="background-position: -700% -0%"></label>
  60. </div>
  61. <div class="tiles">
  62. <input type="radio" name="voxel" id="voxel9" value="9" ><label for="voxel9" style="background-position: -800% -0%"></label>
  63. <input type="radio" name="voxel" id="voxel10" value="10"><label for="voxel10" style="background-position: -900% -0%"></label>
  64. <input type="radio" name="voxel" id="voxel11" value="11"><label for="voxel11" style="background-position: -1000% -0%"></label>
  65. <input type="radio" name="voxel" id="voxel12" value="12"><label for="voxel12" style="background-position: -1100% -0%"></label>
  66. <input type="radio" name="voxel" id="voxel13" value="13"><label for="voxel13" style="background-position: -1200% -0%"></label>
  67. <input type="radio" name="voxel" id="voxel14" value="14"><label for="voxel14" style="background-position: -1300% -0%"></label>
  68. <input type="radio" name="voxel" id="voxel15" value="15"><label for="voxel15" style="background-position: -1400% -0%"></label>
  69. <input type="radio" name="voxel" id="voxel16" value="16"><label for="voxel16" style="background-position: -1500% -0%"></label>
  70. </div>
  71. </div>
  72. </body>
  73. <script type="module">
  74. import * as THREE from './resources/threejs/r114/build/three.module.js';
  75. import {OrbitControls} from './resources/threejs/r114/examples/jsm/controls/OrbitControls.js';
  76. class VoxelWorld {
  77. constructor(options) {
  78. this.cellSize = options.cellSize;
  79. this.tileSize = options.tileSize;
  80. this.tileTextureWidth = options.tileTextureWidth;
  81. this.tileTextureHeight = options.tileTextureHeight;
  82. const {cellSize} = this;
  83. this.cellSliceSize = cellSize * cellSize;
  84. this.cells = {};
  85. }
  86. computeVoxelOffset(x, y, z) {
  87. const {cellSize, cellSliceSize} = this;
  88. const voxelX = THREE.MathUtils.euclideanModulo(x, cellSize) | 0;
  89. const voxelY = THREE.MathUtils.euclideanModulo(y, cellSize) | 0;
  90. const voxelZ = THREE.MathUtils.euclideanModulo(z, cellSize) | 0;
  91. return voxelY * cellSliceSize +
  92. voxelZ * cellSize +
  93. voxelX;
  94. }
  95. computeCellId(x, y, z) {
  96. const {cellSize} = this;
  97. const cellX = Math.floor(x / cellSize);
  98. const cellY = Math.floor(y / cellSize);
  99. const cellZ = Math.floor(z / cellSize);
  100. return `${cellX},${cellY},${cellZ}`;
  101. }
  102. addCellForVoxel(x, y, z) {
  103. const cellId = this.computeCellId(x, y, z);
  104. let cell = this.cells[cellId];
  105. if (!cell) {
  106. const {cellSize} = this;
  107. cell = new Uint8Array(cellSize * cellSize * cellSize);
  108. this.cells[cellId] = cell;
  109. }
  110. return cell;
  111. }
  112. getCellForVoxel(x, y, z) {
  113. return this.cells[this.computeCellId(x, y, z)];
  114. }
  115. setVoxel(x, y, z, v, addCell = true) {
  116. let cell = this.getCellForVoxel(x, y, z);
  117. if (!cell) {
  118. if (!addCell) {
  119. return;
  120. }
  121. cell = this.addCellForVoxel(x, y, z);
  122. }
  123. const voxelOffset = this.computeVoxelOffset(x, y, z);
  124. cell[voxelOffset] = v;
  125. }
  126. getVoxel(x, y, z) {
  127. const cell = this.getCellForVoxel(x, y, z);
  128. if (!cell) {
  129. return 0;
  130. }
  131. const voxelOffset = this.computeVoxelOffset(x, y, z);
  132. return cell[voxelOffset];
  133. }
  134. generateGeometryDataForCell(cellX, cellY, cellZ) {
  135. const {cellSize, tileSize, tileTextureWidth, tileTextureHeight} = this;
  136. const positions = [];
  137. const normals = [];
  138. const uvs = [];
  139. const indices = [];
  140. const startX = cellX * cellSize;
  141. const startY = cellY * cellSize;
  142. const startZ = cellZ * cellSize;
  143. for (let y = 0; y < cellSize; ++y) {
  144. const voxelY = startY + y;
  145. for (let z = 0; z < cellSize; ++z) {
  146. const voxelZ = startZ + z;
  147. for (let x = 0; x < cellSize; ++x) {
  148. const voxelX = startX + x;
  149. const voxel = this.getVoxel(voxelX, voxelY, voxelZ);
  150. if (voxel) {
  151. // voxel 0 is sky (empty) so for UVs we start at 0
  152. const uvVoxel = voxel - 1;
  153. // There is a voxel here but do we need faces for it?
  154. for (const {dir, corners, uvRow} of VoxelWorld.faces) {
  155. const neighbor = this.getVoxel(
  156. voxelX + dir[0],
  157. voxelY + dir[1],
  158. voxelZ + dir[2]);
  159. if (!neighbor) {
  160. // this voxel has no neighbor in this direction so we need a face.
  161. const ndx = positions.length / 3;
  162. for (const {pos, uv} of corners) {
  163. positions.push(pos[0] + x, pos[1] + y, pos[2] + z);
  164. normals.push(...dir);
  165. uvs.push(
  166. (uvVoxel + uv[0]) * tileSize / tileTextureWidth,
  167. 1 - (uvRow + 1 - uv[1]) * tileSize / tileTextureHeight);
  168. }
  169. indices.push(
  170. ndx, ndx + 1, ndx + 2,
  171. ndx + 2, ndx + 1, ndx + 3,
  172. );
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. return {
  180. positions,
  181. normals,
  182. uvs,
  183. indices,
  184. };
  185. }
  186. // from
  187. // http://www.cse.chalmers.se/edu/year/2010/course/TDA361/grid.pdf
  188. intersectRay(start, end) {
  189. let dx = end.x - start.x;
  190. let dy = end.y - start.y;
  191. let dz = end.z - start.z;
  192. const lenSq = dx * dx + dy * dy + dz * dz;
  193. const len = Math.sqrt(lenSq);
  194. dx /= len;
  195. dy /= len;
  196. dz /= len;
  197. let t = 0.0;
  198. let ix = Math.floor(start.x);
  199. let iy = Math.floor(start.y);
  200. let iz = Math.floor(start.z);
  201. const stepX = (dx > 0) ? 1 : -1;
  202. const stepY = (dy > 0) ? 1 : -1;
  203. const stepZ = (dz > 0) ? 1 : -1;
  204. const txDelta = Math.abs(1 / dx);
  205. const tyDelta = Math.abs(1 / dy);
  206. const tzDelta = Math.abs(1 / dz);
  207. const xDist = (stepX > 0) ? (ix + 1 - start.x) : (start.x - ix);
  208. const yDist = (stepY > 0) ? (iy + 1 - start.y) : (start.y - iy);
  209. const zDist = (stepZ > 0) ? (iz + 1 - start.z) : (start.z - iz);
  210. // location of nearest voxel boundary, in units of t
  211. let txMax = (txDelta < Infinity) ? txDelta * xDist : Infinity;
  212. let tyMax = (tyDelta < Infinity) ? tyDelta * yDist : Infinity;
  213. let tzMax = (tzDelta < Infinity) ? tzDelta * zDist : Infinity;
  214. let steppedIndex = -1;
  215. // main loop along raycast vector
  216. while (t <= len) {
  217. const voxel = this.getVoxel(ix, iy, iz);
  218. if (voxel) {
  219. return {
  220. position: [
  221. start.x + t * dx,
  222. start.y + t * dy,
  223. start.z + t * dz,
  224. ],
  225. normal: [
  226. steppedIndex === 0 ? -stepX : 0,
  227. steppedIndex === 1 ? -stepY : 0,
  228. steppedIndex === 2 ? -stepZ : 0,
  229. ],
  230. voxel,
  231. };
  232. }
  233. // advance t to next nearest voxel boundary
  234. if (txMax < tyMax) {
  235. if (txMax < tzMax) {
  236. ix += stepX;
  237. t = txMax;
  238. txMax += txDelta;
  239. steppedIndex = 0;
  240. } else {
  241. iz += stepZ;
  242. t = tzMax;
  243. tzMax += tzDelta;
  244. steppedIndex = 2;
  245. }
  246. } else {
  247. if (tyMax < tzMax) {
  248. iy += stepY;
  249. t = tyMax;
  250. tyMax += tyDelta;
  251. steppedIndex = 1;
  252. } else {
  253. iz += stepZ;
  254. t = tzMax;
  255. tzMax += tzDelta;
  256. steppedIndex = 2;
  257. }
  258. }
  259. }
  260. return null;
  261. }
  262. }
  263. VoxelWorld.faces = [
  264. { // left
  265. uvRow: 0,
  266. dir: [ -1, 0, 0, ],
  267. corners: [
  268. { pos: [ 0, 1, 0 ], uv: [ 0, 1 ], },
  269. { pos: [ 0, 0, 0 ], uv: [ 0, 0 ], },
  270. { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  271. { pos: [ 0, 0, 1 ], uv: [ 1, 0 ], },
  272. ],
  273. },
  274. { // right
  275. uvRow: 0,
  276. dir: [ 1, 0, 0, ],
  277. corners: [
  278. { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  279. { pos: [ 1, 0, 1 ], uv: [ 0, 0 ], },
  280. { pos: [ 1, 1, 0 ], uv: [ 1, 1 ], },
  281. { pos: [ 1, 0, 0 ], uv: [ 1, 0 ], },
  282. ],
  283. },
  284. { // bottom
  285. uvRow: 1,
  286. dir: [ 0, -1, 0, ],
  287. corners: [
  288. { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  289. { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  290. { pos: [ 1, 0, 0 ], uv: [ 1, 1 ], },
  291. { pos: [ 0, 0, 0 ], uv: [ 0, 1 ], },
  292. ],
  293. },
  294. { // top
  295. uvRow: 2,
  296. dir: [ 0, 1, 0, ],
  297. corners: [
  298. { pos: [ 0, 1, 1 ], uv: [ 1, 1 ], },
  299. { pos: [ 1, 1, 1 ], uv: [ 0, 1 ], },
  300. { pos: [ 0, 1, 0 ], uv: [ 1, 0 ], },
  301. { pos: [ 1, 1, 0 ], uv: [ 0, 0 ], },
  302. ],
  303. },
  304. { // back
  305. uvRow: 0,
  306. dir: [ 0, 0, -1, ],
  307. corners: [
  308. { pos: [ 1, 0, 0 ], uv: [ 0, 0 ], },
  309. { pos: [ 0, 0, 0 ], uv: [ 1, 0 ], },
  310. { pos: [ 1, 1, 0 ], uv: [ 0, 1 ], },
  311. { pos: [ 0, 1, 0 ], uv: [ 1, 1 ], },
  312. ],
  313. },
  314. { // front
  315. uvRow: 0,
  316. dir: [ 0, 0, 1, ],
  317. corners: [
  318. { pos: [ 0, 0, 1 ], uv: [ 0, 0 ], },
  319. { pos: [ 1, 0, 1 ], uv: [ 1, 0 ], },
  320. { pos: [ 0, 1, 1 ], uv: [ 0, 1 ], },
  321. { pos: [ 1, 1, 1 ], uv: [ 1, 1 ], },
  322. ],
  323. },
  324. ];
  325. function main() {
  326. const canvas = document.querySelector('#c');
  327. const renderer = new THREE.WebGLRenderer({canvas});
  328. const cellSize = 32;
  329. const fov = 75;
  330. const aspect = 2; // the canvas default
  331. const near = 0.1;
  332. const far = 1000;
  333. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  334. camera.position.set(-cellSize * .3, cellSize * .8, -cellSize * .3);
  335. const controls = new OrbitControls(camera, canvas);
  336. controls.target.set(cellSize / 2, cellSize / 3, cellSize / 2);
  337. controls.update();
  338. const scene = new THREE.Scene();
  339. scene.background = new THREE.Color('lightblue');
  340. const tileSize = 16;
  341. const tileTextureWidth = 256;
  342. const tileTextureHeight = 64;
  343. const loader = new THREE.TextureLoader();
  344. const texture = loader.load('resources/images/minecraft/flourish-cc-by-nc-sa.png', render);
  345. texture.magFilter = THREE.NearestFilter;
  346. texture.minFilter = THREE.NearestFilter;
  347. function addLight(x, y, z) {
  348. const color = 0xFFFFFF;
  349. const intensity = 1;
  350. const light = new THREE.DirectionalLight(color, intensity);
  351. light.position.set(x, y, z);
  352. scene.add(light);
  353. }
  354. addLight(-1, 2, 4);
  355. addLight( 1, -1, -2);
  356. const world = new VoxelWorld({
  357. cellSize,
  358. tileSize,
  359. tileTextureWidth,
  360. tileTextureHeight,
  361. });
  362. const material = new THREE.MeshLambertMaterial({
  363. map: texture,
  364. side: THREE.DoubleSide,
  365. alphaTest: 0.1,
  366. transparent: true,
  367. });
  368. const cellIdToMesh = {};
  369. function updateCellGeometry(x, y, z) {
  370. const cellX = Math.floor(x / cellSize);
  371. const cellY = Math.floor(y / cellSize);
  372. const cellZ = Math.floor(z / cellSize);
  373. const cellId = world.computeCellId(x, y, z);
  374. let mesh = cellIdToMesh[cellId];
  375. const geometry = mesh ? mesh.geometry : new THREE.BufferGeometry();
  376. const {positions, normals, uvs, indices} = world.generateGeometryDataForCell(cellX, cellY, cellZ);
  377. const positionNumComponents = 3;
  378. geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  379. const normalNumComponents = 3;
  380. geometry.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  381. const uvNumComponents = 2;
  382. geometry.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  383. geometry.setIndex(indices);
  384. geometry.computeBoundingSphere();
  385. if (!mesh) {
  386. mesh = new THREE.Mesh(geometry, material);
  387. mesh.name = cellId;
  388. cellIdToMesh[cellId] = mesh;
  389. scene.add(mesh);
  390. mesh.position.set(cellX * cellSize, cellY * cellSize, cellZ * cellSize);
  391. }
  392. }
  393. const neighborOffsets = [
  394. [ 0, 0, 0], // self
  395. [-1, 0, 0], // left
  396. [ 1, 0, 0], // right
  397. [ 0, -1, 0], // down
  398. [ 0, 1, 0], // up
  399. [ 0, 0, -1], // back
  400. [ 0, 0, 1], // front
  401. ];
  402. function updateVoxelGeometry(x, y, z) {
  403. const updatedCellIds = {};
  404. for (const offset of neighborOffsets) {
  405. const ox = x + offset[0];
  406. const oy = y + offset[1];
  407. const oz = z + offset[2];
  408. const cellId = world.computeCellId(ox, oy, oz);
  409. if (!updatedCellIds[cellId]) {
  410. updatedCellIds[cellId] = true;
  411. updateCellGeometry(ox, oy, oz);
  412. }
  413. }
  414. }
  415. for (let y = 0; y < cellSize; ++y) {
  416. for (let z = 0; z < cellSize; ++z) {
  417. for (let x = 0; x < cellSize; ++x) {
  418. const height = (Math.sin(x / cellSize * Math.PI * 2) + Math.sin(z / cellSize * Math.PI * 3)) * (cellSize / 6) + (cellSize / 2);
  419. if (y < height) {
  420. world.setVoxel(x, y, z, randInt(1, 17));
  421. }
  422. }
  423. }
  424. }
  425. function randInt(min, max) {
  426. return Math.floor(Math.random() * (max - min) + min);
  427. }
  428. updateVoxelGeometry(1, 1, 1); // 0,0,0 will generate
  429. function resizeRendererToDisplaySize(renderer) {
  430. const canvas = renderer.domElement;
  431. const width = canvas.clientWidth;
  432. const height = canvas.clientHeight;
  433. const needResize = canvas.width !== width || canvas.height !== height;
  434. if (needResize) {
  435. renderer.setSize(width, height, false);
  436. }
  437. return needResize;
  438. }
  439. let renderRequested = false;
  440. function render() {
  441. renderRequested = undefined;
  442. if (resizeRendererToDisplaySize(renderer)) {
  443. const canvas = renderer.domElement;
  444. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  445. camera.updateProjectionMatrix();
  446. }
  447. controls.update();
  448. renderer.render(scene, camera);
  449. }
  450. render();
  451. function requestRenderIfNotRequested() {
  452. if (!renderRequested) {
  453. renderRequested = true;
  454. requestAnimationFrame(render);
  455. }
  456. }
  457. let currentVoxel = 0;
  458. let currentId;
  459. document.querySelectorAll('#ui .tiles input[type=radio][name=voxel]').forEach((elem) => {
  460. elem.addEventListener('click', allowUncheck);
  461. });
  462. function allowUncheck() {
  463. if (this.id === currentId) {
  464. this.checked = false;
  465. currentId = undefined;
  466. currentVoxel = 0;
  467. } else {
  468. currentId = this.id;
  469. currentVoxel = parseInt(this.value);
  470. }
  471. }
  472. function getCanvasRelativePosition(event) {
  473. const rect = canvas.getBoundingClientRect();
  474. return {
  475. x: (event.clientX - rect.left) * canvas.width / rect.width,
  476. y: (event.clientY - rect.top ) * canvas.height / rect.height,
  477. };
  478. }
  479. function placeVoxel(event) {
  480. const pos = getCanvasRelativePosition(event);
  481. const x = (pos.x / canvas.width ) * 2 - 1;
  482. const y = (pos.y / canvas.height) * -2 + 1; // note we flip Y
  483. const start = new THREE.Vector3();
  484. const end = new THREE.Vector3();
  485. start.setFromMatrixPosition(camera.matrixWorld);
  486. end.set(x, y, 1).unproject(camera);
  487. const intersection = world.intersectRay(start, end);
  488. if (intersection) {
  489. const voxelId = event.shiftKey ? 0 : currentVoxel;
  490. // the intersection point is on the face. That means
  491. // the math imprecision could put us on either side of the face.
  492. // so go half a normal into the voxel if removing (currentVoxel = 0)
  493. // our out of the voxel if adding (currentVoxel > 0)
  494. const pos = intersection.position.map((v, ndx) => {
  495. return v + intersection.normal[ndx] * (voxelId > 0 ? 0.5 : -0.5);
  496. });
  497. world.setVoxel(...pos, voxelId);
  498. updateVoxelGeometry(...pos);
  499. requestRenderIfNotRequested();
  500. }
  501. }
  502. const mouse = {
  503. x: 0,
  504. y: 0,
  505. };
  506. function recordStartPosition(event) {
  507. mouse.x = event.clientX;
  508. mouse.y = event.clientY;
  509. mouse.moveX = 0;
  510. mouse.moveY = 0;
  511. }
  512. function recordMovement(event) {
  513. mouse.moveX += Math.abs(mouse.x - event.clientX);
  514. mouse.moveY += Math.abs(mouse.y - event.clientY);
  515. }
  516. function placeVoxelIfNoMovement(event) {
  517. if (mouse.moveX < 5 && mouse.moveY < 5) {
  518. placeVoxel(event);
  519. }
  520. window.removeEventListener('mousemove', recordMovement);
  521. window.removeEventListener('mouseup', placeVoxelIfNoMovement);
  522. }
  523. canvas.addEventListener('mousedown', (event) => {
  524. event.preventDefault();
  525. recordStartPosition(event);
  526. window.addEventListener('mousemove', recordMovement);
  527. window.addEventListener('mouseup', placeVoxelIfNoMovement);
  528. }, {passive: false});
  529. canvas.addEventListener('touchstart', (event) => {
  530. event.preventDefault();
  531. recordStartPosition(event.touches[0]);
  532. }, {passive: false});
  533. canvas.addEventListener('touchmove', (event) => {
  534. event.preventDefault();
  535. recordMovement(event.touches[0]);
  536. }, {passive: false});
  537. canvas.addEventListener('touchend', () => {
  538. placeVoxelIfNoMovement({
  539. clientX: mouse.x,
  540. clientY: mouse.y,
  541. });
  542. });
  543. controls.addEventListener('change', requestRenderIfNotRequested);
  544. window.addEventListener('resize', requestRenderIfNotRequested);
  545. }
  546. main();
  547. </script>
  548. </html>