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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div id="app-entry"></div>
              
            
!

CSS

              
                .wrapper {
  width: 100%;
  height: 430px;
}

.explode-button {
  cursor: pointer;
  font-size: 20px;
  padding: 10px;
  background: grey;
  height: 22px;
  width: 65px;
  position: absolute;
}

.explosion-wrapper {
    width: 100%;
    height: 100%;
}

.emitter {
    position: absolute;
/*     visibility: hidden; */
    background-color: black;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translateX(-10px) translateY(-10px);
}

.sprite-container {
    position: absolute;
    left: 0;
    top: 0;
    overflow: visible;
    z-index: 5000;
    pointer-events: none;
}

.sprite {
    position: absolute;
    pointer-events: none;
}

              
            
!

JS

              
                class Sprite extends React.Component {
  constructor(props) {
    super(props)
  }

  componentDidMount() {
    TweenMax.set(this.refs.sprite, {
      backgroundColor: this.props.color,
      x: Math.cos(this.props.angle) * this.props.length,
      y: Math.sin(this.props.angle) * this.props.length,
      width: this.props.dimensions,
      height: this.props.dimensions,
      xPercent: -50,
      yPercent: -50,
      visibility: 'hidden',
      force3D: true
    })

    this.buildTimeline()
  }
  
  getRandom(min, max) {
    return min + Math.random() * (max - min);
  }
  
  buildTimeline() {
    const halfDuration = this.props.duration / 2
    const rotationX = this.getRandom(720, 1440)
    const rotationZ = this.getRandom(720, 1440)

    this.props.timeline.to(this.refs.sprite, halfDuration, {
      opacity: 0,
      ease: RoughEase.ease.config({
        points: 20,
        strength: 1.75,
        clamp: true
      })
    }, 0).to(this.refs.sprite, this.props.duration, {
      visibility: 'visible',
      rotationX: `-=${rotationX}`,
      rotationZ: `+=${rotationZ}`,
      physics2D: {
        angle: this.props.angle * 180 / Math.PI, // translate radians to degrees
        velocity: 500, // initial velocity
        gravity: 900
      }
    }, 0).to(this.refs.sprite, 1.25 + Math.random(), {
      opacity: 0
    }, halfDuration)
  }

  render() {
    return (
      <div ref="sprite" className="sprite"></div>
    )
  }
}

Sprite.propTypes = {
  dimensions: React.PropTypes.number,
  color: React.PropTypes.string,
  angle: React.PropTypes.number,
  length: React.PropTypes.number,
  duration: React.PropTypes.number,
}

Sprite.defaultProps = {
  dimensions: 5,
  color: '',
  angle: 0,
  length: 0,
  duration: 0
}

class SpriteContainer extends React.Component {
  constructor(props) {
    super(props)
    
    this.timeline = new TimelineMax({
      paused: true
    })
  }
  
  getRandom(min, max) {
    return min + Math.random() * (max - min);
  }

  getDimensions() {
    return this.getRandom(this.props.spriteSizes.min, this.props.spriteSizes.max)
  }

  getColor() {
    const r = this.getRandom(30, 255)
    const g = this.getRandom(30, 230)
    const b = this.getRandom(30, 230)

    return `rgb(${r}, ${g}, ${b})`
  }
  
  setPosition(bounds) {
    TweenLite.set(this.refs.container, {
      x: bounds.left + bounds.width / 2,
      y: bounds.top + bounds.height / 2
    })
  }
  
  playAnimation() {
    this.timeline.restart()
  }

  render() {    
    let sprites = []
    let dimensions
    let angle
    let length
    let duration
    let color
    let i
    
    for (i = 0; i < this.props.spriteCount; i++) {
      dimensions = this.getDimensions()
      angle = this.getRandom(0.65, 0.85) * Math.PI * 2
      length = Math.random() * (this.props.emitterSize / 2 - dimensions / 2)
      duration = 3 + Math.random()
      color = this.getColor()
      sprites.push(<Sprite key={`sprite${i}`} dimensions={dimensions} angle={angle} length={length} color={color} duration={duration} timeline={this.timeline} />)
    }
    
    return (
      <div ref="container" className="sprite-container">
        {sprites.map(sprite => sprite)}
      </div>
    )
  }
}

SpriteContainer.propTypes = {
  spriteCount: React.PropTypes.number,
  emitterSize: React.PropTypes.number,
  spriteSizes: React.PropTypes.object
}

SpriteContainer.defaultProps = {
  spriteCount: 40,
  emitterSize: 20,
  spriteSizes: {
    min: 6,
    max: 8
  }
}

class Explosion extends React.Component {
  constructor(props) {
    super(props)

    this.spriteContainers = this.createSprites()
  }
  
  createSprites() {
    const containers = []
    let container
    let i

    for (i = 0; i < this.props.explosionCount; i++) {
      container = <SpriteContainer key={`explosion${i}`} ref={`explosion${i}`} spriteCount={this.props.spriteCount} spriteSizes={this.props.spriteSizes} emitterSize={this.props.size} />
      containers.push(container)
    }

    return containers
  }
  
  explode(index) {
    // get the bounds of the emitter
    const bounds = this.refs.emitter.getBoundingClientRect()
    
    // set the position of the container to the center of the emitter
    this.refs[`explosion${index}`].setPosition(bounds)
    
    // play the timeline
    this.refs[`explosion${index}`].playAnimation()
  }

  render() {
    return (
      <div className="explosion-wrapper">
        <div ref="emitter" className="emitter"></div>
        {this.spriteContainers.map(container => container)}
      </div>
    )
  }
}

Explosion.propTypes = {
  size: React.PropTypes.number,
  explosionCount: React.PropTypes.number,
  spriteCount: React.PropTypes.number,
  spriteSizes: React.PropTypes.object,
  speed: React.PropTypes.number,
  gravity: React.PropTypes.number
}

Explosion.defaultProps = {
  size: 20,
  explosionCount: 5,
  spriteCount: 40,
  spriteSizes: {
    min: 6,
    max: 8
  },
  speed: 2,
  gravity: 1
}

class Wrapper extends React.Component {
  constructor(props) {
    super(props)
    
    // bind the click event
    this.testExplosion = this.testExplosion.bind(this)
  }

  testExplosion() {
    // explode ONE group of sprites in a sprite container
    this.refs.explosion.explode(0)
  }

  render() {
    return (
      <div className="wrapper">
        <div className="explode-button" onClick={this.testExplosion}>Explode</div>
        <Explosion ref="explosion" size={20} explosionCount={5} spriteCount={40} spriteSizes={{min: 6, max: 8}} speed={2.4} gravity={0.7} />
      </div>
    )
  }
}

ReactDOM.render((
  <Wrapper />
), document.querySelector('#app-entry'))

              
            
!
999px

Console