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.m12.l3
      .section.section-input
        form
          .col.s12
            .row
              .col.s12
                .header Aspect Ratio

            .row
              .col.s6.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

              .col.s6.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

              .col.s12
                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="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)
                label presets

          .col.s12
            .row
              .col.s12
                .header Width

            .row
              .input-field.col.s6
                input#input-width-min.validate(type="number", v-model.number="filterWidth.min")
                label(for="input-width-min") min

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

    // 結果
    .col.m12.l9
      .section.section-output
        .col.s12
          h2
            small Size of aspect ratio 
            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
            tbody

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

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

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


/* 出力フィールド */
.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;
}
            
          
!
            
              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;
      });
      
      return sizes;
    }
  },
  
  methods: {
    /**
     * プリセットが変更された
     *
     * @param value '{width}:{height}'
     */
    onChangePreset (value) {
      const ratio = value.split(':');
      this.ratio = {
        width: ratio[0],
        height: ratio[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