Doodle3D-Slicer/src/utils.js

144 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-04-24 16:12:48 +02:00
/******************************************************
*
* Utils
* requires jQuery, Three.js
*
******************************************************/
var D3D = {
"version": "0.1",
"website": "http://www.doodle3d.com/",
"contact": "develop@doodle3d.com"
};
2015-04-30 20:34:57 +02:00
//add normal function to Three.js Vector class
THREE.Vector2.prototype.normal = function () {
2015-05-01 10:06:52 +02:00
"use strict";
var x = this.y;
var y = -this.x;
return this.set(x, y);
};
2015-04-24 16:12:48 +02:00
function sendAPI (url, data, callback) {
"use strict";
$.ajax({
url: url,
type: "POST",
data: data,
dataType: "json",
timeout: 10000,
success: function (response) {
if (response.status === "success") {
if (callback !== undefined) {
callback(response.data);
}
}
else {
console.warn(response.msg);
}
}
}).fail(function () {
console.warn("failed connecting to " + url);
sendAPI(url, data, callback);
});
}
function getAPI (url, callback) {
"use strict";
$.ajax({
url: url,
dataType: "json",
timeout: 5000,
success: function (response) {
if (response.status === "success") {
if (callback !== undefined) {
callback(response.data);
}
}
else {
console.warn(response.msg);
}
}
}).fail(function () {
console.warn("failed connecting to " + url);
getAPI(url, callback);
});
}
function downloadFile (file, data) {
2015-05-01 10:06:52 +02:00
"use strict";
$(document.createElement("a")).attr({
download: file,
href: "data:text/plain," + data
})[0].click();
}
2015-04-24 16:12:48 +02:00
Array.prototype.clone = function () {
"use strict";
var array = [];
for (var i = 0; i < this.length; i ++) {
array[i] = this[i];
}
return array;
};
function applyMouseControls (renderer, camera, maxDistance) {
"use strict";
//TODO
//impliment touch controls
//windows mouse wheel fix
var distance = 20;
var rotX = 0;
var rotY = 0;
var moveCamera = false;
2015-05-01 10:06:52 +02:00
2015-04-24 16:12:48 +02:00
function updateCamera () {
camera.position.x = Math.cos(rotY)*Math.sin(rotX)*distance;
camera.position.y = Math.sin(rotY)*distance;
camera.position.z = Math.cos(rotY)*Math.cos(rotX)*distance;
camera.lookAt(new THREE.Vector3(0, 0, 0));
}
$(renderer.domElement).on("mousedown", function (e) {
moveCamera = true;
}).on("wheel", function (e) {
var event = e.originalEvent;
event.preventDefault();
distance = THREE.Math.clamp(distance - event.wheelDelta, 1, maxDistance);
updateCamera();
});
$(window).on("mouseup", function (e) {
moveCamera = false;
}).on("mousemove", function (e) {
2015-05-01 10:06:52 +02:00
var event = e.originalEvent;
2015-04-24 16:12:48 +02:00
if (moveCamera === true) {
rotX = (rotX - event.webkitMovementX/100) % (2*Math.PI);
rotY = THREE.Math.clamp(rotY + event.webkitMovementY/100, -Math.PI/2, Math.PI/2);
updateCamera();
}
});
updateCamera();
}
var requestAnimFrame = (function () {
"use strict";
return requestAnimationFrame || webkitRequestAnimationFrame || mozRequestAnimationFrame || function (callback) {
setTimeout(callback, 1000/60);
};
})();