:: StoryTitle Chapbook: Importing External JS :: UserScript[script] // The following code is used from MDN for // dynamically importing scripts // https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement#Dynamically_importing_scripts window.setup = {}; setup.loadError = function(oError) { throw new URIError("The script " + oError.target.src + " didn't load correctly."); }; setup.loadScript = function(url, onloadFunction) { var newScript = document.createElement("script"); newScript.onerror = setup.loadError; if (onloadFunction) { newScript.onload = onloadFunction; } document.head.appendChild(newScript); newScript.async = true; newScript.src = url; }; :: Start
[JavaScript] setup.loadScript("https://ajax.googleapis.com/ajax/libs/threejs/r84/three.min.js", function() { var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 75, 1, 0.1, 1000 ); var renderer = new THREE.WebGLRenderer(); renderer.setSize( 250, 250 ); document.getElementById("drawArea").appendChild( renderer.domElement ); var geometry = new THREE.BoxGeometry( 1, 1, 1 ); var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } ); var cube = new THREE.Mesh( geometry, material ); scene.add( cube ); camera.position.z = 5; var animate = function () { requestAnimationFrame( animate ); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render( scene, camera ); }; animate(); }); [continued]