Modify the cube's position by utilizing mesh.position.set(x, y, z)
To align your object to the top-left corner of the screen, I employed window.innerWidth
and window.innerHeight
.
Check out the revised JSFiddle demo showcasing your box in the upper-left section of the screen.
If you're unsatisfied with how the cube moves while being dragged and want to continue its normal rotation, eliminate the use of orbitControls
. Instead, opt for this code (jQuery is necessary):
// jQuery required
var isDragging = false;
var previousMousePosition = {
x: 0,
y: 0
};
$(renderer.domElement).on('mousedown', function(e) {
isDragging = true;
})
.on('mousemove', function(e) {
var deltaMove = {
x: e.offsetX - previousMousePosition.x,
y: e.offsetY - previousMousePosition.y
};
if(isDragging) {
var deltaRotationQuaternion = new THREE.Quaternion()
.setFromEuler(new THREE.Euler(
toRadians(deltaMove.y * 1),
toRadians(deltaMove.x * 1),
0,
'XYZ'
));
mesh.quaternion.multiplyQuaternions(deltaRotationQuaternion, mesh.quaternion);
}
previousMousePosition = {
x: e.offsetX,
y: e.offsetY
};
});
$(document).on('mouseup', function(e) {
isDragging = false;
});
function toRadians(angle) {
return angle * (Math.PI / 180);
}
function toDegrees(angle) {
return angle * (180 / Math.PI);
}
Code source: https://jsfiddle.net/MadLittleMods/n6u6asza/
Try out the updated example using your code: https://jsfiddle.net/3eau15pv/3/