ase / Harmonic2222.js
gafoart's picture
Upload 6 files
5268b4e verified
/**
* ------------------------------------------------
* Harmonic class.
* ------------------------------------------------
*/
var backgroundDots = [];
var Harmonic = function(indPm, controllerPm) {
// my index number
this.ind = indPm;
this.m = controllerPm;
// my harmonic series number
this.series = this.ind + 1;
// how many periods will I draw
this.periods = (this.series / 2);
// Stroke size.
this.ratioStroke = 0.0;
this.strokeMin = 2.8; this.strokeMax = 3.3;
// width and height of the bounding box drawing area of each harmonic
this.boxWidth; this.boxHeight;
// my position
this.xp; this.yp;
// Amplitude range.
this.amplitudeMin; this.amplitudeMax;
// Current amplitude is stored as a ratio from 0 to 1 between min and max
this.amplitudeRatio = 0;
// Period size - length of a full sine wave cycle
this.halfPeriodLength;
// color range - r, g, b, a - stored as both current color and ratio 0 to 1
var arrColorRange = [
[190, 190, 190, 1.0], // Starting color: grey
[255, 183, 41, 1.0] // Ending color: when highlighted
];
this.colorMin = arrColorRange[0];
this.colorMax = arrColorRange[1];
this.colorCurr = this.colorMin;
// Current color is stored as a ratio in that range
this.colorRatio = 0;
// counter
this.oscCt = 0 -this.ind * 0.6;
// Am I currently playing?
this.isPlaying = false;
// My volume range (decibels, e.g. 0, -1, -2 etc.)
this.volumeMax = -2; this.volumeMin = -2;
this.volumeRatio = 1;
// Easing ratio (0 to 1)
this.easingRelease = 0.04;
this.easingAttack = 0.2;
this.easing = this.easingAttack;
// My overall ratio of pressed amount from 0 to 1
this.ratioTarget = 0.0;
this.ratioCurr = 0.0;
// store whether a touch event is currently touching me
this.isATouchOverMe = false;
//
this.init();
}
/**
* ------------------------------------------------
* Initialize.
* ------------------------------------------------
*/
Harmonic.prototype.init = function() {
// set the canvas I draw to
this.cv = ctx;
// create synth object
this.synth = new Tone.Synth().toMaster();
// What kind of waveform? "sine" "triangle" etc. Passed in via URL e.g. /?sound=sine
// If not passed in, it defaults to "sine"
this.synth.oscillator.type = SOUND; // "triangle";
this.synth.envelope.attack = 100;
this.synth.envelope.decay = 4;
this.synth.envelope.sustain = 0.9;
this.synth.envelope.release = 10;
// Add sub-octave synth setup
this.subSynth = new Tone.Synth().toMaster();
this.subSynth.oscillator.type = "sawtooth2";
this.subSynth.envelope.attack = 4;
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);
}
/**
* ------------------------------------------------
* Set volume.
* ------------------------------------------------
*/
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;
}
/**
* ------------------------------------------------
* Update function.
* ------------------------------------------------
*/
Harmonic.prototype.upd = function() {
this.oscCt += 0.9; // ensure background dots animate regardless of playback
this.ratioCurr += (this.ratioTarget - this.ratioCurr) * this.easing;
this.amplitudeRatio = this.colorRatio = this.strokeRatio = this.ratioCurr;
// Sin goes from -1 to 1. Normalize to a 0 to 1 range
var ct = (1 + Math.sin(this.oscCt)) / 2;
// make it quiver
if (this.isPlaying) {
this.amplitudeRatio += ct * 0.15;
}
// Calculate current color and size
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; // sine wave modulated stroke
}
var amplitudeCurr = lerp(this.amplitudeMin, this.amplitudeMax, this.amplitudeRatio);
// Arrange circles in concentric layout
var centerX = width / 2;
var centerY = height / 2;
var baseRadius = 30;
var stepRadius = 15;
var radius = 3 * (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) {
// Draw sine wave
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.save();
this.cv.shadowColor = getColor(this.colorCurr);
this.cv.shadowBlur = 20;
this.cv.fillStyle = getColor(transparentColor);
this.cv.fill();
this.cv.lineWidth = strokeCurr;
this.cv.strokeStyle = getColor(this.colorCurr);
this.cv.stroke();
this.cv.restore();
} else {
// Draw a filled circle
this.cv.save();
this.cv.shadowColor = getColor(this.colorCurr);
this.cv.shadowBlur = 20;
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();
this.cv.restore();
}
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();
}
}
}
/**
* ------------------------------------------------
* Update the size.
* ------------------------------------------------
*/
Harmonic.prototype.updateSizeAndPosition = function() {
//
var drawAreaWidth = (width - leftMargin - rightMargin);
var drawAreaHeight = (height - topMargin - bottomMargin);
// What's the aspect ratio? Width / height, e.g. 2:1 = 2.0.
var aspectRatio = drawAreaWidth / drawAreaHeight;
// Don't let it get too wide
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;
}
// where is the upper left corner of this
var xLeft = (width - drawAreaWidth) / 2;
var yTop = topMargin;
// set length of the harmonic to the window height
this.boxHeight = drawAreaHeight;
this.boxWidth = (drawAreaWidth / this.m.harmonics);
// Size of the half-period - how many px for a single crest to go up and down
this.periodLength = (this.boxHeight / this.series) * 2;
this.halfPeriodLength = (this.boxHeight / this.series);
// set max amplitude size. Just make it almost fill up the box.
this.amplitudeMax = (this.boxHeight * 0.04) / 2;
// Don't let them get bigger than the box width though
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.amplitudeMax = this.boxWidth * 0.5;
// Set the minimum amplitude to a little bit smaller than the max.
this.amplitudeMin = this.amplitudeMax * 0.75;
// Set my position
this.xp = xLeft + this.boxWidth * this.ind;
this.yp = yTop;
}
/**
* ------------------------------------------------
* Check if given x,y value intersects with this one (radial detection)
* ------------------------------------------------
*/
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) {
this.isATouchOverMe = true;
if (!this.isPlaying) {
this.startPlaying();
}
} else {
this.isATouchOverMe = false;
if (this.isPlaying) {
this.stopPlaying();
}
}
}
/**
* ------------------------------------------------
* Check if given x,y value intersects with this one
* ------------------------------------------------
*/
Harmonic.prototype.checkIfIShouldBePlaying = function() {
if (this.isATouchOverMe) {
if (!this.isPlaying) {
this.startPlaying();
}
} else {
if (this.isPlaying) {
this.stopPlaying();
}
}
}
/**
* ------------------------------------------------
* Start playing
* ------------------------------------------------
*/
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;
}
/**
* ------------------------------------------------
* Stop playing
* ------------------------------------------------
*/
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;
}