I want to rotate a shape on canvas on mouse move, and I want to detect if mouse is moving in clockwise direction or not, but I do not know how to do that. Here is my code:
var canvas = document.getElementById('canvas');
var img = document.getElementById('photo');
var ctx = canvas.getContext('2d');
var annotation_rect = canvas.getBoundingClientRect();
rect = {
startX : 150,
startY : 50,
w : 250,
h : 150,
endX : 0,
endY : 0,
rotate: 0
};
var drag = false;
var rotating = false;
var update = true; // when true updates canvas
var rotate_angle = 5; // in degrees - for rotating blurred part
var angle = rotate_angle * (Math.PI / 180);
var original_source = img.src;
img.src = original_source;
function rotateRight(){
rect.rotate += angle;
update = true;
}
function rotateLeft(){
rect.rotate -= angle;
update = true;
}
function init() {
img.addEventListener('load', function(){
canvas.width = img.width;
canvas.height = img.height;
canvas.addEventListener('mousedown', mouseDown, false);
canvas.addEventListener('mouseup', mouseUp, false);
canvas.addEventListener('mousemove', mouseMove, false);
});
// start the rendering loop
requestAnimationFrame(updateCanvas);
}
// main render loop only updates if update is true
function updateCanvas(){
if(update){
drawCanvas();
update = false;
}
requestAnimationFrame(updateCanvas);
}
// draws a rectangle with rotation
function drawRect(){
ctx.setTransform(1,0,0,1,rect.startX + rect.w / 2, rect.startY + rect.h / 2);
ctx.rotate(rect.rotate);
ctx.beginPath();
ctx.shadowBlur = 5;
ctx.filter = 'blur(10px)';
ctx.rect(-rect.w/2, -rect.h/2, rect.w, rect.h);
ctx.lineWidth = 3;
ctx.strokeStyle = "#fff";
ctx.fillStyle = "#fff";
ctx.fill();
ctx.stroke();
}
// clears canvas sets filters and draws rectangles
function drawCanvas(){
ctx.setTransform(1,0,0,1,0,0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'rgba(0, 0, 0, 0.6)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawRect()
}
// create new rect add to array
function mouseDown(e) {
drag = true;
}
function mouseUp() { drag = false; update = true; }
function startRotation(e){
rotating = true;
}
function stopRotation(e){
rotating = false;
}
function onShapeRotating(e){
if(rotating){
rotateRight();
}
}
function mouseMove(e) {
var mouseX = e.offsetX - annotation_rect.left,
mouseY = e.offsetY - annotation_rect.top,
endX = rect.startX + rect.w,
endY = rect.startY + rect.h
var cursorOnShape = mouseX >= rect.startX && mouseX <= endX && mouseY >= rect.startY && mouseY <= endY;
if(cursorOnShape){
canvas.style.cursor = "pointer"
canvas.addEventListener('mousedown', startRotation, false);
canvas.addEventListener('mouseup', stopRotation, false);
canvas.addEventListener('mousemove', onShapeRotating, false);
}else{
canvas.style.cursor = "default"
canvas.removeEventListener('mousedown', startRotation, false);
canvas.removeEventListener('mouseup', stopRotation, false);
canvas.removeEventListener('mousemove', onShapeRotating, false);
}
}
init();
canvas{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
display:inline-block;
background:rgba(0,0,0,0.3);
}
<div style="position: relative; overflow: hidden;display:inline-block;">
<img id="photo" src="https://carsales.pxcrush.net/carsales/car/cil/cc5166737225893351785.jpg?width=600&height=300&overlay&aspect=FitWithIn&watermark=1439104668"/>
<canvas id="canvas"></canvas>
</div>
Thus I have an rect drawn on canvas and I detect if the mouse is on that rect and if it is I call the function rotateShape and there I call the function rotateRight. And that is working. If you are on the rect with mouse and you press it down and rotate it would rotate the rect calling the function rotateRight.
But I want to be able to check if the mouse is moving in clockwise direction or not. If it moves in clockwise direction I would call rotateRight and if not then I would call rotateLeft.
Any idea how to do that?