File size: 13,044 Bytes
5268b4e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
// --------------------------------------------------
// Global Constants.
// --------------------------------------------------
// Store PI as global constant.
var MATH_PI = Math.PI;
// Force mobile testing mode to see how it would behave.
var FORCE_MOBILE_FOR_TESTING = false;
// Top area where we draw waveforms and strings, aspect ratio limits
// so for ultra wide or ultra narrow it doesn't get too spread out or scrunched.
var TOP_ASPECT_RATIO_MAX = 1.6;
var TOP_ASPECT_RATIO_MIN = 0.4;
// Pass this in url e.g. ?sound=triangle
var SOUND = getQueryVariable("sound") == null ? "sine" : getQueryVariable("sound").toLowerCase();
// --------------------------------------------------
// Global variables
// --------------------------------------------------
// the Web Audio "context" object
var context = null;
// width of the movie
var width, height;
// initialize timer variable and frame counter
var t0, t1;
// first time running resize funciton?
var needToInitializeCanvas = true;
var canvasElement, ctx, startButtonElement;
// mobile check
var isMobile; var mobileOS;
// Margins for the overall movie
var leftMargin, rightMargin, topMargin, bottomMargin;
// Aspect ratio of the window - e.g. 1.0 = square, 2.0 = 2:1 horizontal, 0.5 = 1:2 vertical.
var aspectRatio;
// Is user pressing
var isMousePressing = false;
// Used to keep track of active touches.
var currentTouches = new Array;
// Link to main current controller object
var m;
// Link to Tone.context audioContext
var audioContext = Tone.context;
//-------------------------//
// Touch Functions
//-------------------------//
// Finds the array index of a touch in the currentTouches array.
var findCurrentTouchIndex = function (id) {
for (var i=0; i < currentTouches.length; i++) {
if (currentTouches[i].id === id) {
return i;
}
}
// Touch not found! Return -1.
return -1;
};
// Creates a new touch in the currentTouches array and draws the starting
// point on the canvas.
var touchStarted = function (e) {
var touches = e.changedTouches;
for (var i=0; i < touches.length; i++) {
var touch = touches[i];
currentTouches.push({
id: touch.identifier,
pageX: touch.pageX,
pageY: touch.pageY,
});
}
};
// Draws a line on the canvas between the previous touch location and
// the new location.
var touchMoved = function (e) {
var touches = e.changedTouches;
// New code
for (var i=0; i < touches.length; i++) {
var touch = touches[i];
var currentTouchIndex = findCurrentTouchIndex(touch.identifier);
if (currentTouchIndex >= 0) {
var currentTouch = currentTouches[currentTouchIndex];
// Update the touch record.
currentTouch.pageX = touch.pageX;
currentTouch.pageY = touch.pageY;
// Store the record.
currentTouches.splice(currentTouchIndex, 1, currentTouch);
} else {
console.log('Touch was not found!');
}
}
};
// Draws a line to the final touch position on the canvas and then
// removes the touh from the currentTouches array.
var touchEnded = function (e) {
var touches = e.changedTouches;
// New code
for (var i=0; i < touches.length; i++) {
var touch = touches[i];
var currentTouchIndex = findCurrentTouchIndex(touch.identifier);
// Harmonics mode
if (MODE == 0) {
// Strings mode
} else if (MODE == 1) {
var g;
// find the grabber we should destroy
for (var i = 0; i < m.arrGrabbers.length; i++) {
g = m.arrGrabbers[i];
if (g.touchId == touch.identifier) {
// let go of threads if it's holding any
g.dropAll();
m.destroyGrabber(g);
}
}
}
// Now remove the touch itself
if (currentTouchIndex >= 0) {
var currentTouch = currentTouches[currentTouchIndex];
// Remove the record.
currentTouches.splice(currentTouchIndex, 1);
} else {
console.log('Touch was not found!');
}
}
};
// Removes cancelled touches from the currentTouches array.
var touchCancelled = function (e) {
var touches = e.changedTouches;
for (var i=0; i < touches.length; i++) {
var currentTouchIndex = findCurrentTouchIndex(touches[i].identifier);
// Harmonics mode
if (mode == 0) {
// Strings mode
} else if (mode == 1) {
var g;
// find the grabber we should destroy
for (var i = 0; i < m.arrGrabbers.length; i++) {
g = m.arrGrabbers[i];
if (g.touchId == touch.identifier) {
m.destroyGrabber(g);
}
}
// Wind mode
} else if (mode == 2) {
// Bars mode
} else {
}
// Now remove the touch object
if (currentTouchIndex >= 0) {
// Remove the touch record.
currentTouches.splice(currentTouchIndex, 1);
} else {
console.log('Touch was not found!');
}
}
};
// ----------------------------------------------
// Mouse Functions
// ----------------------------------------------
var xMouse, yMouse;
var mouseDown = function(e) {
m.mouseDown(e);
isMousePressing = true;
xMouse = e.clientX;
yMouse = e.clientY;
}
var mouseMove = function(e) {
xMouse = e.clientX;
yMouse = e.clientY;
};
var mouseUp = function(e) {
m.mouseUp(e);
isMousePressing = false;
};
// ----------------------------------------------
// Initialize
// ----------------------------------------------
window.addEventListener('load', function() {
// create canvases
canvasElement = document.getElementById("canvas0");
startButtonElement = document.getElementById("startButtonImage");
ctx = canvasElement.getContext("2d");
// Create Web Audio Context, future proofed for future browsers
contextClass = (window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext);
if (contextClass) {
context = new contextClass();
} else {
// Web Audio API not available. Ask user to use a supported browser.
}
// initialize mouse position -- *** might need to look at this later,
// as it initializes to the top left corner, the initial mouse down moment
// might be seen as a giant jump
xMouse = 0; yMouse = 0;
// create main object
m = new HarmonicController();
// run the resize function now
rsize();
// start now
begin();
});
/**
* ------------------------------------------------
* Update loop run via requestAnimationFrame.
* ------------------------------------------------
*/
var render = function() {
t1 = context.currentTime;
timeDelta = t1-t0;
// clear the draw area
ctx.clearRect(0, 0, width, height);
// ThreadController object update
m.upd();
// increment for the next loop
t0 = t1;
// next frame
requestAnimFrame(render);
};
/**
* ------------------------------------------------
* Begin dots' warmup before the song begins.
* ------------------------------------------------
*/
var begin = function() {
rsize();
m.begin();
// Set up an event listener for new touches.
canvasElement.addEventListener('touchstart', function(e) {
e.preventDefault();
touchStarted(event);
});
// Set up an event listener for when a touch ends.
canvasElement.addEventListener('touchend', function(e) {
e.preventDefault();
touchEnded(e);
});
// Set up an event listener for when a touch leaves the canvas.
canvasElement.addEventListener('touchleave', function(e) {
e.preventDefault();
touchEnded(e);
});
// Set up an event listener for when the touch instrument is moved.
canvasElement.addEventListener('touchmove', function(e) {
e.preventDefault();
touchMoved(e);
});
// Set up an event listener to catch cancelled touches.
canvasElement.addEventListener('touchcancel', function(e) {
e.preventDefault();
touchCancelled(e);
});
// Mouse events
canvasElement.addEventListener('mousedown', function(e) {
e.preventDefault();
mouseDown(e);
});
// Mouse events
canvasElement.addEventListener('mousemove', function(e) {
e.preventDefault();
mouseMove(e);
});
// Mouse events
canvasElement.addEventListener('mouseup', function(e) {
e.preventDefault();
mouseUp(e);
});
// start the drawing loop.
requestAnimFrame(render);
};
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60); // it's not using this, it's just the fallback.
};
})();
var finishedFile = function(bufferPm) {
bufferLoader.finishedFile(bufferPm);
};
// ------------------------------------------------------
// Universal functions.
// ------------------------------------------------------
// convert color - rgba format
function getColor(color) {
var r = Math.round(color[0]);
var g = Math.round(color[1]);
var b = Math.round(color[2]);
var a = (color[3]);
return 'rgba('+r+','+g+','+b+','+a+')';
}
// convert color - rgba format, but ignore alpha, set to custom alpha
function getColorMinusAlpha(color, alp) {
var r = Math.round(color[0]);
var g = Math.round(color[1]);
var b = Math.round(color[2]);
var a = alp;
return 'rgba('+r+','+g+','+b+','+a+')';
}
// linear extrapolate
function lerp(a, b, t) {
return a + (b-a)*t;
}
function lerpColor(a, b, t) {
var c1 = lerp(a[0], b[0], t);
var c2 = lerp(a[1], b[1], t);
var c3 = lerp(a[2], b[2], t);
var c4 = lerp(a[3], b[3], t);
return [c1, c2, c3, c4];
}
// limit to range
function lim(n, n0, n1) {
if (n < n0) { return n0; } else if (n >= n1) { return n1; } else { return n; }
}
// normalize
var norm = function(a,a0,a1) {
return (a-a0)/(a1-a0);
}
// return the sign of a number. -1 if negative, 1 if it's zero or positive.
function sign(n) {
if (n < 0) return -1;
else return 1;
}
// function: lim
// desc: Returns intersection of segement AB and segment EF as Point
// Returns null if there is no intersection
// params: Takes four Points, returns a Point object
//---------------------------------------------------------------
var lineIntersect = function(A,B,E,F) {
var ip, a1, a2, b1, b2, c1, c2;
// calculate
a1 = B.y-A.y; a2 = F.y-E.y;
b1 = A.x-B.x; b2 = E.x-F.x;
c1 = B.x*A.y - A.x*B.y; c2 = F.x*E.y - E.x*F.y;
// det
var det=a1*b2 - a2*b1;
// if lines are parallel
if (det == 0) { return null; }
// find point of intersection
var xip = (b1*c2 - b2*c1)/det;
var yip = (a2*c1 - a1*c2)/det;
// now check if that point is actually on both line segments using distance
if (Math.pow(xip - B.x, 2) + Math.pow(yip - B.y, 2) > Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2)) { return null; }
if (Math.pow(xip - A.x, 2) + Math.pow(yip - A.y, 2) > Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2)) { return null; }
if (Math.pow(xip - F.x, 2) + Math.pow(yip - F.y, 2) > Math.pow(E.x - F.x, 2) + Math.pow(E.y - F.y, 2)) { return null; }
if (Math.pow(xip - E.x, 2) + Math.pow(yip - E.y, 2) > Math.pow(E.x - F.x, 2) + Math.pow(E.y - F.y, 2)) { return null; }
// else it's on both segments, return it
return new Point(xip, yip);
}
// ------------------------------------------------
// class: Point
// description:
// ------------------------------------------------
var Point = function(px,py) {
this.x = px; this.y = py;
}
// When window is resized
function rsize(e) {
var lastWidth = width;
var lastHeight = height;
width = window.innerWidth;
height = window.innerHeight;
// Aspect ratio of the window - e.g. 1.0 = square, 2.0 = 2:1 horizontal, 0.5 = 1:2 vertical.
aspectRatio = width/height;
// set margins based on that
leftMargin = rightMargin = height * 0.07;
topMargin = bottomMargin = height * 0.07;
// did it not change?
var isSameSize = (lastWidth == width) && (lastHeight == height);
// center point
xCenter = Math.round(width * 0.5);
yCenter = Math.round(height / 2);
// make the canvas objects match window size
if ((canvasElement != null) && (!isSameSize || needToInitializeCanvas)) {
if (needToInitializeCanvas) needToInitializeCanvas = false;
canvasElement.width = window.innerWidth;
canvasElement.height = window.innerHeight;
// set line style (for some reason it needed to be done here...)
ctx.lineCap = 'round';
}
// Move the start button to upper right
startButtonElement.style.left = (width - 40) + "px";
startButtonElement.style.top = "10px";
m.updateSizeAndPosition();
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
}
window.addEventListener('resize', rsize, false);
// this function resumes the audio context in case it's in a suspended state
function startAudio(){
if (audioContext.state !== 'running'){
audioContext.resume()
}
}
// this checks if the context is already running and removes the button if it is
var interval = setInterval(function(){
if (audioContext.state === 'running'){
clearInterval(interval);
document.querySelector('#startbutton').remove()
}
}, 100);
|