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></canvas>

<div class="form-wrapper">
  <form onsubmit="return false;">
    <hgroup>
      <h1>Focusss</h1>
      <h3>Form focus without outlines by <a href="https://twitter.com/hakimel" tabindex="-1">@hakimel</a>.</h3>
    </hgroup>

    <label for="name">Name</label>
    <input type="text" id="name">
    
    <label for="email">Email</label>
    <input type="email" id="email">
    
    <label for="password">Password</label>
    <input type="password" id="password">
    
    <div class="checkbox">
      <input type="checkbox" id="check1">
      <label for="check1">I accept the terms</label><br>
    </div>
    
    <div class="checkbox">
      <input type="checkbox" id="check2">
      <label for="check2">Pineapple on pizza is a crime</label><br>
    </div>
    
    <button>Submit</button>
  </form>
</div>
              
            
!

CSS

              
                * { box-sizing: border-box; }

:root {
  --focus-head-color: hsl(218deg 92% 57%);
  --focus-tail-color: hsl(218deg 92% 50%);
}

html, body {
  font-size: 20px;
  height: 100%;
  margin: 0;
  background-color: #fff;
  font-family: Roboto, sans-serif;
}

.form-wrapper {
  position: relative;
  display: grid;
  height: 100%;
  margin: 0;
  place-items: center;
  max-width: 100%;
  padding: 2.5rem;
}

form {
  margin: auto;
  width: 100%;
  max-width: 400px;
}

h1 {
  font-size: 2rem;
  font-weight: 700;
  margin: 0 0 0.5rem 0;
}
h3 {
  font-size: 1rem;
  font-weight: 400;
  margin: 0 0 2rem 0;
}
a {
  color: var(--focus-head-color);
  text-decoration: none;
}

label {
  color: #444;
}

input[type="text"],
input[type="email"],
input[type="password"] {
  display: block;
  width: 100%;
  margin: 0.5rem 0 1rem 0;
  padding: 0.5rem;
  font-size: 1rem;
  border: 0;
  border-radius: 0.125rem 0.25rem;
  background-color: hsl(222, 20%, 95%);
}

.checkbox {
  margin: 0.5rem 0;
}

input[type="checkbox"] {
  margin-right: 0.5rem;
}

button {
  display: block;
  padding: 0.5rem 1rem;
  border: 0;
  border-radius: 0.125rem 0.25rem;
  background-color: var(--focus-head-color);
  color: #fff;
  margin: 2rem 0 0 auto;
}

input:focus,
button:focus{
  outline: 0;
}

canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}
              
            
!

JS

              
                const SPACING = 16;
const RADIUS = 8;
const CHECK_RADIUS = RADIUS * 1.8;
const CHECK_SIZE = 10;
const CHECK_LENGTH = Math.ceil( 10 * 1.8 );
const TAIL_LENGTH = 8;

const rootStyles = getComputedStyle( document.documentElement );
const HEAD_COLOR = rootStyles.getPropertyValue( '--focus-head-color' );
const TAIL_COLOR = rootStyles.getPropertyValue( '--focus-tail-color' );

const head = { r: RADIUS, tr: RADIUS };   
const tail = [];
const checkmarks = new Map();

const canvas = document.querySelector( 'canvas' );
const context = canvas.getContext( '2d' );

let width, height, dpr, currentFocus;

document.body.addEventListener( 'focus', event => focus( event.target ), true );
document.body.addEventListener( 'input', event => validate( event.target ), true );
document.querySelectorAll( 'input:not([type="checkbox"])' ).forEach( input => {
  checkmarks.set( input, { v: 0, tv: 0 } );
});

function resize() {

  dpr = window.devicePixelRatio || 1;

	width = window.innerWidth;
	height = window.innerHeight;

	canvas.width = width * dpr;
	canvas.height = height * dpr;

	context.scale( dpr, dpr );

}

function animate() {

  paint();

  requestAnimationFrame( animate );

}

function paint() {

  context.clearRect( 0, 0, width, height );

  if( currentFocus ) {

    // Add to the tail
    tail.push( { ...head } );
    if( tail.length > TAIL_LENGTH ) tail.shift();

    // Paint the tail
    if( tail.length > 3 ) {

      context.beginPath();
      context.moveTo( tail[0].x, tail[0].y );

      for( var i = 2; i < tail.length - 2; i++ ) {
        const p1 = tail[i];
        const p2 = tail[i+1];

        context.quadraticCurveTo(
          p1.x, p1.y,
          ( p1.x + p2.x ) / 2,
          ( p1.y + p2.y ) / 2
        );
      }

      context.quadraticCurveTo(
        tail[i].x, tail[i].y,
        tail[i+1].x, tail[i+1].y
      );

      context.lineWidth = RADIUS;
      context.lineCap = 'round';
      context.strokeStyle = TAIL_COLOR;
      context.stroke();
    }

    head.tr = currentFocus.classList.contains( 'valid' ) ? CHECK_RADIUS : RADIUS;
  
    // Animate the head towards its target values
    head.x += ( head.tx - head.x ) * 0.2;
    head.y += ( head.ty - head.y ) * 0.2;
    head.r += ( head.tr - head.r ) * 0.2;

    head.vx *= 0.8;
    head.x += head.vx;

    context.beginPath();
    context.arc( head.x, head.y, head.r, 0, Math.PI*2 );
    context.fillStyle = HEAD_COLOR;
    context.fill();

  }
  
  // Paint checkmarks
  for( [inputElement, checkmark] of checkmarks ) {
    checkmark.v += ( checkmark.tv - checkmark.v ) * 0.2;
    
    if( checkmark.v > 0.05 ) {
      const midX = inputElement.offsetLeft - CHECK_SIZE/2 - SPACING - 3;
      const midY = inputElement.offsetTop + inputElement.offsetHeight/2 + 1;

      context.save();
      context.beginPath();
      
      context.moveTo( midX + CHECK_SIZE/2, midY - CHECK_SIZE/2 );
      context.lineTo( midX - 1, midY + CHECK_SIZE/2 - 1 );
      context.lineTo( midX - CHECK_SIZE/2, midY ); 

      context.lineWidth = 3;
      context.lineCap = 'round';
      context.lineJoin = 'round'; 
      context.setLineDash([CHECK_LENGTH, CHECK_LENGTH]);
      context.lineDashOffset = CHECK_LENGTH + Math.round( checkmark.v * CHECK_LENGTH );
  
      context.globalCompositeOperation = 'lighter';
      context.strokeStyle = '#555';
      context.stroke();

      context.globalCompositeOperation = 'overlay';
      context.strokeStyle = '#fff';
      context.stroke(); 
      
      context.restore();
      
    }
  }

}

function focus( element ) {

  const previousFocus = currentFocus;

  if( element ) currentFocus = element;

  if( !currentFocus || !currentFocus.matches( 'input, button' ) ) return;

  head.tx = currentFocus.offsetLeft - SPACING - RADIUS;
  head.ty = currentFocus.offsetTop + currentFocus.offsetHeight/2;
  
  // Skip animation on first focus
  if( isNaN( head.x ) ) {
    head.x = head.tx;
    head.y = head.ty;
  }
  
  // Bounce leftward when focus changes
  if( currentFocus !== previousFocus ) {
    head.vx = -8 - Math.abs( head.tx - head.x ) / 5;
  }

}

function validate( element ) {
  
  let valid = false;
  
  switch( element.getAttribute( 'type' ) ) {
    case 'email': valid = /(.+)@(.+){2,}\.(.+){2,}/.test( element.value ); break;
    case 'password': valid = element.value.length > 6; break;
    default: valid = element.value.length > 0; break;
  }

  element.classList.toggle( 'valid', valid );
  
  checkmarks.get( element ).tv = valid ? 1 : 0;   
  
}

resize();
animate();

window.addEventListener( 'resize', () => {
  requestAnimationFrame( () => {
    resize();
    focus();
    paint();
  } );
} );

window.addEventListener( 'scroll', () => {
  requestAnimationFrame( () => {
    focus();
    paint();
  } );
} );

// document.querySelectorAll( 'input' )[0].focus();
              
            
!
999px

Console