Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                #app
              
            
!

CSS

              
                body, html
  height: 100%
  width: 100%
  margin: 0
  padding: 0

body
  background: rebeccapurple
  display: flex
  justify-content: center
  align-items: center
    
.container
  height: 240px
  width: 240px
  background: white
  display: flex
  flex-direction: column
  
.Animation-anchor
  position: relative
  height: 30px
  width: 30px
  margin: auto
  
  
.Animation-element
  position: absolute
  background: grey
  border-radius: 50%
  height: 30px
  width: 30px
  will-change: opacity, transform
  
  
button
  outline: none
  border: 1px solid black
  background: #333
  color: white
  font-weight: 600
  letter-spacing: 1px
  &:hover
    color: rebeccapurple
    background: goldenrod
  &:focus
    box-shadow: 0 0 3px black
    
  
              
            
!

JS

              
                class AnimationExample extends React.Component {
  constructor() {
    super();
    this.play = this.play.bind(this);
  }
  
  play() {
    if (!this.animationComponent) return;
    
    this.animationComponent.play();
  }
  
  render() {
    return (
      <div className="container">
        <AnimationComponent
          ref={(r)=> {if(r) this.animationComponent = r}}
        />
        <button type='button'
          onClick={this.play}
          >Play</button>
      </div>
    );
  }
}

class AnimationComponent extends React.Component {
  constructor() {
    super();
    this.elementRefs = [];
    this.player = null;
  }
  
  play() {
    if (!this.player) return;
    
    if (this.player.playState === 'running') {
      this.player.finish();
    }
    
    this.player.play();
  }
  
  _generateExpandEffect(el, index, array) {
    const endX = Math.round( 80 * Math.cos(index * 2 * Math.PI/array.length));
    const endY = Math.round( 80 * Math.sin(index * 2 * Math.PI/array.length));
    const keyframes = [
      {transform: 'translate(0,0)'}, 
      {transform: `translate(${endX}px, ${endY}px)`}
    ];

    const timing = {
      duration: 250, 
      fill: 'forwards', 
    };
    
    return new KeyframeEffect(el, keyframes, timing);
  }
  
  _generateCollapseEffect(el, index, array) {
    const endX = Math.round( 80 * Math.cos(index * 2 * Math.PI/array.length));
    const endY = Math.round( 80 * Math.sin(index * 2 * Math.PI/array.length));
    const keyframes = [ 
      {transform: `translate(${endX}px, ${endY}px)`},
      {transform: 'translate(0,0)'}
    ];

    const timing = {
      duration: 250, 
      fill: 'forwards', 
    };
    
    return new KeyframeEffect(el, keyframes, timing);
  }
  
  _generateFadeInEffect(el, index) {
    const keyframes = [
      {opacity: 0},
      {opacity: 1}
    ];
    
    const timing = {
      duration: 50,
      fill: 'forwards'
    };
    
    return new KeyframeEffect(el, keyframes, timing);
  }
  
  _generateFadeOutEffect(el, index) {
    const keyframes = [
      {opacity: 1},
      {opacity: 0}
    ];
    
    const timing = {
      duration: 50,
      fill: 'forwards'
    };
    
    return new KeyframeEffect(el, keyframes, timing);
  }
  
  _generateAnimatedElements(quantity) {
    quantity = (typeof quantity === 'number' && quantity >= 0) ? +quantity : 0;
    const animatedElements = [];
    for (let i = 0; i < quantity; i++) {
      animatedElements.push(
        <div className='Animation-element'
          ref={(r) => this.elementRefs.push(r)}>
        </div>
      );
    }
    return animatedElements;
  }
  
  componentDidMount() {
    const expandEffects = this.elementRefs.map(this._generateExpandEffect);
    const collapseEffects = this.elementRefs.map(this._generateCollapseEffect);
    const fadeInEffects = this.elementRefs.map(this._generateFadeInEffect);
    const fadeOutEffects = this.elementRefs.map(this._generateFadeOutEffect);
    
    // any of these can be changed to Sequence or Group effects
    const expandGroup = new GroupEffect(expandEffects);
    const collapseGroup = new GroupEffect(collapseEffects);
    const fadeOutGroup = new SequenceEffect(fadeOutEffects);
    const fadeInGroup = new SequenceEffect(fadeInEffects);
    const animationSequence = new SequenceEffect([
      expandGroup, 
      fadeOutGroup, 
      fadeInGroup,
      collapseGroup
    ]);

    this.player = new Animation(animationSequence, document.timeline);
  }
  
  render() {
    const animatedElements = this._generateAnimatedElements(11);
    return (
      <div className="Animation-anchor">
        { animatedElements }
      </div>
    );
  }
}

ReactDOM.render(<AnimationExample />, document.getElementById('app'));
              
            
!
999px

Console