cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              #app
  .row
    .col.s12
      h1 Aspect Ratio Calc
  .row
    // 入力
    .section.section-input.col.s12
      form
        .input-ratio
          .input-field
            input#input-aspect-width.validate(type="number", min="0.0001", step="0.0001", required, v-model.number="ratio.width")
            label(for="input-aspect-width") width

          .input-field
            input#input-aspect-height.validate(type="number", min="0.0001", step="0.0001", required, v-model.number="ratio.height")
            label(for="input-aspect-height") height

          .input-field
            select
              option(value="", disabled, selected) preset
              option(value="16:9") 16:9
              option(value="16:10") 16:10
              option(value="4:3") 4:3                  
              option(value="3:2") 3:2
              option(value="5:4") 5:4
              option(value="5:3") 5:3
              option(value="21:9") 21:9
              //- option(value="1:1.618") 1:1.618 (golden ratio)
              //- option(value="1:1.1414") 1:1.1414 (sliver ratio)
              //- option(value="1:1.732") 1:1.732 (platinum ratio)
              //- option(value="1:3.303") 1:3.303 (bronze ratio)

        .input-filter
          //- 幅のフィルター
          .input-field
            input#input-width-min.validate(type="number", v-model.number="filterWidth.min")
            label(for="input-width-min") min width

          .input-field
            input#input-width-max.validate(type="number", v-model.number="filterWidth.max")
            label(for="input-width-max") max width

    // 結果
    .col.s12
      .section.section-output
        .col.s12
          h2
            span#result-aspect-ratio
              | {{ratio.width}}
              span.result-aspect-ratio__div :
              | {{ratio.height}}

          .divider
          
          .row
            p.result-num
              | {{results.length}} results

          // 計算結果
          table.striped.centered#results(v-show="results.length > 0")
            thead
              tr
                td #
                td Width
                td Height
                td Resolution
            tbody

              tr(v-for="item in results")
                td.results__line-number {{ item.index }}
                td.results__ratio {{ item.width }}
                td.results__ratio {{ item.height }}
                td.results__number-of-pixels {{ item.resolution | comma }}
            
          
!
            
              @use postcss-simple-vars;
@use postcss-nested;

body {
  margin: 20px;
  background-color: #f5f5f5;
}


h1 {
  font-size: 2rem;
}

.section {
  
  /* 見出し */
  .header {
    font-size: 1.3em;
  }
  
  /* clearfix */
  &:after{
    content: '';
    display: block;
    clear: both;
  }
  
  &:not(:nth-last-of-type(1)) {
    margin-bottom: 40px;
  }
}
  
/* 入力フィールド */
.section-input {
  position: sticky;
  top: 0;
  background-color: #f5f5f5;
}


.input-field {
  display: inline-block;
}


.input-ratio {
  display: inline-block;
  margin-right: 3rem;
  
  .input-field {
    width: 80px;
    
    &:not(:last-of-type) {
      margin-right: 0.5rem;
    }
    
    &:last-of-type {
      width: 200px;
    }
  }
}


.input-filter {
  display: inline-block;
  
  .input-field {
    width: 80px;
    
    &:not(:last-of-type) {
      margin-right: 0.5rem;
    }
  }
}


/* 出力フィールド */
.section-output {
  background-color: #fff;
}


/* 計算結果のアスペクト比 */
.result-aspect-ratio__div {
  padding-left: 0.1em;
  padding-right: 0.1em;
}


/* 出力結果 */
#results {
  width: auto;
}

/* 出力結果の行数 */
.results__line-number {
  display: inline-block;
  color: #cfd8dc;
  margin-right: 0.5em;
  user-select: none;
}

/* 出力結果の比率 */
.results__ratio {
  color: #c62828;
}

/* 画素数 */
.results__number-of-pixels {
  color: #757575;
}
            
          
!
            
              const initialValue = {
  ratio: {
    width: 4,
    height: 3
  },
  presetRatio: '',
  filterWidth: {
    min: 640,
    max: 800
  },
  results: []
};  // デフォルト値


const app = new Vue({
  el: '#app',
  data: initialValue,
                  
  computed: {
    results () {
      let sizes = calcSizeByRatio(parseFloat(this.ratio.width), parseFloat(this.ratio.height), this.filterWidth.min, this.filterWidth.max); // 計算結果
      
      // 結果にindex、画素数を付加する
      sizes.forEach(function (val, index) {
        val.index = index + 1;
        val.resolution = val.width * val.height;
      });
      
      return sizes;
    }
  },
  
  methods: {
    /**
     * プリセットが変更された
     *
     * @param value '{width}:{height}'
     */
    onChangePreset (value) {
      const ratio = value.split(':');
      this.ratio = {
        width: ratio[0],
        height: ratio[1]
      };
    }
  },
  
  filters: {
    
    /**
     * 3桁ごとにカンマ区切り
     *
     * @param num 数値
     * @return {string} 3桁区切りの文字列
     */
    comma(num) {
      return String(num).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
    }
  }
});



/**
 * 比率で割り切れるサイズを計算する
 * 
 * @param ratioWidth 
 * @param ratioHeight 
 * @param minWidth 計算するサイズの最小
 * @param maxWidth 計算するサイズの最大
 * @return 比率のリスト
 */
function calcSizeByRatio(ratioWidth = 4, ratioHeight = 3, minWidth = 100, maxWidth) {
  let result = [];  // 結果
  
  // 最大サイズの指定がない場合、最小サイズ+500に設定する。
  if (isNaN(maxWidth) === true || _.isNumber(maxWidth) === false) {
      maxWidth = minWidth + 500;
  }
  
  // 最小サイズと最大サイズが逆だった場合入れ替える。
  if (minWidth > maxWidth) {
    [minWidth, maxWidth] = [maxWidth, minWidth];
  }
  
  for (let i = minWidth; i <= maxWidth; i++) {
    let h = i * (ratioHeight / ratioWidth);
    if (h === Math.floor(h)) {
      //高さが割り切れるなら結果に追加
      result[result.length] = {
        width: i,
        height: h
      };
    }
  }
  
  return result;
}




// プルダウンの初期設定
$(function() {
  const $select = $('select');
  
  $select.material_select();
  $select.on('change', function () {
    app.onChangePreset(this.options[this.selectedIndex].value); // 選択された項目のvalue値を渡す
  });
});
            
          
!
999px
Loading ..................

Console