cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmloctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <canvas id="c"></canvas>
<p>
  Current Angle: <span id="angle"></span><br />
  &nbsp;Target Angle: <span id="target-angle"></span><br />
</p>
<p>I'm trying to have the joint rotate smoothly around the center of the canvas, toward the angle of mouse pointer. What I have works, but I want it to animate the shortest distance possible, to get to the mouse angle. The problem occurs when the value loops around at the horizontal line (3.14 and -3.14). Mouseover that area to see how the direction switches and it takes the long way back around.</p>
<p>Relevant lines of code are 20 and 48. Any ideas on how to get this working?</p>
            
          
!
            
              body {
  background: #ddd;
  font: 100%/1.5 monospace;
}

canvas {
  background: #fff;
  display: block;
}

p {
  max-width: 40em;
  padding-left: 1.5em;
}
            
          
!
            
              var c = document.getElementById( 'c' ),
    ctx = c.getContext( '2d' ),
    cw = c.width = 300,
    ch = c.height = 200,
    mx = 0,
    my = 0,
    joint = {
      x: cw / 2,
      y: ch / 2,
      length: 50,
      angle: 0,
      targetAngle: 0,
      easing: 0.05
    },
    angleElem = document.getElementById( 'angle' );
    targetAngleElem = document.getElementById( 'target-angle' );

function step() {
  requestAnimationFrame( step );
  ctx.clearRect( 0, 0, cw, ch );
  
  // ease the current angle to the target angle
  /*joint.angle += ( joint.targetAngle - joint.angle ) * joint.easing;*/
  
  // get normalised direction from joint to mouse
  var dx = mx - joint.x,
      dy = my - joint.y,
      len = Math.sqrt(dx * dx + dy * dy);
      dx /= len ? len : 1.0; dy /= len ? len : 1.0;
  // get current direction
  var dirx = Math.cos(joint.angle),
      diry = Math.sin(joint.angle);
  // ease the current direction to the target direction
  dirx += (dx - dirx) * 0.1;
  diry += (dy - diry) * 0.1;

  joint.angle = Math.atan2(diry, dirx);
  joint.targetAngle = Math.atan2(dy, dx);
  
  ctx.save();
  ctx.beginPath();
  ctx.translate( joint.x, joint.y + 0.5 );
  ctx.rotate( joint.angle );
  ctx.moveTo( 0, 0 );
  ctx.lineTo( joint.length, 0 );
  ctx.lineWidth = 5;
  ctx.stroke();
  ctx.restore();
  
  ctx.beginPath();
  ctx.arc( joint.x, joint.y, 10, 0, Math.PI * 2 );
  ctx.fill();
  
  ctx.beginPath();
  ctx.moveTo( 0, joint.y + 0.5 );
  ctx.lineTo( joint.x, joint.y + 0.5 );
  ctx.lineWidth = 1;
  ctx.stroke();
  
  angleElem.innerHTML = joint.angle.toFixed( 3 );
  targetAngleElem.innerHTML = joint.targetAngle.toFixed( 3 );
}

window.addEventListener( 'mousemove', function( e ) {
  mx = e.clientX,
  my = e.clientY;  
}, false );

step();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console