Skip to content

Instantly share code, notes, and snippets.

@zachwinter
Last active June 18, 2019 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachwinter/89bc24f340eac6a1d21f61b42a6a3eb9 to your computer and use it in GitHub Desktop.
Save zachwinter/89bc24f340eac6a1d21f61b42a6a3eb9 to your computer and use it in GitHub Desktop.
A simple `spotify-viz` sketch.
import Visualizer from './classes/visualizer'
import { interpolateRgb, interpolateBasis } from 'd3-interpolate'
import { getRandomElement } from './util/array'
import { sin, circle } from './util/canvas'
export default class Example extends Visualizer {
constructor () {
super({ volumeSmoothing: 10 })
this.theme = ['#18FF2A', '#7718FF', '#06C5FE', '#FF4242', '#18FF2A']
}
hooks () {
this.sync.on('bar', beat => {
this.lastColor = this.nextColor || getRandomElement(this.theme)
this.nextColor = getRandomElement(this.theme.filter(color => color !== this.nextColor))
})
}
paint ({ ctx, height, width, now }) {
const bar = interpolateBasis([0, this.sync.volume * 10, 0])(this.sync.bar.progress)
const beat = interpolateBasis([0, this.sync.volume * 300, 0])(this.sync.beat.progress)
ctx.fillStyle = 'rgba(0, 0, 0, .08)'
ctx.fillRect(0, 0, width, height)
ctx.lineWidth = bar
ctx.strokeStyle = interpolateRgb(this.lastColor, this.nextColor)(this.sync.bar.progress)
sin(ctx, now / 50, height / 2, this.sync.volume * 50, 100)
ctx.stroke()
ctx.fillStyle = 'rgba(0, 0, 0, 1)'
ctx.beginPath()
ctx.lineWidth = beat
circle(ctx, width / 2, height / 2, this.sync.volume * height / 5 + beat / 10)
ctx.stroke()
ctx.fill()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment