cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <h1>Digital Ocean</h1>
<canvas id="cvs"></canvas>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Lobster');

body {
  padding: 0;
  margin: 0;
}

h1 {
  position: fixed;
  text-align: center;
  z-index: 1;
  top: 70vh;
  width: 100%;
  font-family: 'Lobster', cursive;
  font-size: 4rem;
}

canvas {
  margin-bottom: 15vh;
  width: 100%;
  height: 100%;
  pointer-events: none;
  touch-callout: none;
}
            
          
!
            
              /** 
    Author: Damian Drygiel
    WWW:    http://drygiel.com
*/

function Waves(cvs, options) {

    var self = this,
        paused = true,
        rafid;

    self.isOK = false;

    self.defaults = {
        accuracy: 1,
        speed: 0.16,
        offset: 246,
        height: 30,
        color: 0xFFFFFF,
        bgColor: 0xFFFFFF,
        size: 1000,
        segments: 40,
        camera: {
            perspective: -70,
            position: { x: 450, y: 40, z: 0 },
            lookAt: { x: 0, y: 140, z: 50 }
        },
        fog: {
            enabled: true,
            density: 0.005,
        },
        renderCallback: function (s) { }
    };

    self.isPaused = function () {
        return paused;
    };
    self.dots = [];

    function extend(destination, source) {
        for (var property in source) {
            if (typeof source[property] === 'object')
                destination[property] = extend(destination[property], source[property]);
            else
                destination[property] = source[property];
        }
        return destination;
    }

    function addEvent(elem, type, eventHandle) {
        if (elem == null || elem == undefined) return;
        if (elem.addEventListener) {
            elem.addEventListener(type, eventHandle, false);
        } else if (elem.attachEvent) {
            elem.attachEvent("on" + type, eventHandle);
        } else {
            elem["on" + type] = eventHandle;
        }
    };

    function getDevicePixelRatio() {
        var ratio = 1;
        // To account for zoom, change to use deviceXDPI instead of systemXDPI
        if (window.screen.systemXDPI !== undefined && window.screen.logicalXDPI !== undefined && window.screen.systemXDPI > window.screen.logicalXDPI) {
            // Only allow for values > 1
            ratio = window.screen.systemXDPI / window.screen.logicalXDPI;
        }
        else if (window.devicePixelRatio !== undefined) {
            ratio = window.devicePixelRatio;
        }
        return ratio;
    }

    function OnResizeCVS() {
        var pixelRatio = 1 / getDevicePixelRatio();
        self.camera.aspect = window.innerWidth / window.innerHeight;
        self.camera.updateProjectionMatrix();
        self.renderer.setSize(window.innerWidth * pixelRatio * self.options.accuracy, window.innerHeight * pixelRatio * self.options.accuracy, false);
    }

    function init() {
        self.options = extend(self.defaults, options);

        if (!!!window.WebGLRenderingContext) return false;

        try {
            self.renderer = new THREE.WebGLRenderer({ canvas: cvs, antialias: true });
            self.renderer.setClearColor(self.options.bgColor);
            // SCENE
            self.scene = new THREE.Scene();

            if (self.options.fog.enabled)
                self.scene.fog = new THREE.FogExp2(self.options.bgColor, self.options.fog.density);

            // CAMERA
            self.camera = new THREE.PerspectiveCamera(self.options.camera.perspective, window.innerWidth / window.innerHeight, 1, 10000);
            self.camera.position.set(self.options.camera.position.x, self.options.camera.position.y, self.options.camera.position.z);
            self.lookAt = new THREE.Vector3(self.options.camera.lookAt.x, self.options.camera.lookAt.y, self.options.camera.lookAt.z);
            self.camera.lookAt(self.lookAt);
            self.scene.add(self.camera);

            // ON WINDOW RESIZE
            addEvent(window, 'resize', OnResizeCVS);
            OnResizeCVS();

            // PLANE
            var material = new THREE.MeshBasicMaterial({
                color: self.options.color,
                wireframe: true,
            });
            var geometry = new THREE.PlaneGeometry(self.options.size, self.options.size * 2, self.options.segments, self.options.segments * 2);

            self.mesh = new THREE.Mesh(geometry, material);
            self.mesh.rotation.x = Math.PI / 2;
            self.scene.add(self.mesh);

        } catch (e) {
            return false;
        }
        return true;
    }
    self.isOK = init();

    function animate() {
        rafid = requestAnimationFrame(animate);
        waves(Date.now() / 1000);
        self.options.renderCallback(self);
        self.renderer.render(self.scene, self.camera);
    }

    function waves(time) {

        var verts = self.mesh.geometry.vertices;

        for (var i = 0, len = verts.length; i < len; i++) {
            verts[i].z = Math.sin(verts[i].x * Math.PI / self.options.offset + time * self.options.speed)
                        * Math.cos(verts[i].y * Math.PI / self.options.offset + time * self.options.speed)
                        * self.options.height;
        }

        self.mesh.geometry.verticesNeedUpdate = true;
    }

    self.toggle = function (run) {
        if (!self.isOK) return false;

        if (run === undefined)
            self.toggle(!paused);

        else if (!!run && paused) {
            paused = false;
            animate();
        }
        else if (!!!run) {
            paused = true;
            cancelAnimationFrame(rafid);
        }
        return true;
    }
}

(function() {
  var cvs = document.getElementById('cvs');
  var mouseX = 0;
  
  document.onmousemove = function (e) {
    mouseX = e.clientX;
  };

  var waves = new Waves(cvs, {
      color: 0xBBBBBB,
      renderCallback: function (v) {
          var targetX = mouseX / window.innerWidth * 200 - 100;
          v.lookAt.z += (-targetX - v.lookAt.z) / 100;
          v.camera.lookAt(v.lookAt);
      }
  });
  
  waves.toggle(true);
})();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console