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.
Vanilla Boilerplate / threejs + gsap
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment