JavaScript Canvas - rectangle spin around circle -


i'm trying make rectangle spin around circle , spin around itself. brilliant drawing show mean :)

enter image description here

i made spinning rectangle around circle can't make spin rectangle around itself. can me? code:

var game = {          canvas: document.getelementbyid('mycanvas'),          ctx: document.getelementbyid('mycanvas').getcontext('2d'),           char: {             x: 100,             y: 100,             w: 50,             h: 50,             inc: 0         },          init: function() {             game.drawall();          },          player: {             move: function() {                  game.char.inc += 0.05;                  var                      x = game.char.inc,                      y = game.char.inc;                      game.char.x = 100 * (math.cos(x));                     game.char.y = 100 * (math.sin(y));              }          },          drawall: function() {             game.ctx.clearrect(0,0,800,600);             game.ctx.save();             game.ctx.translate(400, 300);             game.ctx.rotate(math.cos(game.char.inc));             game.ctx.fillrect(game.char.x, game.char.y, game.char.w, game.char.h);             game.ctx.translate(-400, -300);             window.requestanimationframe(game.drawall);             game.player.move();         } } $(function() { game.init(); }); 

http://jsfiddle.net/mqpsu/2/

you can use canvas transforms rotate rectangle around centerpoint:

a demo: http://jsfiddle.net/m1erickson/k6b2z/

function animate(){      // request animation continues when frame done      requestanimationframe(animate);      // draw circle rect rotate around      ctx.clearrect(0,0,canvas.width,canvas.height);     ctx.beginpath();     ctx.arc(cx,cy,10,0,math.pi*2);     ctx.closepath();     ctx.fill();      // save untransformed context state      ctx.save();      // move origin(0,0) of canvas cx,cy      ctx.translate(cx,cy);      // rotate entire canvas around new origin(cx,cy)      ctx.rotate(rotation);      // draw rectangle     // note: draw it's in unrotated space     // translate+rotate make possible without needing lots of math!      ctx.strokerect(-rectwidth/2+20,-rectheight/2,rectwidth,rectheight);      // restore context untransformed state      ctx.restore();      rotation+=math.pi/360; } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -