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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!--
// from
// http://www.gamedev.net/topic/444154-closest-point-on-a-line/
 
function getClosestPoint(A:*, B:*, P:*, segmentClamp:Boolean=true):Point {
    var AP:Point = new Point(P.x - A.x, P.y - A.y),
        AB:Point = new Point(B.x - A.x, B.y - A.y);
    var ab2:Number=AB.x*AB.x+AB.y*AB.y;
    var ap_ab:Number=AP.x*AB.x+AP.y*AB.y;
    var t:Number=ap_ab/ab2;
    if (segmentClamp) {
        if (t<0.0) {
            t=0.0;
        } else if (t> 1.0) {
            t=1.0;
        }
 
    }
    return new Point(A.x + AB.x * t, A.y + AB.y * t);
}
 
 
var point:Sprite = Sprite(addChild(new Sprite()));
point.x = 50;
point.y = 50;
with(point.graphics) beginFill(0), drawCircle(0,0, 3);
 
var line:MovieClip = MovieClip(addChild(new MovieClip()));
line.a = new Point(20, 100);
line.b = new Point(300, 60);
with(line.graphics) lineStyle(0), moveTo(line.a.x, line.a.y), lineTo(line.b.x, line.b.y);
 
var closestPoint:Point = getClosestPoint(line.a, line.b, point);
 
var closest:Sprite = Sprite(addChild(new Sprite()));
closest.x = closestPoint.x;
closest.y = closestPoint.y;
with(closest.graphics) beginFill(0xFF0000), drawCircle(0,0, 3);
-->
              
            
!

CSS

              
                * {
  margin: 0; padding: 0;
}
              
            
!

JS

              
                let canvas = document.createElement('canvas'), 
    c = canvas.getContext('2d'), 
    SIZE = 400;

canvas.width = SIZE;
canvas.height = SIZE;
document.body.appendChild(canvas);

function clear() {
  c.fillStyle = 'gray';
  c.fillRect(0, 0, canvas.width, canvas.height);
}

// http://www.gamedev.net/topic/444154-closest-point-on-a-line/
function getClosestPoint(a, b, p, segmentClamp = true) {
    let ap = { x: p.x - a.x, y: p.y - a.y },
        ab = { x: b.x - a.x, y: b.y - a.y },
        ab2 = ab.x * ab.x + ab.y * ab.y,
        apab = ap.x * ab.x + ap.y * ab.y,
        t = apab / ab2;
  
    if (segmentClamp) {
        if (t < 0.0) {
          t = 0.0;
        } else if (t > 1.0) {
            t = 1.0;
        }
 
    }
    return { x: a.x + ab.x * t, y: a.y + ab.y * t};
}

clear();

// point
let pnt = { x: 50, y: 50 };
c.fillStyle = 'red';
c.beginPath(); c.arc(pnt.x, pnt.y, 4, 0, Math.PI * 2, true);
c.fill();

// line
let a = { x: 20, y: 100 };
let b = { x: 300, y: 60 };
c.beginPath();
c.moveTo(a.x, a.y);
c.lineTo(b.x, b.y);
c.stroke();

// closest point
let closestPoint = getClosestPoint(a, b, pnt);
c.fillStyle = 'black';
c.beginPath(); c.arc(
  closestPoint.x, closestPoint.y, 4, 0, Math.PI * 2, true
);
c.fill();
 


              
            
!
999px

Console