Pen Settings

HTML

CSS

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

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

              
                <div id="container"></div>
              
            
!

CSS

              
                body {
				color: #ccc;
				font-family:Monospace;
				font-size:13px;
				text-align:center;
				background-color: black;
				margin: 0px;
				padding: 0px;
				overflow: hidden;
			}
#container {
  height: 100vh;
  width: 100vw;
}

body { cursor: none; }
              
            
!

JS

              
                var current_step_id = 0;
var FPS_RATE = 45;
var BALL_BOUND = 15;

var ballForce = {
  xspeed: 0,
  yspeed: 0,
  xforce: 0,
  yforce: 0,
  mass: 20
};

var points = [];

var mouse = {
  x: 0,
  y: 0
};

var speed = 5;

var is_falling = true;
var get_up = false;

var container,
  scene,
  camera,
  renderer,
  mouseLight,
  ball,
  light,
  vectorMouse = new THREE.Vector3(0, 0, 20),
  visualPointLight;

init();
animate();

function init() {
  // Bernoulli curve
  var j = 2 * Math.PI;
  var k = Math.PI / 1000;
  var scale = 70;
  var linegeometry = new THREE.Geometry();

  for (var t = 0; t < j; t += k) {
    //  heart
     var x =  (scale / 34) * 16 * (Math.sin(t)* Math.sin(t) * Math.sin(t))
     var y = -(-scale / 34 * (13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t)))
   vec = new THREE.Vector3(x, y , 0)
    points.push(vec);
    linegeometry.vertices.push(vec);
  }
  
  points.push(points[0]);
  linegeometry.vertices.push(points[0]);

  //create scene
  scene = new THREE.Scene();

  //set camera position
  camera = new THREE.PerspectiveCamera(
    20,
    window.innerWidth / window.innerHeight,
    10,
    200
  );
  camera.position.set(0, 0, 200);
  scene.add(camera);

  //create ambient light
  var ambientLight = new THREE.AmbientLight(0xffffff, 0.4, 2);
  scene.add(ambientLight);
  
  var linematerial = new THREE.LineBasicMaterial({
    color: 0x0000ff
  });

  var line = new THREE.Line( linegeometry, linematerial );
  scene.add( line );

  // create mouse light
  light = new THREE.PointLight(0xffff00, 2.2, 100, 2);
  light.position.set(0, 0, 20);
  scene.add(light);
  
  
  // create point light above ball to add 3D effect
  pointLight = new THREE.PointLight(0xffffff);
  pointLight.position.set(0, 0, 250);
  scene.add(pointLight);

  // create visual cursor
  var CursorMaterial = new THREE.MeshPhongMaterial({ color: 0xffff00 });
  visualPointLight = new THREE.Mesh(
    new THREE.CircleGeometry(1, 32),
    CursorMaterial
  );
  visualPointLight.position.set(0, 0, 40);
  scene.add(visualPointLight);

 
var material = new THREE.MeshPhongMaterial({
    color: 0x7e1aff,
    shininess: 30
  });

 
  // create ball alias "bouboule"
  ball = new THREE.Mesh(new THREE.SphereBufferGeometry(6, 64, 64), material);
  ball.position.set(points[0].x, 140, 0);
  ball.name = "bouboule";
  scene.add(ball);
 
 
  // generate renderer
  renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);

  //place scene in container
  container = document.getElementById("container");
  container.appendChild(renderer.domElement);

  // When the mouse moves, call the given function
  document.addEventListener("mousemove", onMouseMove, false);
  // When window resize, resize all
  window.addEventListener("resize", onWindowResize, false);
}

// Follows the mouse event
function onMouseMove(event) {
  event.preventDefault();
  mouse.x = event.clientX / window.innerWidth * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

  // Make the sphere follow the mouse
  var vector = new THREE.Vector3(mouse.x, mouse.y, 100);
  vector.unproject(camera);
  var dir = vector.sub(camera.position).normalize();
  var distance = -camera.position.z / dir.z;
  var pos = camera.position.clone().add(dir.multiplyScalar(distance));
  vectorMouse = pos;

  light.position.copy(pos);
  visualPointLight.position.copy(pos);
  light.translateZ(15);
  visualPointLight.translateZ(15);
}

function onWindowResize() {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
}
//
function animate() {
  //Frame per second handmer
  setTimeout(function() {
    requestAnimationFrame(animate);
  }, 1000 / FPS_RATE);

  render();
}

function render() {
  camera.lookAt(scene.position);
  scene.traverse(function(object) {
    if (object.name == "bouboule") {
      if (is_falling) {
        speed -= 0.088;
        if (speed <= 0.4 && !get_up) { speed = 0.4; }
        if (object.position.y <= -2 && !get_up) { get_up = true; }
        if (get_up) {
          speed = -0.14;
          if (object.position.y >= 0) { is_falling = false; }
        }
        object.position.y = object.position.y - speed;
      }
      else {
        applyMagnetForce();
        getSpringForce();
        applyFriction();
        applyAllForces();
        getFloatingForce();
      }
    }
  });
  renderer.autoClear = false;
  renderer.clear();
  renderer.render(scene, camera);
}

function applyMagnetForce() {
  var dx = (vectorMouse.x - ball.position.x) * 1.2;
  var dy = (vectorMouse.y - ball.position.y) * 1.2;
  var distance = Math.sqrt(dx * dx + dy * dy);
  var force = 20 / (distance);
  //Make sure the simulation doesn't explode
  if (force > 3) {
    force = 3;
  }
  //Find the horizontal and vertical components of the force
  var xforce = force * (dx / distance);
  var yforce = force * (dy / distance);
  //Apply forces to particles
  ballForce.xforce = -xforce;
  ballForce.yforce = -yforce;
}

function getSpringForce() {
  var dx = ball.position.x - points[current_step_id].x;
  var dy = ball.position.y - points[current_step_id].y;
  var distance = Math.sqrt(dx * dx + dy * dy);
  var force = 0.4 * (distance);
  //Find the horizontal and vertical components of the force
  if (distance == 0) return;
  var xforce = - force * (dx / distance);
  var yforce = - force * (dy / distance);
  //Apply forces to particles
  ballForce.xforce += xforce;
  ballForce.yforce += yforce;
}

function getFloatingForce() {
  var dx = ball.position.x - points[current_step_id].x;
  var dy = ball.position.y - points[current_step_id].y;
  var distance = Math.sqrt(dx * dx + dy * dy);
  var force = 10 / (distance * distance);
  //Make sure the simulation doesn't explode
  if (force > 3) {
    force = 3;
  }
  //Find the horizontal and vertical components of the force
  var xforce = force * (dx / distance);
  var yforce = force * (dy / distance);
  //Apply forces to particles
  ballForce.xforce = -xforce;
  ballForce.yforce = -yforce;
  current_step_id = (current_step_id == points.length - 1) ? 0 : current_step_id + 1;
}

function applyFriction() {
  var s = Math.sqrt(ballForce.xspeed * ballForce.xspeed + ballForce.yspeed * ballForce.yspeed);
  var force = 3 * speed;
  if (s == 0) return;
  //Find the horizontal and vertical components of the force
  var xforce = force * (ballForce.xspeed / s);
  var yforce = force * (ballForce.yspeed / s);
  //Apply forces to particles
  
  ballForce.xforce += xforce;
  ballForce.yforce += yforce;
}

function applyAllForces() {
  ballForce.xspeed += ballForce.xforce / ballForce.mass;
  ballForce.yspeed += ballForce.yforce / ballForce.mass;
  ball.position.x += ballForce.xspeed;
  ball.position.y += ballForce.yspeed;
}

              
            
!
999px

Console