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

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

              
                #app
//- Left pointer to draw
//- Right pointer to erase
              
            
!

CSS

              
                *
  box-sizing border-box
  touch-action none
  
:root
  --green-one: hsl(130, 64%, 76%);
  --green-two: hsl(136, 53%, 51%);
  --green-three: hsl(136, 54%, 41%);
  --green-four: hsl(139, 54%, 28%);
  --cell-radius: 2;
  --grey: hsl(216, 14%, 93%);
  --color: hsl(0, 0%, 40%);

body
  display flex
  align-items center
  justify-content center
  min-height 100vh

.commit-grid {
  --cell-radius: 2;
  --cell-size: 11;
  --cell-gap: 5;
  --bg: var(--grey);
  display: inline-grid;
  grid-template-columns: repeat(53, auto);
  grid-template-rows: repeat(7, auto);
  grid-gap: calc(var(--cell-gap) * 1px);
  grid-auto-flow: dense;

  &:active {
    cursor: var(--cursor, pointer);
  }

  &__cell {
    height: calc(var(--cell-size) * 1px);
    width: calc(var(--cell-size) * 1px);
    background: var(--bg);
    border-radius: calc(var(--cell-radius) * 1px);
    grid-column: var(--column);

    &[data-level="1"] {
      --bg: var(--green-one);
    }
    &[data-level="2"] {
      --bg: var(--green-two);
    }
    &[data-level="3"] {
      --bg: var(--green-three);
    }
    &[data-level="4"] {
      --bg: var(--green-four);
    }
  }

}
              
            
!

JS

              
                import React, { useRef } from 'https://cdn.skypack.dev/react'
import { render } from 'https://cdn.skypack.dev/react-dom'
import { MonoSynth } from 'https://cdn.skypack.dev/tone'

// import BRUSH_PATH from '../../assets/images/brush.png'
// import './commit-grid.styl'

const MAX_LEVEL = 4

const NOTES = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
const PITCH = [5, 4, 3, 2]

const CommitGrid = ({
  cells = new Array(356).fill().map(i => 0),
  onChange = () => {},
  muted = false,
}) => {
  const gridRef = useRef(null)
  const rendered = useRef(null)
  const erasing = useRef(null)
  const synthRef = useRef(null)

  const playNote = (index, level) => {
    if (!synthRef.current) {
      synthRef.current = new MonoSynth({
        volume: -8,
        detune: 0,
        portamento: 0,
        envelope: {
          attack: 0.05,
          attackCurve: 'linear',
          decay: 0.3,
          decayCurve: 'exponential',
          release: 0.8,
          releaseCurve: 'exponential',
          sustain: 0.4,
        },
        filter: {
          Q: 1,
          detune: 0,
          frequency: 0,
          gain: 0,
          rolloff: -12,
          type: 'lowpass',
        },
        filterEnvelope: {
          attack: 0.001,
          attackCurve: 'linear',
          decay: 0.7,
          decayCurve: 'exponential',
          release: 0.8,
          releaseCurve: 'exponential',
          sustain: 0.1,
          baseFrequency: 300,
          exponent: 2,
          octaves: 4,
        },
        oscillator: {
          detune: 0,
          frequency: 440,
          partialCount: 8,
          partials: [
            1.2732395447351628,
            0,
            0.4244131815783876,
            0,
            0.25464790894703254,
            0,
            0.18189136353359467,
            0,
          ],
          phase: 0,
          type: 'square8',
        },
      }).toDestination()
    }
    synthRef.current.triggerAttackRelease(
      `${NOTES[index]}${PITCH[level - 1]}`,
      '32n'
    )
  }

  const draw = (e) => {
    e.preventDefault()
    const cell =
      e.clientX && e.clientY
        ? document.elementFromPoint(e.clientX, e.clientY)
        : e.target
    if (
      cell &&
      cell.parentNode === gridRef.current &&
      rendered.current !== cell
    ) {
      rendered.current = cell
      const INDEX = parseInt(cell.getAttribute('data-index'), 10)
      const LEVEL = parseInt(cell.getAttribute('data-level'), 10) || 0
      if (erasing.current) {
        cell.setAttribute('data-level', 0)
        cells[INDEX] = 0
        if (!muted && LEVEL !== 0)
          synthRef.current.triggerAttackRelease('A2', '32n')
      } else {
        const NEW_LEVEL = Math.min(MAX_LEVEL, LEVEL + 1)
        if (NEW_LEVEL !== LEVEL) {
          cell.setAttribute('data-level', NEW_LEVEL)
          cells[INDEX] = NEW_LEVEL
          if (!muted) playNote(INDEX % 7, NEW_LEVEL)
        }
      }
      if (onChange) onChange()
    }
  }

  const stopDrawing = (e) => {
    e.preventDefault()
    rendered.current = null
    erasing.current = false
    window.removeEventListener('pointermove', draw)
    window.removeEventListener('pointerup', stopDrawing)
  }

  const startDrawing = (e) => {
    e.preventDefault()
    // If right mouse button, go into erase mode
    if (e.button === 2) {
      e.preventDefault()
      erasing.current = true
    }
    draw(e)
    window.addEventListener('pointermove', draw)
    window.addEventListener('pointerup', stopDrawing)
  }

  const moot = (e) => {
    e.preventDefault()
    return false
  }

  if (!cells.length) return null

  return (
    <div
      onPointerDown={startDrawing}
      onContextMenu={moot}
      onDrag={moot}
      onDragEnd={moot}
      onDragEnter={moot}
      onDragExit={moot}
      onDragLeave={moot}
      onDragOver={moot}
      onDragStart={moot}
      ref={gridRef}
      className="commit-grid">
      {cells.map((cell, index) => {
        const COLUMN = Math.floor(index / 7) + 1
        return (
          <div
            style={{
              '--column': COLUMN,
            }}
            className="commit-grid__cell"
            data-level={cell || 0}
            data-index={index}
            key={index}></div>
        )
      })}
    </div>
  )
}

const App = () => <CommitGrid/>

render(<App/>, document.querySelector('#app'))
              
            
!
999px

Console