Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
  <head>
    <title>Basic Three.js App</title>
  </head>
  <body>   
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/109/three.min.js"></script>
    <script src="script.js"></script>
  </body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                //Utils
export function windowDimensions() {
  const height = window.innerHeight
  const width = window.innerWidth
  const aspect = width / height;
  return { height, width, aspect}
}

export function onResize(fn) {
  window.addEventListener('resize', fn)
}

//Camera
function cameraDimensions(aspect, viewSize = 1000) {
  const caps = viewSize / 2
  const sides = aspect*viewSize / 2
  return { caps, sides }
}

export function getCamera() {
  const { aspect } = windowDimensions()
  const { caps, sides } = cameraDimensions(aspect)

  //const camera = new THREE.PerspectiveCamera( 75, aspect, 0.1, 1000 )
  const camera = new THREE.OrthographicCamera(
    -sides, sides,
    caps, -caps,
    -1000, 1000
  )

  window.addEventListener('resize', () => {
    updateCamera(camera)
  })
  
  camera.position.z = 500;
  return camera
}

function updateCamera(camera) {
  const { aspect } = windowDimensions()
  const { caps, sides } = cameraDimensions(aspect)
  camera.left = -sides,
  camera.right = sides,
  camera.top = caps,
  camera.bottom = -caps,
  camera.updateProjectionMatrix();
}


//WebGl renderer
function useWebGl(root = document.body) {
  const renderer = new THREE.WebGLRenderer({ alpha: true })
  renderer.setClearColor( 0x009900, 1 )
  renderWindow(renderer)
  root.appendChild( renderer.domElement )
  onResize(() => renderWindow(renderer))
  return { WebGl: renderer }
}

function renderWindow(renderer) {
  const { height, width } = windowDimensions()
  renderer.setSize( width / 2, height / 2 )
}


//ThreeJS Engine
function initEngine() {
  const scene = new THREE.Scene()
  const camera = getCamera()
  
  const { WebGl } = useWebGl()
  function everyFrame(callback = () => {}) {
    callback()
    WebGl.render( scene, camera )
    requestAnimationFrame(() => everyFrame(callback))
  }

  return {scene, everyFrame}
}


//Geometry
export function usePlane(scene) {
  const material = new THREE.MeshBasicMaterial({ color: 0x00ff22, wireframe: true })
  const geometry = new THREE.PlaneGeometry(300, 300)
  const plane = new THREE.Mesh( geometry, material )
  scene.add( plane );
  return {plane}
}


//JS Composition and Execution
const { scene, everyFrame } = initEngine()
const { plane } = usePlane(scene)
everyFrame(() => {})
              
            
!
999px

Console