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

Auto Save

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

              
                <a href='//twitter.com/ycwhk' target='_blank'>couple</a>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Nova+Mono&display=swap');

canvas {
    display: grid; width: 100%; height: 100vh;
    position: fixed; top: 0; left: 0;
    cursor: grab;
}

a {
    display: block;
    color: black;
    padding: 5vmin;
    width: min-content;
    font-family: 'nova mono', monospace;
    position: relative; z-index: 1;
}

a:hover:after {
    display: block;
    content: "#2374C6, #C20F00, #FFDD22, #FFFFFF, #000000."
}

body {
    display: block;
    height: 100vh;
}
              
            
!

JS

              
                import * as $ from '//unpkg.com/three@0.118.3/build/three.module.js'
import { OrbitControls } from '//unpkg.com/three@0.118.3/examples/jsm/controls/OrbitControls.js'
import { EffectComposer } from '//unpkg.com/three@0.118.3/examples/jsm/postprocessing/EffectComposer'
import { RenderPass } from '//unpkg.com/three@0.118.3/examples/jsm/postprocessing/RenderPass'
import { UnrealBloomPass } from '//unpkg.com/three@0.118.3/examples/jsm/postprocessing/UnrealBloomPass'

// ----
// Boot
// ----


const renderer = new $.WebGLRenderer({ antialias: true });
const scene = new $.Scene();
const camera = new $.PerspectiveCamera(75, 2, 0.1, 100);
const controls = new OrbitControls(camera, renderer.domElement);
const composer = new EffectComposer(renderer);
const res = new $.Vector2();
window.addEventListener('resize', () => {
    const { clientWidth, clientHeight } = renderer.domElement;
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(clientWidth, clientHeight, false);
    camera.aspect = clientWidth / clientHeight;
    camera.updateProjectionMatrix();
    composer.setPixelRatio(window.devicePixelRatio);
    composer.setSize(clientWidth, clientHeight);
    renderer.getDrawingBufferSize(res);
});
document.body.prepend(renderer.domElement);
window.dispatchEvent(new Event('resize'));
renderer.setAnimationLoop(t => {
    composer.render();
    controls.update();
});

// ----
// Main
// ---- 

const BLUEISH = '#2374C6';
const REDISH = '#C20F00';
const YELLOWISH = '#FFDD22';
const BLACK = '#000000';
const WHITE = '#FFFFFF';

scene.background = new $.Color(WHITE);
camera.position.set(5, 7, 5);
controls.autoRotate = true;
renderer.shadowMap.enabled = true;

const light = new $.DirectionalLight(WHITE, 1.2);
light.position.set(0, 5, 0);
light.castShadow = true;
light.shadow.camera.near = 1;
light.shadow.camera.far = 10;
light.shadow.camera.left = -10;
light.shadow.camera.right = 10;
light.shadow.camera.top = 10;
light.shadow.camera.bottom = -10;
light.shadow.bias = -0.01;
scene.add(light);

//// Generalize https://codepen.io/ycw/pen/ExKyJdR?editors=0010 `class Box`

class F {
    constructor({ mat, size }) {
        this.size = size;
        this.mat = mat;
        const { pivot, object } = this.buildMesh();
        this.object = object;
        this.pivot = pivot;
    }
    buildPivot(w, h, d, tx, ty, tz, px, py, pz) {
        const geom = new $.BoxBufferGeometry(w, h, d);
        geom.translate(tx, ty, tz);
        const pivot = new $.Mesh(geom, this.mat);
        pivot.position.set(px, py, pz);
        pivot.castShadow = true;
        pivot.receiveShadow = true;
        return pivot;
    }
    buildMesh() { // t-pose on xz heading to z-
        const SZ = this.size;
        const S = SZ / 2; // box half size
        const T = SZ / 50; // thickness
        const pivot = {};
        pivot.right = this.buildPivot(SZ * 2, T, SZ, SZ, 0, 0, S, 0, 0);
        pivot.left = this.buildPivot(SZ * 2, T, SZ, -SZ, 0, 0, -S, 0, 0);
        pivot.body = this.buildPivot(SZ, T, SZ, 0, 0, 0, 0, 0, 0);
        pivot.head = this.buildPivot(SZ, T, SZ, 0, 0, -S, 0, 0, -S);
        pivot.head.rotation.x = Math.PI / 20;
        pivot.uppertail = this.buildPivot(SZ, T, SZ, 0, 0, S, 0, 0, S);
        pivot.lowertail = this.buildPivot(SZ, T, SZ, 0, 0, S, 0, 0, SZ);
        const object = new $.Group();
        object.add(pivot.right);
        object.add(pivot.left);
        object.add(pivot.body);
        object.add(pivot.head);
        object.add(pivot.uppertail);
        pivot.uppertail.add(pivot.lowertail);
        return { pivot, object };
    }
    animate_wing(duration) {
        return new Promise((onComplete) => {
            const defaults = {
                ease: 'power3',
                duration: duration / 3
            };
            const k = Math.PI / 6;
            const n_flutter = 2;
            const t_down = duration * 0.2 / n_flutter;
            const t_up = duration * 0.2 / n_flutter;
            const t_soar = duration - t_down - t_up;
            const tl_flutter = gsap.timeline({ defaults, repeat: n_flutter })
                .addLabel('DOWN')
                .to(this.pivot.right.rotation, { z: -k, duration: t_down }, 'DOWN')
                .to(this.pivot.left.rotation, { z: k, duration: t_down }, 'DOWN')
                .addLabel('UP')
                .to(this.pivot.right.rotation, { z: k, duration: t_up }, 'UP')
                .to(this.pivot.left.rotation, { z: -k, duration: t_up }, 'UP');
            const tl_soar = gsap.timeline({ defaults, pasused: true })
                .addLabel('SOAR')
                .to(this.pivot.right.rotation, { z: 0, duration: t_soar }, 'SOAR')
                .to(this.pivot.left.rotation, { z: 0, duration: t_soar }, 'SOAR');
            gsap.timeline({ onComplete, repeat: -1 })
                .add(tl_flutter)
                .add(tl_soar);
        });
    }
    animate_tail(duration) {
        return new Promise((onComplete) => {
            const defaults = {
                ease: 'power3.out',
                duration: duration / 4
            };
            const k = Math.PI / 100;
            gsap.timeline({ onComplete, defaults, repeat: -1 })
                .addLabel('UP')
                .to(this.pivot.uppertail.rotation, { x: -k }, 'UP')
                .to(this.pivot.lowertail.rotation, { x: k * 2 }, 'UP')
                .addLabel('DOWN')
                .to(this.pivot.uppertail.rotation, { x: k }, 'DOWN')
                .to(this.pivot.lowertail.rotation, { x: -k * 2 }, 'DOWN')
        });
    }
    animate(duration) {
        return Promise.all([
            this.animate_wing(duration),
            this.animate_tail(duration)
        ]);
    }
}

//// Make F

{
    const size = 1;
    const colors = [REDISH, YELLOWISH];

    for (let i = 0, I = 2; i < I; ++i) {
        const color = colors[i % colors.length];
        const mat = new $.MeshLambertMaterial({ color });
        const f = new F({ mat, size });
        f.animate(2);
        const fly_pivot = new $.Group();
        fly_pivot.position.x = (i * size * 4);
        fly_pivot.position.z = (i * size * 4);
        fly_pivot.position.y = 5 + size;
        fly_pivot.add(f.object);
        scene.add(fly_pivot);
        f.object.position.y = size - fly_pivot.position.y;
        const duration = i / I * 2 + 1;
        const ease = 'power3';
        gsap.timeline({ repeat: -1, defaults: { ease, duration } })
            .set(fly_pivot.rotation, { z: Math.PI / -5 })
            .to(fly_pivot.rotation, { z: Math.PI / 5 })
            .to(fly_pivot.rotation, { z: Math.PI / -5 });
    }
}

//// Ground

{
    const geom = new $.BoxBufferGeometry(1000, 0.01, 1000, 100, 1, 100);
    const mat = new $.MeshLambertMaterial({ color: BLUEISH })
    const mesh = new $.Mesh(geom, mat);
    mesh.receiveShadow = true;
    scene.add(mesh);
}

//// Animate - repeating background

{
    const bg = new $.Group();
    for (let i = 0, I = 64; i < I; ++i) {
        const h = Math.random();
        const r = 1 + Math.random() * 4;
        const geom = new $.ConeBufferGeometry(r, h);
        const mat = new $.MeshLambertMaterial({ color: BLUEISH });
        const mesh = new $.Mesh(geom, mat);
        mesh.receiveShadow = true;
        bg.add(mesh);
        reset(mesh);
    }
    scene.add(bg);
    function reset(mesh) {
        const z = -50 - Math.random() * 100;
        mesh.position.z = z;
        mesh.position.x = Math.random() * 100 - 50;
        const duration = Math.abs(z) / 20;
        const ease = 'linear';
        const onComplete = () => reset(mesh);
        gsap.to(mesh.position, { z: Math.abs(z), duration, onComplete, ease });
    }
}

//// PostProcessing

const renderPass = new RenderPass(scene, camera);
const bloomPass = new UnrealBloomPass(res, .4, 1, 0.8);
composer.addPass(renderPass);
composer.addPass(bloomPass);

              
            
!
999px

Console