css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <p>E = <span id="energy">0</span> kg&middot;px<sup>2</sup>/s<sup>2</sup> <input type="button" value="restart" onclick="start()"></p>
<canvas id="canvas" width="400" height="300">Canvas non supportato! Aggiorna il tuo browser</canvas>
            
          
!
            
              body {
  width:400px;
  margin-left:auto;
  margin-right:auto;
  background-color:#ddf;
  font-family:sans-serif;
}
p {
  text-align:center;
}
canvas {
  box-shadow: 8px 8px 16px;
  border-style: solid;
  border-width: 4px;
  border-color: #f00;
}

            
          
!
            
              var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

function clean() {
  ctx.fillStyle="#ff8";
  ctx.fillRect(0,0,canvas.width,canvas.height);
}

var Vector={};
Vector.aperture=Math.PI/12;
Vector.len=10;
Vector.color="#f0f";
Vector.draw=function(x,y,vx,vy){
  ctx.strokeStyle=Vector.color;
  ctx.lineWidth=1;
  ctx.beginPath();
  ctx.moveTo(x,y);
  ctx.lineTo(x+vx,y+vy);
  ctx.stroke();
  ctx.closePath();
  ctx.fillStyle=Vector.color;
  ctx.beginPath();
  var x0=x+vx;
  var y0=y+vy;
  ctx.moveTo(x0,y0);
  var angle=Math.atan2(vy,vx);
  var apt=Vector.aperture;
  var len=Vector.len;
  var x1=x0-len*Math.cos(angle+apt);
  var y1=y0-len*Math.sin(angle+apt);
  ctx.lineTo(x1,y1);
  var x2=x0-len*Math.cos(angle-apt);
  var y2=y0-len*Math.sin(angle-apt);
  ctx.lineTo(x2,y2);
  ctx.fill();
  ctx.closePath();
}

var Spring={};
Spring.color="#080";
Spring.segs=20;
Spring.draw=function(x0,y0,x1,y1){
  ctx.strokeStyle=Spring.color;
  ctx.lineWidth=1;
  ctx.beginPath();
  ctx.moveTo(x0,y0);
  var dx=x1-x0;
  var dy=y1-y0;
  var len=Math.sqrt(dx*dx+dy*dy);
  if(len>0) {
    var invLen=4/len;
    var maxCt=Spring.segs;
    for(var ct=1; ct<maxCt; ct++) {
      ctx.lineTo(x0+dx/maxCt*ct+invLen*dy,y0+dy/maxCt*ct-invLen*dx);
      invLen=-invLen;
    }
    ctx.lineTo(x1,y1);
  }
  ctx.stroke();
  ctx.closePath();
}

function Point(x,y,r,m){
  this.x=x;
  this.y=y;
  this.radius=r;
  this.vx=0;
  this.vy=0;
  this.mass=m;
}
var showVelocity=true;
Point.prototype.draw=function(){
  Spring.draw(Spring.x,Spring.y,this.x,this.y);
  if(showVelocity)
    Vector.draw(this.x,this.y,this.vx,this.vy);
  ctx.fillStyle="#008";
  ctx.beginPath();
  ctx.arc(Spring.x,Spring.y,3,0,2*Math.PI);
  ctx.fill();
  ctx.closePath();
  ctx.fillStyle="#fff";
  ctx.strokeStyle="#00f";
  ctx.lineWidth=2;
  ctx.beginPath();
  ctx.arc(this.x,this.y,this.radius,0,2*Math.PI);
  ctx.fill();
  ctx.stroke();
  ctx.closePath();
}
var g=20;
Spring.x=200;
Spring.y=100;
Spring.k=0.5;
Spring.rest=canvas.height/2-Spring.y;
Point.prototype.update=function(dt) {
  Spring.dx=this.x-Spring.x;
  Spring.dy=this.y-Spring.y;
  var length=Math.sqrt(Spring.dx*Spring.dx+Spring.dy*Spring.dy);
  Spring.len=length-Spring.rest;
  var kk=Spring.k*Spring.len/length/this.mass;
  var ax=-kk*Spring.dx;
  var ay=-kk*Spring.dy+g;
  var dt2=dt*dt;
  this.x+=this.vx*dt+0.5*ax*dt2;
  this.y+=this.vy*dt+0.5*ay*dt2;
  this.vx+=ax*dt;
  this.vy+=ay*dt;
}
Point.prototype.check=function() {
  if(this.x<this.radius || this.x>=canvas.width-this.radius) this.vx=-this.vx;
  if(this.y<this.radius || this.y>=canvas.height-this.radius) this.vy=-this.vy;
}
Point.prototype.getEnergy=function() {
  var Ue = 0.5*Spring.k*Spring.len*Spring.len;
  var Ug = this.mass*g*(canvas.height-this.y);
  var K = 0.5*this.mass*(this.vx*this.vx+this.vy*this.vy);
  return Ue+Ug+K;
}

var dt=1/60;
var timer;
var energyText=document.getElementById("energy");
function update() {
  clean();
  var steps=50;
  for(var ct=0; ct<steps; ct++)
    point.update(dt/steps);
  point.check();
  point.draw();
  energyText.innerHTML=Math.round(point.getEnergy());
  timer=window.setTimeout(update,dt*1000 /10);
}

var point;
function start() {
  point=new Point(canvas.width/2,canvas.height/2,5,1);
  point.vx=Math.random()*100-50;
  point.vy=Math.random()*100-50;
  if(timer) window.clearTimeout(timer);
  update();
}
start();
            
          
!
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