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

  • SEARCH
  • Home
  • 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 6697873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:30:42+00:00 2026-05-26T06:30:42+00:00

Friends, i’m finding rotating a text canvas object a bit tricky. The thing is,

  • 0

Friends, i’m finding rotating a text canvas object a bit tricky. The thing is, I’m drawing a graphic, but sometimes the width of each bar is smaller than the ‘value’ of that bar. So I have to ratate the ‘value’ 90 degrees. It will work in most cases.

I am doing the following

a function(x, y, text, maxWidth...)
var context = this.element.getContext('2d');

var metric = context.measureText(text); //metric will receive the measures of the text

if(maxWidth != null){
    if(metric.width > maxWidth) context.rotate(Math.PI / 2);
}
context.fillText(text, x, y);

Ok, but it doesn’t really work. Problems that I have seen: The text duplicates in different angles. The angles are not what I want (perhaps just a matter of trigonometry).

Well I just don’t know what to do. I read something about methods like ‘save’ and ‘restore’ but I don’t what to do with them. I’ve made some attempts but no one worked.

Would you help me with this, guys?

  • 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-26T06:30:42+00:00Added an answer on May 26, 2026 at 6:30 am

    This is a bit tricky to answer simply because there are a lot of concepts going on, so I’ve made you an example of what I think you’d like to do here:

    http://jsfiddle.net/5UKE3/

    The main part of it is this. I’ve put in a lot of comments to explain whats going on:

    function drawSomeText(x, y, text, maxWidth) {
        //metric will receive the measures of the text
        var metric = ctx.measureText(text); 
        console.log(metric.width);
    
        ctx.save(); // this will "save" the normal canvas to return to
        if(maxWidth != null && metric.width > maxWidth) {
            // These two methods will change EVERYTHING
            // drawn on the canvas from this point forward
            // Since we only want them to apply to this one fillText,
            // we use save and restore before and after
    
            // We want to find the center of the text (or whatever point you want) and rotate about it
            var tx = x + (metric.width/2);
            var ty = y + 5;
    
            // Translate to near the center to rotate about the center
            ctx.translate(tx,ty);
            // Then rotate...
            ctx.rotate(Math.PI / 2);
            // Then translate back to draw in the right place!
            ctx.translate(-tx,-ty);
        }
        ctx.fillText(text, x, y);
        ctx.restore(); // This will un-translate and un-rotate the canvas
    }
    

    To rotate around the right spot you have to translate to that spot, then rotate, then translate back.

    Once you rotate the canvas the context is rotated forever, so in order to stop all your new drawing operations from rotating when you dont want them to, you have to use save and restore to “remember” the normal, unrotated context.

    If anything else doesn’t make sense let me know. Have a good time making canvas apps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Friends, I know how to deploy and retrieve a single element in LINQ, but
Friends, I am new to lucene full text search. i have developed page with
Friends On HP-UX box when Iam passing a string object to function Im getting
Friends text box with autocomplete script (which supports multiple word completion from userstable fullname
Friends, I have an application, ten pages, but full of buttons(with background images), big
friends, here is my layout with image and text. i want to show text
friends, i am using following layout and tring to enter text and then saving
Friends, I am trying to validate my html form using an external javascript but
Friends: in PostgreSQL plpython, am trying to do an iterative search/replace in a text
Friends, I m quite new so sorry for the basic question but after hours

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.