Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

              
                
              
            
!

CSS

              
                body {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    background: #F5F9FC;
}
              
            
!

JS

              
                const { timeline, to } = gsap

const width = 80
const height = 80

let renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: true
})

renderer.setSize(width, height)
renderer.setPixelRatio(2)
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.PCFSoftShadowMap

document.querySelector('body').appendChild(renderer.domElement)

const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(50, width / height, 0.1, 1000)

camera.position.z = 300

const rectangle = (ctx, x, y, width, height, radius) => {
    ctx.moveTo(x, y + radius)
    ctx.lineTo(x, y + height - radius)
    ctx.quadraticCurveTo(x, y + height, x + radius, y + height)
    ctx.lineTo(x + width - radius, y + height)
    ctx.quadraticCurveTo(x + width, y + height, x + width, y + height - radius)
    ctx.lineTo(x + width, y + radius)
    ctx.quadraticCurveTo(x + width, y, x + width - radius, y)
    ctx.lineTo(x + radius, y)
    ctx.quadraticCurveTo(x, y, x, y + radius)
}

const rectangleReverse = (ctx, x, y, width, height, radius) => {
    ctx.moveTo(x, y + height - radius)
    ctx.lineTo(x, y + radius)
    ctx.quadraticCurveTo(x, y, x + radius, y)
    ctx.lineTo(x + width - radius, y)
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius)
    ctx.lineTo(x + width, y + height - radius)
    ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height)
    ctx.lineTo(x + radius, y + height)
    ctx.quadraticCurveTo(x, y + height, x, y + height - radius)
}

let rect = new THREE.Shape()
rectangle(rect, -72, -72, 144, 144, 66)
var rectSmall = new THREE.Path()
rectangleReverse(rectSmall, -60, -60, 120, 120, 52)
rect.holes.push(rectSmall)

let shape = new THREE.ExtrudeBufferGeometry(rect, {
    curveSegments: 24,
    depth: 8,
    bevelEnabled: true,
    bevelSegments: 10,
    steps: 10,
    bevelSize: 4,
    bevelThickness: 4
})

const geometry = new THREE.Geometry()
geometry.fromBufferGeometry(shape)
geometry.mergeVertices()
geometry.center()

let material = new THREE.MeshPhongMaterial({
    color: 0x008FFD,
    shininess: 25
})
let donut = new THREE.Mesh(geometry, material)

donut.castShadow = true
donut.receiveShadow = true

scene.add(donut)

let planeGeometry = new THREE.PlaneGeometry(200, 200),
    planeMaterial = new THREE.ShadowMaterial({ opacity: .04 })

const plane = new THREE.Mesh(planeGeometry, planeMaterial)
plane.position.z = -80
plane.receiveShadow = true
scene.add(plane)

const lightFront = new THREE.DirectionalLight(0xFFFFFF, .5)
lightFront.position.set(0, 50, 300)
lightFront.castShadow = true

const d = 200
lightFront.shadow.camera.left = -d
lightFront.shadow.camera.right = d
lightFront.shadow.camera.top = d
lightFront.shadow.camera.bottom = -d

lightFront.shadow.mapSize.width = 512
lightFront.shadow.mapSize.height = 512

const lightTop = new THREE.DirectionalLight(0xFFFFFF, .5)
lightTop.position.set(0, 400, 40)

scene.add(lightFront)
scene.add(lightTop)

scene.add(new THREE.AmbientLight(0x0184fa))

// function twist(geometry, amount, axis) {
//     const quaternion = new THREE.Quaternion()
//     for(let i = 0; i < geometry.vertices.length; i++) {
//         quaternion.setFromAxisAngle(new THREE.Vector3(axis == 'x' ? 1 : 0, axis == 'y' ? 1 : 0, 0), geometry.vertices[i][axis] * amount)
//         geometry.vertices[i].applyQuaternion(quaternion)
//     }
//     geometry.verticesNeedUpdate = true
// }

to(donut.rotation, {
    repeat: -1,
    keyframes: [{
        x: THREE.Math.degToRad(180),
        duration: 1,
        ease: 'power1.inOut'
    }, {
        y: THREE.Math.degToRad(-180),
        duration: 1,
        ease: 'power1.inOut'
    }]
})

// geometry.lastDegreeX = 0
// geometry.lastDegreeY = 0
// geometry.degreeX = 0
// geometry.degreeY = 0

// to(geometry, {
//     duration: 2,
//     repeat: -1,
//     ease: 'none',
//     keyframes: [{
//         degreeX: .005
//     }, {
//         degreeX: 0
//     }, {
//         degreeY: .005
//     }, {
//         degreeY: 0
//     }],
//     onUpdate() {
//         let x = gsap.getProperty(this.targets()[0], 'degreeX'),
//             y = gsap.getProperty(this.targets()[0], 'degreeY')

//         twist(geometry, x - geometry.lastDegreeX, 'x')
//         twist(geometry, y - geometry.lastDegreeY, 'y')

//         geometry.lastDegreeX = x
//         geometry.lastDegreeY = y
//     }
// })

const render = () => {
    renderer.render(scene, camera)
    
    requestAnimationFrame(render)
}

render()

              
            
!
999px

Console