|
|
|
|
|
|
|
|
|
|
|
var MATH_PI = Math.PI; |
|
|
|
var FORCE_MOBILE_FOR_TESTING = false; |
|
|
|
|
|
var TOP_ASPECT_RATIO_MAX = 1.6; |
|
var TOP_ASPECT_RATIO_MIN = 0.4; |
|
|
|
var SOUND = getQueryVariable("sound") == null ? "sine" : getQueryVariable("sound").toLowerCase(); |
|
|
|
|
|
|
|
|
|
|
|
var context = null; |
|
|
|
var width, height; |
|
|
|
var t0, t1; |
|
|
|
var needToInitializeCanvas = true; |
|
var canvasElement, ctx, startButtonElement; |
|
|
|
var isMobile; var mobileOS; |
|
|
|
var leftMargin, rightMargin, topMargin, bottomMargin; |
|
|
|
var aspectRatio; |
|
|
|
var isMousePressing = false; |
|
|
|
var currentTouches = new Array; |
|
|
|
var m; |
|
|
|
var audioContext = Tone.context; |
|
|
|
|
|
|
|
|
|
|
|
|
|
var findCurrentTouchIndex = function (id) { |
|
for (var i=0; i < currentTouches.length; i++) { |
|
if (currentTouches[i].id === id) { |
|
return i; |
|
} |
|
} |
|
|
|
return -1; |
|
}; |
|
|
|
|
|
|
|
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, |
|
}); |
|
} |
|
}; |
|
|
|
|
|
|
|
var touchMoved = function (e) { |
|
var touches = e.changedTouches; |
|
|
|
for (var i=0; i < touches.length; i++) { |
|
var touch = touches[i]; |
|
var currentTouchIndex = findCurrentTouchIndex(touch.identifier); |
|
if (currentTouchIndex >= 0) { |
|
var currentTouch = currentTouches[currentTouchIndex]; |
|
|
|
currentTouch.pageX = touch.pageX; |
|
currentTouch.pageY = touch.pageY; |
|
|
|
currentTouches.splice(currentTouchIndex, 1, currentTouch); |
|
} else { |
|
console.log('Touch was not found!'); |
|
} |
|
} |
|
}; |
|
|
|
|
|
|
|
var touchEnded = function (e) { |
|
var touches = e.changedTouches; |
|
|
|
for (var i=0; i < touches.length; i++) { |
|
var touch = touches[i]; |
|
var currentTouchIndex = findCurrentTouchIndex(touch.identifier); |
|
|
|
|
|
if (MODE == 0) { |
|
|
|
|
|
} else if (MODE == 1) { |
|
var g; |
|
|
|
for (var i = 0; i < m.arrGrabbers.length; i++) { |
|
g = m.arrGrabbers[i]; |
|
if (g.touchId == touch.identifier) { |
|
|
|
g.dropAll(); |
|
m.destroyGrabber(g); |
|
} |
|
} |
|
} |
|
|
|
|
|
if (currentTouchIndex >= 0) { |
|
var currentTouch = currentTouches[currentTouchIndex]; |
|
|
|
currentTouches.splice(currentTouchIndex, 1); |
|
} else { |
|
console.log('Touch was not found!'); |
|
} |
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
var touchCancelled = function (e) { |
|
var touches = e.changedTouches; |
|
for (var i=0; i < touches.length; i++) { |
|
var currentTouchIndex = findCurrentTouchIndex(touches[i].identifier); |
|
|
|
|
|
if (mode == 0) { |
|
|
|
|
|
} else if (mode == 1) { |
|
var g; |
|
|
|
for (var i = 0; i < m.arrGrabbers.length; i++) { |
|
g = m.arrGrabbers[i]; |
|
if (g.touchId == touch.identifier) { |
|
m.destroyGrabber(g); |
|
} |
|
} |
|
|
|
} else if (mode == 2) { |
|
|
|
|
|
} else { |
|
|
|
} |
|
|
|
|
|
if (currentTouchIndex >= 0) { |
|
|
|
currentTouches.splice(currentTouchIndex, 1); |
|
} else { |
|
console.log('Touch was not found!'); |
|
} |
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
}; |
|
|
|
|
|
|
|
|
|
window.addEventListener('load', function() { |
|
|
|
|
|
canvasElement = document.getElementById("canvas0"); |
|
startButtonElement = document.getElementById("startButtonImage"); |
|
ctx = canvasElement.getContext("2d"); |
|
|
|
contextClass = (window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext); |
|
if (contextClass) { |
|
context = new contextClass(); |
|
} else { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
xMouse = 0; yMouse = 0; |
|
|
|
|
|
m = new HarmonicController(); |
|
|
|
rsize(); |
|
|
|
begin(); |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
var render = function() { |
|
|
|
t1 = context.currentTime; |
|
timeDelta = t1-t0; |
|
|
|
ctx.clearRect(0, 0, width, height); |
|
|
|
m.upd(); |
|
|
|
t0 = t1; |
|
|
|
requestAnimFrame(render); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
var begin = function() { |
|
|
|
rsize(); |
|
m.begin(); |
|
|
|
canvasElement.addEventListener('touchstart', function(e) { |
|
e.preventDefault(); |
|
touchStarted(event); |
|
}); |
|
|
|
canvasElement.addEventListener('touchend', function(e) { |
|
e.preventDefault(); |
|
touchEnded(e); |
|
}); |
|
|
|
canvasElement.addEventListener('touchleave', function(e) { |
|
e.preventDefault(); |
|
touchEnded(e); |
|
}); |
|
|
|
canvasElement.addEventListener('touchmove', function(e) { |
|
e.preventDefault(); |
|
touchMoved(e); |
|
}); |
|
|
|
canvasElement.addEventListener('touchcancel', function(e) { |
|
e.preventDefault(); |
|
touchCancelled(e); |
|
}); |
|
|
|
canvasElement.addEventListener('mousedown', function(e) { |
|
e.preventDefault(); |
|
mouseDown(e); |
|
}); |
|
|
|
canvasElement.addEventListener('mousemove', function(e) { |
|
e.preventDefault(); |
|
mouseMove(e); |
|
}); |
|
|
|
canvasElement.addEventListener('mouseup', function(e) { |
|
e.preventDefault(); |
|
mouseUp(e); |
|
}); |
|
|
|
|
|
requestAnimFrame(render); |
|
}; |
|
|
|
|
|
window.requestAnimFrame = (function(){ |
|
return window.requestAnimationFrame || |
|
window.webkitRequestAnimationFrame || |
|
window.mozRequestAnimationFrame || |
|
function( callback ){ |
|
window.setTimeout(callback, 1000 / 60); |
|
}; |
|
})(); |
|
|
|
var finishedFile = function(bufferPm) { |
|
bufferLoader.finishedFile(bufferPm); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
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+')'; |
|
} |
|
|
|
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+')'; |
|
} |
|
|
|
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]; |
|
} |
|
|
|
function lim(n, n0, n1) { |
|
if (n < n0) { return n0; } else if (n >= n1) { return n1; } else { return n; } |
|
} |
|
|
|
var norm = function(a,a0,a1) { |
|
return (a-a0)/(a1-a0); |
|
} |
|
|
|
function sign(n) { |
|
if (n < 0) return -1; |
|
else return 1; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
var lineIntersect = function(A,B,E,F) { |
|
var ip, a1, a2, b1, b2, c1, c2; |
|
|
|
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; |
|
|
|
var det=a1*b2 - a2*b1; |
|
|
|
if (det == 0) { return null; } |
|
|
|
var xip = (b1*c2 - b2*c1)/det; |
|
var yip = (a2*c1 - a1*c2)/det; |
|
|
|
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; } |
|
|
|
return new Point(xip, yip); |
|
} |
|
|
|
|
|
|
|
|
|
var Point = function(px,py) { |
|
this.x = px; this.y = py; |
|
} |
|
|
|
|
|
function rsize(e) { |
|
|
|
var lastWidth = width; |
|
var lastHeight = height; |
|
|
|
width = window.innerWidth; |
|
height = window.innerHeight; |
|
|
|
aspectRatio = width/height; |
|
|
|
|
|
leftMargin = rightMargin = height * 0.07; |
|
topMargin = bottomMargin = height * 0.07; |
|
|
|
var isSameSize = (lastWidth == width) && (lastHeight == height); |
|
|
|
xCenter = Math.round(width * 0.5); |
|
yCenter = Math.round(height / 2); |
|
|
|
if ((canvasElement != null) && (!isSameSize || needToInitializeCanvas)) { |
|
if (needToInitializeCanvas) needToInitializeCanvas = false; |
|
canvasElement.width = window.innerWidth; |
|
canvasElement.height = window.innerHeight; |
|
|
|
ctx.lineCap = 'round'; |
|
} |
|
|
|
|
|
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); |
|
|
|
|
|
function startAudio(){ |
|
if (audioContext.state !== 'running'){ |
|
audioContext.resume() |
|
} |
|
} |
|
|
|
|
|
var interval = setInterval(function(){ |
|
if (audioContext.state === 'running'){ |
|
clearInterval(interval); |
|
document.querySelector('#startbutton').remove() |
|
} |
|
}, 100); |
|
|