im tried to rotate a line with this
window.onload= function(){
var canvas=document.getElementById("foo");
var context=canvas.getContext("2d");
context.moveTo(250, 50);
context.lineTo(250, 250);
context.stroke();
context.rotate(0.30);
};
what am i doing wrong? i think im missing some steps. can anyone explain?
the rotate() actually rotates the entire coordinate system. Which defaults to 0,0 (Upper left corner of your canvas). You'd need to do something along these lines:
context.save(); // saves the coordinate system
context.translate(250,50); // now the position (0,0) is found at (250,50)
context.rotate(0.30); // rotate around the start point of your line
context.moveTo(0,0) // this will actually be (250,50) in relation to the upper left corner
context.lineTo(0,200) // (250,250)
context.stroke();
context.restore(); // restores the coordinate system back to (0,0)
Check out this link for a really good explanation of how translate() and rotate() work: tutsplus tutorial
Steve
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With