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

              
                <canvas id="view"></canvas>
              
            
!

CSS

              
                #view {
  position: fixed;
}
              
            
!

JS

              
                console.clear();

const app = new PIXI.Application({
  width: innerWidth,
  height: innerHeight,
  view: document.querySelector("#view"),
  antialias: true,
  resizeTo: window
});

const rope = new PIXI.Graphics();
const sprite = PIXI.Sprite.from("https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/dog.png?v=1");
sprite.anchor.set(0.5);
sprite.scale.set(0.5)

const p1 = new PIXI.Point(100, 200);
const p2 = sprite.position.copyFrom(p1);
const p3 = new PIXI.Point(1000, 200);

const points = [p1, p2, p3];

app.stage.addChild(rope, sprite);

const duration = 2;

const tl = gsap.timeline({
    delay: 1,
    repeat: -1,
    repeatDelay: 1,
    yoyo: true,
    onUpdate: update
  })
  .to(sprite, {
    duration,
    ease: "sine.inOut",
    x: p3.x
  }, 0)
  .to(sprite, {
    duration: duration / 2,
    y: "+=80",
    ease: "sine.inOut",
    repeat: 1,
    yoyo: true
  }, 0);

update();

function update() {
  
  rope.clear().lineStyle(4, 0xffffff);
  
  drawSpline(rope, points);
}

function drawSpline(graphics, points, closed = false, alpha = 0.5) {
      
  const size = points.length - (closed ? 0 : 1);
  
  graphics.moveTo(points[0].x, points[0].y);
  
  for (let i = 0; i < size; i++) {
    
    let p0, p1, p2, p3;
    
    if (closed) {
      p0 = points[(i - 1 + size) % size];
      p1 = points[i];
      p2 = points[(i + 1) % size];
      p3 = points[(i + 2) % size];
      
    } else {
      p0 = i == 0 ? points[0] : points[i - 1];
      p1 = points[i];
      p2 = points[i + 1];
      p3 = i == size - 1 ? p2 : points[i + 2];
    }
        
    let d1 = Math.sqrt(Math.pow(p0.x - p1.x, 2) + Math.pow(p0.y - p1.y, 2));
    let d2 = Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
    let d3 = Math.sqrt(Math.pow(p2.x - p3.x, 2) + Math.pow(p2.y - p3.y, 2));
    
    let d3powA  = Math.pow(d3, alpha);
    let d3pow2A = Math.pow(d3, 2 * alpha);
    
    let d2powA  = Math.pow(d2, alpha);
    let d2pow2A = Math.pow(d2, 2 * alpha);
    
    let d1powA  = Math.pow(d1, alpha);
    let d1pow2A = Math.pow(d1, 2 * alpha);
    
    let A = 2 * d1pow2A + 3 * d1powA * d2powA + d2pow2A;
    let B = 2 * d3pow2A + 3 * d3powA * d2powA + d2pow2A;
    let N = 3 * d1powA * (d1powA + d2powA);
    
    if (N > 0) N = 1 / N;        
    let M = 3 * d3powA * (d3powA + d2powA);    
    if (M > 0) M = 1 / M;
        
    let x1 = (-d2pow2A * p0.x + A * p1.x + d1pow2A * p2.x) * N;
    let y1 = (-d2pow2A * p0.y + A * p1.y + d1pow2A * p2.y) * N;

    let x2 = (d3pow2A * p1.x + B * p2.x - d2pow2A * p3.x) * M;
    let y2 = (d3pow2A * p1.y + B * p2.y - d2pow2A * p3.y) * M;
        
    if (x1 == 0 && y1 == 0) {
      x1 = p1.x;
      y1 = p1.y;
    }
    
    if (x2 == 0 && y2 == 0) {
      x2 = p2.x;
      y2 = p2.y;
    }
    
    graphics.bezierCurveTo(x1, y1, x2, y2, p2.x, p2.y);
  }
  
  if (closed) {
    graphics.closePath();
  }
}

              
            
!
999px

Console