Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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>8拍子</h1>
<ul id="pad">
   <li id="beat">Beat</li>
</ul>
              
            
!

CSS

              
                body {
  background: #333;
  font-family: sans-serif;
}

h1, p {
  color: #fff;
  text-align: center;
}

#pad {
  background: #333;
  padding: 10px;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  margin: 0 auto;
  position: relative;
}

li {
  margin: 5px;
  background: #666;
  border: 0px solid #333;
  width: 100px;
  height: 100px;
  line-height: 100px;
  border-radius: 5px;
  text-align: center;
  display: inline-block;
  color: #fff;
  box-shadow: 5px 5px 5px rgba(0,0,0, 0.3);
}

li:hover {
  opacity: 0.7;
}

.b_key {
  position: absolute;
  z-index: 10;
  top: 10px;
  margin: 0 -20px;
  padding: 0;
  background: #000;
  border: 1px solid #fff;
  color: #fff;
  width: 35px;
  height: 75px;
  text-align: center;
  display: inline-block; 
}

              
            
!

JS

              
                // DOM
const beat = document.querySelector('#beat');

// エンベロープ(キック)
let optsMembrane = {
  pitchDecay: 0.001,
  envelope: {
    attack: 0.001 ,
    decay: 0.75 ,
    sustain: 0.01 ,
    release: 0.01 
  },
  volume: 25
}

// エンベロープ(スネア)
let optsNoiseSnare = {
  envelope: {
    attack: 0.001 ,
    decay: 0.5 ,
    sustain: 0.01
  }
}

// エンベロープ(ハイハット)
let optsNoiseHihat = {
  type: "brown",
  envelope: {
    attack: 0.001 ,
    decay: 0.03 ,
    sustain: 0
  }
}

// シンセ生成
const membrane = new Tone.MembraneSynth(optsMembrane).toMaster();
const noise1 = new Tone.NoiseSynth(optsNoiseSnare).toMaster();
const noise2 = new Tone.NoiseSynth(optsNoiseHihat).toMaster();

// シンセ実行
const kick = () => {
  membrane.triggerAttackRelease('C0','2n'); 
};

const snare = () => {
  noise1.triggerAttackRelease('8n');
};

const hihat = () => {
  noise2.triggerAttackRelease('32n');
};

let beatNumber = 8;
let beatLength = 4 / beatNumber;

//リズム設定
let kickRhythm = [
  ['0:' + beatLength * 0 + ':0'],
  ['0:' + beatLength * 4 + ':0'],
];

let snareRhythm = [
  ['0:' + beatLength * 2 + ':0'],
  ['0:' + beatLength * 6 + ':0'],
];

let hihatRhythm = [];
for(i = 0; i < beatNumber ; i++) {
  hihatRhythm.push('0:' + beatLength * i + ':0');
}

// シンセにリズムを設定
let kickPart = new Tone.Part(kick, kickRhythm).start();
let snarePart = new Tone.Part(snare, snareRhythm).start()
let hihatPart = new Tone.Part(hihat, hihatRhythm).start();

// ループ設定
kickPart.loop = true;
snarePart.loop = true;
hihatPart.loop = true;

// BPM設定
Tone.Transport.bpm.value = 100;

// クリックイベント
beat.addEventListener('click', () => {
  Tone.Transport.toggle();
}, false);


              
            
!
999px

Console