JavaScript Canvas - Rectangle rotates around circle


Amma

I'm trying to make a rectangle to rotate around a circle and rotate around itself. My wonderful drawing will show what I mean :)

enter image description here

I made a rotated rectangle around the circle, but I cannot rotate the rectangle around it. Can someone help me? my 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/

Card

You can use the canvas transform to rotate the rectangle around the center point:

Demo : http://jsfiddle.net/m1erickson/k6B2z/ _

function animate(){

    // request that the animation continues when this frame is done

    requestAnimationFrame(animate);

    // draw a circle for the rect to 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 the untransformed context state

    ctx.save();

    // move the origin(0,0) of the canvas to cx,cy

    ctx.translate(cx,cy);

    // rotate the entire canvas around the new origin(cx,cy)

    ctx.rotate(rotation);

    // draw a rectangle
    // note: just draw it like it's in unrotated space
    // translate+rotate make this possible without needing lots of math!

    ctx.strokeRect(-rectWidth/2+20,-rectHeight/2,rectWidth,rectHeight);

    // restore the context to its untransformed state

    ctx.restore();

    rotation+=Math.PI/360;
}

Related


Rotate rectangle around circumference of circle on canvas

Mussmanson I'm trying to create a circular "equalizer" effect using JavaScript and HTML canvas for a small project I'm working on, and it works great, except for one small thing. It's just a series of rectangular bars moving to the mp3 over time, nothing fancy

Rotate rectangle around circumference of circle on canvas

Mussmanson I'm trying to create a circular "equalizer" effect using JavaScript and HTML canvas for a small project I'm working on, and it works great, except for one small thing. It's just a series of rectangular bars moving to the mp3 over time, nothing fancy

Rotate rectangle around circumference of circle on canvas

Mussmanson I'm trying to create a circular "equalizer" effect using JavaScript and HTML canvas for a small project I'm working on, and it works great, except for one small thing. It's just a series of rectangular bars moving to the mp3 over time, nothing fancy

Rotate rectangle around circumference of circle on canvas

Mussmanson I'm trying to create a circular "equalizer" effect using JavaScript and HTML canvas for a small project I'm working on, and it works great, except for one small thing. It's just a series of rectangular bars moving to the mp3 over time, nothing fancy

Javascript Canvas draw rectangle or circle

nameless I'm looking for a way to draw rectangles or circles "on the fly" on a canvas. I found various ways to fillRect()draw rectangles, but not in real time. I mean, being able mouseDown()to move it from one point to another point in the canvas, it defines t

SVG element rotates around a circle

ata field So I have an SVG element - the big circle - with a set of elements inside. I want to rotate these elements around this big circle. The code is pretty simple, but I'm lost on how to set this circle (graph__skils) on the correct path (big circle). As y

Animated text rotates around a circle

calyx I used lettering.js (which is a span injector) and css transforms to map a line of text around the edge of the circle. My current small goal is to animate that text using keyframes. My problem is that when I put the animation declaration in the same h1 r

SVG element rotates around a circle

ata field So I have an SVG element - the big circle - with a set of elements inside. I want to rotate these elements around this big circle. The code is pretty simple, but I'm lost on how to set this circle (graph__skils) on the correct path (big circle). As y

SVG element rotates around a circle

ata field So I have an SVG element - the big circle - with a set of elements inside. I want to rotate these elements around this big circle. The code is pretty simple, but I'm lost on how to set this circle (graph__skils) on the correct path (big circle). As y

Animated text rotates around a circle

calyx I used lettering.js (which is a span injector) and css transforms to map a line of text around the edge of the circle. My current small goal is to animate that text using keyframes. My problem is that when I put the animation declaration in the same h1 r

Rotates around the angle and position set by the circle.

DannieCoderBoi I've been struggling to make a div rotate around a circle to a set point. Here is the fiddle : http://jsfiddle.net/TMuD5/2/ However, I seem to have reached the limit of my skill level. I'm only a junior in JS/JQuery, so please be humble. If you

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

A circle moving around the edge of the canvas

mdiadi I need to move a circle around the edge of the canvas. Moving right and then down works fine, but when it needs to move left, it jumps to the bottom right and then moves right again and again. I don't know how to fix it. var can = document.getElementB

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

A circle moving around the edge of the canvas

mdiadi I need to move a circle around the edge of the canvas. Moving right and then down works fine, but when it needs to move left, it jumps to the bottom right and then moves right again and again. I don't know how to fix it. var can = document.getElementB

draw soundbar around canvas circle

quirk I have the following to plot the frequencies of audio clips as a soundbar: const drawSinewave = function() { requestAnimationFrame(drawSinewave); analyser.getByteFrequencyData(sinewaveDataArray); canvasCtx.fillStyle = 'white'; canvasCtx

A circle moving around the edge of the canvas

mdiadi I need to move a circle around the edge of the canvas. Moving right and then down works fine, but when it needs to move left, it jumps to the bottom right and then moves right again and again. I don't know how to fix it. var can = document.getElementB

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

A circle moving around the edge of the canvas

mdiadi I need to move a circle around the edge of the canvas. Moving right and then down works fine, but when it needs to move left, it jumps to the bottom right and then moves right again and again. I don't know how to fix it. var can = document.getElementB

A circle moving around the edge of the canvas

mdiadi I need to move a circle around the edge of the canvas. Moving right and then down works fine, but when it needs to move left, it jumps to the bottom right and then moves right again and again. I don't know how to fix it. var can = document.getElementB

Javascript canvas clock rotates numbers in correct position

Novara The clock figures show how the wrong position rotates to the correct position. I don't think the angle of each digit is properly corrected. There are errors in these lines of code, this is calculating the angle of each number Anyone find the bug and how

Javascript canvas clock rotates numbers in correct position

Novara The clock figures show how the wrong position rotates to the correct position. I don't think the angle of each digit is properly corrected. There are errors in these lines of code, this is calculating the angle of each number Anyone find the bug and how

Javascript canvas clock rotates numbers in correct position

Novara The clock figures show how the wrong position rotates to the correct position. I don't think the angle of each digit is properly corrected. There are errors in these lines of code, this is calculating the angle of each number Anyone find the bug and how

Javascript canvas clock rotates numbers in correct position

Novara The clock figures show how the wrong position rotates to the correct position. I don't think the angle of each digit is properly corrected. There are errors in these lines of code, this is calculating the angle of each number Anyone find the bug and how