Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6251757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:40:23+00:00 2026-05-24T13:40:23+00:00

I am just trying my hands at HTML5’s most talked feature i.e canvas. There

  • 0

I am just trying my hands at HTML5’s most talked feature i.e canvas. There are 2 rectangles and I only want to rotate 1st and keep the 2nd one as it is. Problem is when the below code runs my whole canvas is rotated and both rectangles are rotated. I can’t even find any API where I can get reference to the object that I have drawn and rotate only that specific object instead of the whole context.

Code:

<script type="text/javascript">
    var context;
    var radian = 0.01;
    var w, h;
    $(document).ready(function () {
        w = document.width;
        h = document.height;
        var canvas = $('#canvas');
        context = canvas[0].getContext('2d');
        canvas[0].width = w;
        canvas[0].height = h;
        setInterval(startAnim, 200);
    });

    function startAnim() {
        context.clearRect(0, 0, w, h);
        context.strokeStyle = 'rgb(0,0,0)';
        context.fillStyle = 'rgb(0,0,0)';
        context.fillRect(0, 0, w, h);
        context.strokeStyle = 'rgb(0,0,0)';
        context.fillStyle = 'rgb(255,255,0)';
        context.rotate(radian);
        context.strokeRect(400, 300, 200, 200);
        context.fillRect(400, 300, 200, 200);
        context.fillStyle = 'rgb(0,255,255)';
        context.fillRect(500, 400, 200, 200);
        radian += 0.01;
    }
</script>

How can I prevent this from happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:40:25+00:00Added an answer on May 24, 2026 at 1:40 pm

    Finally, I have resolved the issue on my own by trial and error. This is the code:

    <script type="text/javascript">
        var context;
        var radian = 0.01;
        var w, h;
        $(document).ready(function () {
            w = document.width;
            h = document.height;
            var canvas = $('#canvas');
            context = canvas[0].getContext('2d');
            canvas[0].width = w;
            canvas[0].height = h;
            setInterval(startAnim, 100);
        });
    
        function startAnim() {
    
            context.save();
            context.strokeStyle = 'rgb(0,0,0)';
            context.fillStyle = 'rgb(0,0,0)';
            context.fillRect(0, 0, w, h);
            context.rotate(radian);
            context.strokeStyle = 'rgb(0,0,0)';
            context.fillStyle = 'rgb(255,255,0)';
            context.strokeRect(400, 300, 200, 200);
            context.fillRect(400, 300, 200, 200);
            context.restore();
    
            context.fillStyle = 'rgb(0,255,255)';
            context.fillRect(500, 400, 200, 200);
    
            radian += 0.1;
        }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to canvas some opinions here. I was wondering how people go about
I just started trying my hands on EF4 code first this morning and I
Just trying to get my head around Generics by reading this enlightening article by
Just trying to get my irb sessions to actually list the current line of
Just trying to get up to speed with the SDK... So, I've created my
Just trying to get my head round Spring and figuring out how I wire
Just trying to get diff to work better for certain kinds of documents. With
just trying to test for equality in this piece of code, but getting a
Just trying to still get my head around IOC principles. Q1: Static Methods -
Just trying to establish whether prototype can do something like $$('#ID a:last').css('color','#111'); Any ideas

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.