Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save janily/1b68bc39d33bdcf8984c6c172b3e0166 to your computer and use it in GitHub Desktop.
Save janily/1b68bc39d33bdcf8984c6c172b3e0166 to your computer and use it in GitHub Desktop.

Revisions

  1. @mauriciomassaia mauriciomassaia renamed this gist Sep 21, 2021. 1 changed file with 0 additions and 0 deletions.
  2. @mauriciomassaia mauriciomassaia created this gist Aug 31, 2021.
    92 changes: 92 additions & 0 deletions vanilla-boilerplate.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    <!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>Vanilla Boilerplate / threejs + gsap</title>
    <style>
    html,
    body {
    padding: 0;
    margin: 0;
    }
    </style>
    </head>
    <body>
    <script type="module">
    import * as THREE from 'https://cdn.skypack.dev/three'
    import gsap from 'https://cdn.skypack.dev/gsap'

    const WIDTH = window.innerWidth
    const HEIGHT = window.innerHeight
    let spinningX = false

    // Setup 3d Environment
    const scene = new THREE.Scene()
    const camera = new THREE.PerspectiveCamera(75, WIDTH / HEIGHT, 0.1, 1000)
    camera.position.z = 4

    const renderer = new THREE.WebGLRenderer()
    renderer.setSize(WIDTH, HEIGHT)
    document.body.appendChild(renderer.domElement)

    const ambientLight = new THREE.AmbientLight(0x0000ff, 0.5)
    scene.add(ambientLight)

    const light = new THREE.PointLight(0xffffff, 2, 20)
    light.position.z = 3
    light.position.y = 2
    scene.add(light)

    const geometry = new THREE.BoxGeometry(1, 1, 1)
    const material = new THREE.MeshLambertMaterial({ color: 0xff00ff });
    const cube = new THREE.Mesh(geometry, material)
    scene.add(cube)

    function animate () {
    requestAnimationFrame(animate)

    cube.rotation.y += 0.005

    renderer.render(scene, camera)
    }

    function onMouseUp (e) {
    if (spinningX) return

    spinningX = true

    gsap.to(cube.scale, {
    x: 1.5,
    y: 1.5,
    z: 1.5,
    duration: 0.4,
    ease: 'back.inOut',
    yoyo: true,
    repeat: 1
    })

    gsap.to(cube.rotation, {
    x: cube.rotation.x + Math.PI * 2,
    duration: 1,
    ease: 'quad.inOut',
    onComplete: () => {
    spinningX = false
    }
    })
    }

    function onResize () {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
    }

    window.addEventListener('mouseup', onMouseUp)
    window.addEventListener('resize', onResize, false)

    animate();
    </script>
    </body>
    </html>