|
|
|
|
|
|
|
|
|
|
|
var backgroundDots = []; |
|
var Harmonic = function(indPm, controllerPm) { |
|
|
|
this.ind = indPm; |
|
this.m = controllerPm; |
|
|
|
this.series = this.ind + 1; |
|
|
|
this.periods = (this.series / 2); |
|
|
|
this.ratioStroke = 0.0; |
|
this.strokeMin = 2.8; this.strokeMax = 3.3; |
|
|
|
this.boxWidth; this.boxHeight; |
|
|
|
this.xp; this.yp; |
|
|
|
this.amplitudeMin; this.amplitudeMax; |
|
|
|
this.amplitudeRatio = 0; |
|
|
|
this.halfPeriodLength; |
|
|
|
var arrColorRange = [ |
|
[190, 190, 190, 1.0], |
|
[255, 183, 41, 1.0] |
|
]; |
|
this.colorMin = arrColorRange[0]; |
|
this.colorMax = arrColorRange[1]; |
|
this.colorCurr = this.colorMin; |
|
|
|
this.colorRatio = 0; |
|
|
|
this.oscCt = 0 -this.ind * 0.6; |
|
|
|
this.isPlaying = false; |
|
|
|
this.volumeMax = -2; this.volumeMin = -2; |
|
this.volumeRatio = 1; |
|
|
|
this.easingRelease = 0.04; |
|
this.easingAttack = 0.2; |
|
this.easing = this.easingAttack; |
|
|
|
this.ratioTarget = 0.0; |
|
this.ratioCurr = 0.0; |
|
|
|
this.isATouchOverMe = false; |
|
|
|
this.init(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Harmonic.prototype.init = function() { |
|
|
|
this.cv = ctx; |
|
|
|
this.synth = new Tone.Synth().toMaster(); |
|
|
|
|
|
this.synth.oscillator.type = SOUND; |
|
this.synth.envelope.attack = 0.5; |
|
this.synth.envelope.decay = 0.5; |
|
this.synth.envelope.sustain = 0.9; |
|
this.synth.envelope.release = 1.1; |
|
|
|
this.subSynth = new Tone.Synth().toMaster(); |
|
this.subSynth.oscillator.type = "sawtooth2"; |
|
this.subSynth.envelope.attack = 1.0; |
|
this.subSynth.envelope.decay = 0.5; |
|
this.subSynth.envelope.sustain = 0.9; |
|
this.subSynth.envelope.release = 1.1; |
|
this.subSynth.volume.value = -10; |
|
this.setVolume(0); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Harmonic.prototype.setVolume = function(r) { |
|
this.volumeRatio = r; |
|
var harmonicStrength = 1 - (this.ind / this.m.harmonics); |
|
var mainVolume = lerp(this.volumeMin, this.volumeMax, this.volumeRatio) + harmonicStrength * 6; |
|
var subVolume = lerp(this.volumeMin, this.volumeMax - 10, this.volumeRatio) + harmonicStrength * 6; |
|
|
|
this.synth.volume.value = mainVolume; |
|
this.subSynth.volume.value = subVolume; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Harmonic.prototype.upd = function() { |
|
this.oscCt += 0.9; |
|
|
|
this.ratioCurr += (this.ratioTarget - this.ratioCurr) * this.easing; |
|
this.amplitudeRatio = this.colorRatio = this.strokeRatio = this.ratioCurr; |
|
|
|
|
|
var ct = (1 + Math.sin(this.oscCt)) / 2; |
|
|
|
if (this.isPlaying) { |
|
this.amplitudeRatio += ct * 0.15; |
|
} |
|
|
|
|
|
|
|
this.colorCurr = lerpColor(this.colorMin, this.colorMax, this.colorRatio); |
|
var strokeBase = lerp(this.strokeMin, this.strokeMax, this.strokeRatio); |
|
var strokeCurr = strokeBase; |
|
if (this.isPlaying) { |
|
strokeCurr = strokeBase + Math.sin(this.oscCt * 2) * 1.2; |
|
} |
|
var amplitudeCurr = lerp(this.amplitudeMin, this.amplitudeMax, this.amplitudeRatio); |
|
|
|
|
|
|
|
var centerX = width / 2; |
|
var centerY = height / 2; |
|
var baseRadius = 30; |
|
var stepRadius = 15; |
|
|
|
var radius = 2 * (baseRadius + (this.m.harmonics - this.ind - 1) * stepRadius + (amplitudeCurr * 0.5)); |
|
var x = centerX; |
|
var y = centerY; |
|
|
|
|
|
var transparentColor = [this.colorCurr[0], this.colorCurr[1], this.colorCurr[2], 0.1]; |
|
if (this.isPlaying) { |
|
|
|
var wavelength = 440; |
|
var amplitude = radius * 0.05; |
|
var segments = 1000; |
|
this.cv.beginPath(); |
|
for (let i = 0; i <= segments; i++) { |
|
var angle = (i / segments) * Math.PI * 2; |
|
var r = radius + Math.sin(angle * this.series + this.oscCt * 0.05) * amplitude; |
|
var px = centerX + Math.cos(angle) * r; |
|
var py = centerY + Math.sin(angle) * r; |
|
if (i === 0) { |
|
this.cv.moveTo(px, py); |
|
} else { |
|
this.cv.lineTo(px, py); |
|
} |
|
} |
|
this.cv.closePath(); |
|
this.cv.fillStyle = getColor(transparentColor); |
|
this.cv.fill(); |
|
this.cv.lineWidth = strokeCurr; |
|
this.cv.strokeStyle = getColor(this.colorCurr); |
|
this.cv.stroke(); |
|
} else { |
|
|
|
this.cv.beginPath(); |
|
this.cv.arc(x, y, radius, 0, Math.PI * 2); |
|
this.cv.fillStyle = getColor(transparentColor); |
|
this.cv.fill(); |
|
this.cv.lineWidth = strokeCurr; |
|
this.cv.strokeStyle = getColor(this.colorCurr); |
|
this.cv.stroke(); |
|
this.cv.closePath(); |
|
} |
|
|
|
if (this.isPlaying) { |
|
var dotSpacing = 5; |
|
var dotSize = 3.5; |
|
var segments = 300; |
|
var freq = this.series; |
|
var amp = amplitudeCurr * 30; |
|
for (let i = 0; i <= segments; i++) { |
|
let angle = (i / segments) * Math.PI * 2; |
|
let sineOffset = Math.sin(angle * freq + this.oscCt * 0.05) * amp; |
|
let r = radius + sineOffset; |
|
let px = centerX + Math.cos(angle) * r; |
|
let py = centerY + Math.sin(angle) * r; |
|
|
|
this.cv.beginPath(); |
|
this.cv.arc(px, py, dotSize, 0, Math.PI * 2); |
|
this.cv.fillStyle = getColor([255, 255, 255, 1.0]); |
|
this.cv.fill(); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Harmonic.prototype.updateSizeAndPosition = function() { |
|
|
|
var drawAreaWidth = (width - leftMargin - rightMargin); |
|
var drawAreaHeight = (height - topMargin - bottomMargin); |
|
|
|
var aspectRatio = drawAreaWidth / drawAreaHeight; |
|
|
|
var aspectRatioMax = TOP_ASPECT_RATIO_MAX; |
|
var aspectRatioMin = TOP_ASPECT_RATIO_MIN; |
|
if (aspectRatio > aspectRatioMax) { |
|
drawAreaWidth = drawAreaHeight * aspectRatioMax; |
|
} else if (aspectRatio < aspectRatioMin) { |
|
drawAreaHeight = drawAreaWidth / aspectRatioMin; |
|
} |
|
|
|
var xLeft = (width - drawAreaWidth) / 2; |
|
var yTop = topMargin; |
|
|
|
this.boxHeight = drawAreaHeight; |
|
this.boxWidth = (drawAreaWidth / this.m.harmonics); |
|
|
|
this.periodLength = (this.boxHeight / this.series) * 2; |
|
this.halfPeriodLength = (this.boxHeight / this.series); |
|
|
|
this.amplitudeMax = (this.boxHeight * 0.04) / 2; |
|
|
|
var maxLimit = this.boxWidth * 0.5; |
|
var minLimit = this.boxWidth * 0.2; |
|
if (this.amplitudeMax > maxLimit) { |
|
this.amplitudeMax = maxLimit; |
|
} else if (this.amplitudeMax < minLimit) { |
|
this.amplitudeMax = minLimit; |
|
} |
|
|
|
|
|
this.amplitudeMin = this.amplitudeMax * 0.75; |
|
|
|
this.xp = xLeft + this.boxWidth * this.ind; |
|
this.yp = yTop; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Harmonic.prototype.checkIfATouchIsOverMe = function(x, y) { |
|
var centerX = width / 2; |
|
var centerY = height / 2; |
|
var baseRadius = 30; |
|
var stepRadius = 15; |
|
var radius = 2 * (baseRadius + (this.m.harmonics - this.ind - 1) * stepRadius); |
|
var dx = x - centerX; |
|
var dy = y - centerY; |
|
var distance = Math.sqrt(dx * dx + dy * dy); |
|
if (distance <= radius + stepRadius * 0.5 && distance >= radius - stepRadius * 0.5) { |
|
this.isATouchOverMe = true; |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Harmonic.prototype.checkIfIShouldBePlaying = function() { |
|
if (this.isATouchOverMe) { |
|
if (!this.isPlaying) { |
|
this.startPlaying(); |
|
} |
|
} else { |
|
if (this.isPlaying) { |
|
this.stopPlaying(); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Harmonic.prototype.startPlaying = function() { |
|
this.isPlaying = true; |
|
var baseFreq = 136.10; |
|
var freq = baseFreq * (this.ind + 1); |
|
this.synth.triggerAttack(freq); |
|
this.subSynth.triggerAttack(freq / 4); |
|
this.easing = this.easingAttack; |
|
this.ratioTarget = 1.0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Harmonic.prototype.stopPlaying = function() { |
|
this.isPlaying = false; |
|
if (audioContext.state == "running") this.synth.triggerRelease(); |
|
this.subSynth.triggerRelease(); |
|
this.easing = this.easingRelease; |
|
this.ratioTarget = 0.0; |
|
} |
|
|