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

              
                <svg class="svgdef" id="boxsvgref" version="1.1"xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" x="0px" y="0px" width="100px" height="100px" viewBox="0 0 100 100" style="enable-background:new 0 0 100 100;" xml:space="preserve">
<polyline class="keyline" points="0,0 100,0 100,100 0,100 0,0.7 " stroke="white" stroke-width="1" stroke-linecap="round" vector-effect="non-scaling-stroke" fill="none"/>
</svg>

<!-- <div class="border border--withscore"><img class="content" src="//lorempixel.com/300/300/abstract/1/" alt=""></div>

<div class="border"><img class="content" src="//lorempixel.com/200/150/abstract/2/" alt=""></div>

<div class="border"><img class="content" src="//lorempixel.com/600/250/abstract/5/" alt=""></div> -->


<div class="border border--withscore"><img class="content" src="//placebear.com/350/400" alt=""></div>

<div class="border"><img class="content" src="//placebear.com/200/150" alt=""></div>

<div class="border"><img class="content" src="//placebear.com/600/250" alt=""></div>

<div class="border"><img class="content" src="//placebear.com/250/250" alt=""></div>

<div class="border"><img class="content" src="//placebear.com/200/400" alt=""></div>

              
            
!

CSS

              
                body {
  background: #001122;
}

.svgdef {
  display: none;
}

.border {
  display: inline-block;
  font-size: 0;
  margin: 4px;
  position: relative;
  
  padding: 15px;
  
  .content {
    opacity: 0;
    transition: 0.3s opacity;
  }
  
  svg {
    display: block;
    // height: 100%;
    left: 0px;
    position: absolute;
    top: 0px;
    // width: 100%;
    z-index: 2;
    
    .keyline {
    }
    
    &.score {
      filter: drop-shadow(0px 0px 5px #55FF44) blur(1px);
    }
  }
}
              
            
!

JS

              
                let elements = document.querySelectorAll('.border');
let svg = document.getElementById('boxsvgref');

class borderLoader {
  constructor(element) {
    this.element = element;
    
    this.content = element.querySelector('.content');
    
    let img = element.querySelector('img');
    
    if(img) {
      img.addEventListener('load', ()=> { this.init(); this.animating = true; } );
    } else {
      this.init();
    }
    
    window.addEventListener('click', ()=> { this.animating = true });
    
    this.element.style.opacity = 0;
  }
  
  init() {
    this.element.style.opacity = 1;
    
    console.log(this.element.className);
    
    let newsvg = svg.cloneNode(true);
    newsvg.id="";
    let w = this.element.offsetWidth;
    let h = this.element.offsetHeight;
    
    newsvg.setAttribute('width', w +'px');
    newsvg.setAttribute('height', h +'px');
    newsvg.setAttribute('viewBox', `0 0 ${w} ${h}`);

    this.line = newsvg.querySelector('.keyline');
    this.line.setAttribute('points', '7.5,7.5 '+(w - 7.5)+', 7.5 '+(w - 7.5)+', '+(h - 7.5) +' 7.5, '+(h - 7.5)+' 7.5, 7.5');
  
    this.element.appendChild(newsvg);
    
    console.log(this.element.className.indexOf('border--withscore'))
    
    if(this.element.className.indexOf('border--withscore') > -1) {
      this.hasScore = true;
      let scoresvg = newsvg.cloneNode(true);
      
      this.scoreline = scoresvg.querySelector('.keyline');
      
      scoresvg.className.baseVal = 'score';
    
      window.scoreline = this.scoreline;
      this.element.appendChild(scoresvg);
    }
    
    this.reset();
  }
  
  reset() {
    // this.length = this.line.getTotalLength();
    
    this.line.style.strokeDasharray = this.length + ' ' + this.length;
    this.line.style.strokeDashoffset = this.currentLength;
    
    this.content.style.opacity = 0;
    
    this.currentLength = this.length;
    
    if(this.scoreline) {
      this.scoreline.setAttribute('stroke-width', 4);
      this.scoreline.style.strokeDasharray = '20 ' + (this.currentLength - 10);
      this.scoreline.style.strokeDashoffset = 0;
    }
    
  }
  
  animate() {
    if(this.hasScore) {
      this.scoreline.style.strokeDashoffset -= 2;
      if(this.animatingScore) {
      } else {
        if(this.currentLength <= 0) {
          this.currentLength = 0;
          this.content.style.opacity = 1;
          this.animatingScore = true;
        } else {
          this.currentLength -= this.speed;
        }
      }
      requestAnimationFrame(this.animate.bind(this));
    } else {
      this.currentLength -= this.speed;

      if(this.currentLength <= 0) {
        this.animating = false;
        this.currentLength = 0;
        this.content.style.opacity = 1;
      } else {
        requestAnimationFrame(this.animate.bind(this));
      }
    }
  }
  
  set currentLength(value) {
    if(!isNaN(value)) {
      this._currentLength = value;
      this.line.style.strokeDashoffset = value;
    }
  }
  get currentLength() {
    return this._currentLength;
  }
  
  set speed(value) {
    if(value > 0) this._speed = value;
  }
  get speed() {
    return this._speed || 25;
  }
  
  set animating(value) {
    let isAnimating = this.animating;
    this._animating = value === true;
    
    if(this._animating && !isAnimating) {
      this.reset();
      requestAnimationFrame(this.animate.bind(this));
    }
  }
  get animating() {
    return this._animating === true;
  }
  
  get length() {
    let totalLength = 0;
    let prevPos;
    for (var i = 0 ; i < this.line.points.numberOfItems; i++) {
      var pos = this.line.points.getItem(i);
      if (i > 0) {
        totalLength += Math.sqrt(Math.pow((pos.x - prevPos.x), 2) + Math.pow((pos.y - prevPos.y), 2));
      }
      prevPos = pos;
    }
    return totalLength;
  }
}

elements.forEach((element)=> {
  let a = new borderLoader(element);
});
              
            
!
999px

Console