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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <h1>和音</h1>
<p>スリーコード</p>
<ul id="piano">
   <li id="Key_C" class="w_key">C</li>
   <li id="Key_F" class="w_key">F</li>
   <li id="Key_G" class="w_key">G</li>
</ul>
              
            
!

CSS

              
                h1, p {
  text-align: center;
}

#piano {
  background: #333;
  padding: 10px;
  width: 105px;
  display: block;
  margin: 0 auto;
}

.w_key {
  background: #FFF;
  border: 0px solid #333;
  width: 30px;
  text-align: center;
  padding: 50px 0;
  display: inline-block;
}

.w_key:hover {
  opacity: 0.7;
}
              
            
!

JS

              
                //DOM
var Key_C = document.querySelector('#Key_C');
var Key_F = document.querySelector('#Key_F');
var Key_G = document.querySelector('#Key_G');
var Keys = [Key_C, Key_F, Key_G]

//音階
var scale = [
  //休符
  'null', 
  //1オクターブ目
  'C4', 'C#4', 'D4', 'D#4', 'E4', 'F4', 'F#4', 'G4', 'G#4', 'A4', 'A#4', 'B4',
  //2オクターブ目
  'C5', 'C#5', 'D5', 'D#5', 'E5', 'F5', 'F#5', 'G5', 'G#5', 'A5', 'A#5', 'B5'];

//ルート音
var root = {C: 0, Cs: 1, D: 2, Ds: 3, E: 4, F: 5, Fs: 6, G: 7, Gs: 8, A: 9, As: 10, B: 11};

//メジャースケール
var codeM =[1,5,8];

//3コード
var threeCode = [root['C'],root['F'],root['G']]
var C_chord = [];
var F_chord = [];
var G_chord = [];
var chords = [C_chord,F_chord,G_chord]

//和音
for (var i = 0 ; i < codeM.length; i++ ) {
  //var C_chord, F_chord, G_chord
  for (var  j = 0; j < codeM.length; j++){
    var nmb = scale[threeCode[i]+codeM[j]];
    chords[i].push(nmb); 
  }
}

//シンセ生成
var synth = new Tone.PolySynth().toMaster();

//イベントリスナ
for (var i = 0; i < Keys.length; i++) {
(function(i) {
  Keys[i].addEventListener('click', function () { 
  //3コードが8分音符の長さ鳴る
  synth.triggerAttackRelease(chords[i], '4n');
  }, false);
 })(i);
}
              
            
!
999px

Console