What I have is this:

What I want is to rotate the red rectangle e.g. 20 degrees, but
You can use a simple function to do this as an alternative to translate and rotate:
This function will let you draw a line starting at x and y, and end at length at angle:
function lineToAngle(ctx, x1, y1, length, angle) {
angle *= Math.PI / 180;
var x2 = x1 + length * Math.cos(angle),
y2 = y1 + length * Math.sin(angle);
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
return {x: x2, y: y2};
}
Usage:
lineToAngle(ctx, x, y, length, angle);
var canvas = document.querySelector('canvas'),
ctx = canvas.getContext('2d'),
x = 100, y =50, length = 40, angle = 0, dlt = -2;
(function animate() {
//clear
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
lineToAngle(ctx, x, y, length, angle);
ctx.lineWidth = 10;
ctx.stroke();
angle += dlt;
if (angle < -90 || angle > 0) dlt = -dlt;
requestAnimationFrame(animate);
})();
function lineToAngle(ctx, x1, y1, length, angle) {
angle *= Math.PI / 180;
var x2 = x1 + length * Math.cos(angle),
y2 = y1 + length * Math.sin(angle);
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
return {x: x2, y: y2};
}
body {background-color:#555557}
canvas {background:#ffffd;border:1px solid #000}
