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

              
                <body>
  
</body>
              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}

body {
  position: relative;
  background: #333;
}

canvas {
  position: absolute;
  transform: translate(-50%, 10%);
  top: 50%;
  left: 50%;
  width: 80%;
  height: 80vh;
  border-radius: 20px;
}
              
            
!

JS

              
                // function for rounding thousands
const rT = x => ~~(x*1000 + 0.5) / 1000;
// some easing functions
const EASING = {
  easeIn: t => t * t * t,
  easeOut: t => (--t) * t * t + 1,
  easeInOut: t => t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
  linear: t => t,
  easeInQuint: t => t * t * t * t * t,
  easeOutQuint: t => 1 + (--t) * t * t * t * t,
  easeInOutQuint: t => t < .5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t
}
// example object that is drawn to the canvas
class Circle {
  constructor(x,y,radius,color) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color;
  }
  
  update() {
    // nothing to do here
  }
  
  render(ctx) {
    ctx.fillStyle = this.color;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
    ctx.fill();
  }
}
// basic class to control the movement of some object
class ObjectMover {
  constructor(obj) {
    this.obj = obj;
    this.moves = []; //basic queue for the moves
    
    this.hasStarted;
    this.isRunning;
    this.startTime;
    this.animationTime;
    this.lastTime;
    this.timeInAnimation;
    this.direction = {};
    this.easing;
    this.start = {};
    this.target = {};
    this.lastMoveLength = {};
    this.delay;
  }
  
  update() {
    
    if(this.moves.length > 0 && !this.isRunning) {
      this.initMove();
    }
    if(this.isRunning) {
      this.updateMove();  
    }
    // no update then
  }
  
  updateMove() {
    let now = Date.now();
    let time, factor;
    if(this.delay > 0) {
      let offset = now - this.lastTime;
      this.delay -= offset;
      this.lastTime = now;
    } else {
      if (this.timeInAnimation === 0) {
        this.startTime = now + 10;
      }
      this.timeInAnimation = now - this.startTime;
      time = this.timeInAnimation / this.animationTime;
      time = time > 1 ? 1 : time;
      factor = this.easing(time); 
      let moveX = this.direction.x * factor * this.animationLength;
      let moveY = this.direction.y * factor * this.animationLength;
      // this.obj.x = rT(this.start.x + this.direction.x * factor * this.animationLength);
      // this.obj.y = rT(this.start.y + this.direction.y * factor * this.animationLength);
      this.obj.x += moveX - this.lastMoveLength.x;
      this.obj.y += moveY - this.lastMoveLength.y;
      this.lastMoveLength.x = moveX;
      this.lastMoveLength.y = moveY;
      
      if (this.timeInAnimation >= this.animationTime) {
        // move is finished
        this.resetMove();
      }
    }
  }
  
  resetMove() {
    this.isRunning = false;
  }
  
  render(ctx) {
    this.obj.render(ctx);
  }
  
  initMove() {
    let move = this.moves.shift();
    this.start.x = this.obj.x;
    this.start.y = this.obj.y;
    this.target.x = move.target.x;
    this.target.y = move.target.y;
    this.lastMoveLength.x = 0;
    this.lastMoveLength.y = 0;
    let directionX = rT(this.target.x - this.start.x),
        directionY = rT(this.target.y - this.start.y);
    this.animationLength = rT(Math.sqrt(
      Math.pow(directionX,2) + Math.pow(directionY,2)
    ));
    this.animationTime = move.time;
    // this.startTime = Date.now();
    this.easing = move.easing;
    this.delay = move.delay;
    this.direction.x = 1 / this.animationLength * directionX;
    this.direction.y = 1 / this.animationLength * directionY;
    this.isRunning = true;
    this.lastTime = Date.now();
    this.timeInAnimation = 0;
  }
  
  addMove(targetX,targetY,time,easing,delay) {
    delay = delay || 0;
    targetX = rT(targetX);
    targetY = rT(targetY);
    this.moves.push({
      target: {
        x: targetX,
        y: targetY
      },
      time: time,
      easing: easing,
      delay: delay
    })
  }
}


// basic canvas setup
let canvas = document.createElement('canvas');
document.querySelector('body').appendChild(canvas);
let ctx = canvas.getContext('2d');
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;

let circ;
let om;
let om2;
// resize listener for changing desired canvas resolution, after resize event happened
window.addEventListener('resize', e => {
  canvas.width = canvas.clientWidth;
  canvas.height = canvas.clientHeight;
  circ = undefined;
  om = undefined;
  om2 = undefined;
  loop();
});
/*
let addMoveLoop = om => {
  om.addMove(canvas.width * .9,canvas.height * .2,3000,EASING.easeInOutQuint,200);
  om.addMove(canvas.width * .9,canvas.height * .8,1000,EASING.easeInOutQuint,200);
  om.addMove(canvas.width * .1,canvas.height * .8,4000,EASING.easeInOutQuint,200);
  om.addMove(canvas.width * .1,canvas.height * .2,1000,EASING.easeInOutQuint,200);
  om.addMove(canvas.width * .5,canvas.height * .5,3000,EASING.easeInOutQuint,200);
} 
*/
let moveLoop = (om,om2) => {
  om.addMove(canvas.width * .9,canvas.height * .8,5000,EASING.easeInOut,200);
  om2.addMove(canvas.width * .1, canvas.height * .2, 5000,EASING.easeInQuint,200);
}
const loop = () => {
  if (!circ || !om || !om2) {
    console.log('rescale')
    circ = new Circle(canvas.width * .1, canvas.height * .8, canvas.width * .01, '#333333');
    om = new ObjectMover(circ);
    om2 = new ObjectMover(circ);
  } 
  ctx.fillStyle = '#e6e6e6';
  ctx.fillRect(0,0,canvas.width,canvas.height);
  om.update();
  om2.update();
  circ.render(ctx);
  // basic back and forth
  if (om.moves.length === 0 && !om.isRunning && om2.moves.length === 0 && !om2.isRunning) {
    moveLoop(om,om2);
  }
  requestAnimationFrame(loop);
}

loop();

              
            
!
999px

Console