css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              $main-bg: #4e4e4e;

body {
  background: $main-bg;
}

.container {
  position: absolute;
  top: 45%;
  left: 50%;
  margin: 0 auto;
  text-align: center;
}

/**
 * Default Vertical Progress
 */
.progress-vertical {
  position: relative;
  background: #e9ecef;
  width: 10px;
  height: 100px;
  border-radius: 0.5rem;
}

.progress-bar-vertical {
  position: absolute;
  width: 100%;
  border-radius: 0;
  background: #007bff;
  transform: rotate(-180deg);
  bottom: 0;
  height: 20px; // value
}

/**
 * Meter
 */
.meter-wrapper {
  position: relative;
  display: inline-block;
}

.meter {
  background: #262626;
  height: 100%;
  width: 20px;
  border-radius: 0;
  margin-right: 2px;
  display: inline-block;

  &__rms {
    background: linear-gradient(to top ,#0BF662, #69f662);
    z-index: 5;
    height: 50%; // value
  }

  &__peak {
    background: #a4d057;
    height: 2px;
    left: 0;
    bottom: 80%; // value
    
  }

  &__peak-bar {
    background: #548b43;
    height: 70%; // value
  }

  &__threshold {
    background: $main-bg;
    height: 2px;
    bottom: 93.75%; // value
    z-index: 100;
    width: 100%; // 可調整
  }
}

// 警告樣式
.warning {
  .meter__rms {
    background: #e60808;
  }

  .meter__peak {
    background: #bd1010;
  }

  .meter__peak-bar {
    background: #871616;
  }
}

            
          
!
            
              
/**
 * Utils
 */
const isOverLoud = (value, options) => value >= options.warnThreshold;
const convertToPercent = (value, options) => {
  // TODO: 計算正確的刻度
  const min = options.range[0];
  const max = options.range[1];
  const ratio = 100 / (max - min);

  if (value >= max) return 100;
  if (value <= min) return 0;

  return (value - min) * ratio;
};

/**
 * 音量儀表
 */
const Meter = props => {
  const showLeftWarn = isOverLoud(props.rms.left, props.options)
    ? "warning"
    : "";
  const showRightWarn = isOverLoud(props.rms.right, props.options)
    ? "warning"
    : "";
  return (
    <div className="meter-wrapper" style={{ height: props.options.mainHeight }}>
      {/* 0 dB 基準線 */}
      <div
        className="progress-bar-vertical meter__threshold"
        role="progressbar"
        style={{
          bottom: `${convertToPercent(0, props.options)}%`
        }}
      />
      {/* 左聲道 */}
      <div
        className={`progress-vertical meter ${showLeftWarn}`}
        style={{ width: props.options.perWidth }}
      >
        <div
          className="progress-bar-vertical meter__rms"
          role="progressbar"
          style={{
            height: `${convertToPercent(props.rms.left, props.options)}%`
          }}
          aria-valuenow={convertToPercent(props.rms.left, props.options)}
          aria-valuemin="0"
          aria-valuemax="100"
        />
        <div
          className="progress-bar-vertical meter__peak-bar"
          role="progressbar"
          style={{
            height: `${convertToPercent(props.peak.left, props.options)}%`
          }}
          aria-valuenow={convertToPercent(props.peak.left, props.options)}
          aria-valuemin="0"
          aria-valuemax="100"
        />
        <div
          className="progress-bar-vertical meter__peak"
          role="progressbar"
          style={{
            bottom: `${convertToPercent(props.peak.left, props.options)}%`,
            display:
              convertToPercent(props.peak.left, props.options) > 0
                ? "initial"
                : "none"
          }}
          aria-valuenow={convertToPercent(props.peak.left, props.options)}
          aria-valuemin="0"
          aria-valuemax="100"
        />
      </div>
      {/* 右聲道 */}
      <div
        className={`progress-vertical meter ${showRightWarn}`}
        style={{ width: props.options.perWidth }}
      >
        <div
          className="progress-bar-vertical meter__rms"
          role="progressbar"
          style={{
            height: `${convertToPercent(props.rms.right, props.options)}%`
          }}
          aria-valuenow={convertToPercent(props.rms.right, props.options)}
          aria-valuemin="0"
          aria-valuemax="100"
        />
        <div
          className="progress-bar-vertical meter__peak-bar"
          role="progressbar"
          style={{
            height: `${convertToPercent(props.peak.right, props.options)}%`
          }}
          aria-valuenow={convertToPercent(props.peak.right, props.options)}
          aria-valuemin="0"
          aria-valuemax="100"
        />
        <div
          className="progress-bar-vertical meter__peak"
          role="progressbar"
          style={{
            bottom: `${convertToPercent(props.peak.right, props.options)}%`,
            display:
              convertToPercent(props.peak.right, props.options) > 0
                ? "initial"
                : "none"
          }}
          aria-valuenow={convertToPercent(props.peak.right, props.options)}
          aria-valuemin="0"
          aria-valuemax="100"
        />
      </div>
    </div>
  );
};

Meter.propTypes = {
  options: PropTypes.shape({
    range: PropTypes.arrayOf(PropTypes.number),
    warnThreshold: PropTypes.number,
    mainHeight: PropTypes.string,
    perWidth: PropTypes.string
  }),
  rms: PropTypes.shape({
    left: PropTypes.number,
    right: PropTypes.number
  }),
  peak: PropTypes.shape({
    left: PropTypes.number,
    right: PropTypes.number
  })
};

Meter.defaultProps = {
  options: {
    range: [-90, 6],
    warnThreshold: 0,
    mainHeight: "200px",
    perWidth: "8px"
  },
  rms: {
    left: 0,
    right: -24
  },
  peak: {
    left: 3,
    right: -18
  }
};

/**
 * Layout
 */
const Layout = () => (
  <div className="container">
    <Meter />
  </div>
)

ReactDOM.render(<Layout />, document.getElementById('root'));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console