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 3992258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:44:35+00:00 2026-05-20T06:44:35+00:00

I managed to draw a line on a canvas using html5: ctx.moveTo(x1, y1); ctx.lineTo(x2,

  • 0

I managed to draw a line on a canvas using html5:

ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();

This works. I now want to “annotate” the line with text. So basically, I want there to be custom (e.g. whatever I pass in) text appearing along the length of the line. The difficulty is that the line can appear in any orientation (e.g. have any slope) so the text needs to be oriented accordingly. Any ideas how to start?

  • 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-20T06:44:36+00:00Added an answer on May 20, 2026 at 6:44 am

    I have created an example of this on my website. In general, you want to:

    1. translate the context to the anchor point of the text, then
    2. rotate the context by the amount (in radians) you desire, and then
    3. fillText as normal.

    I have included the relevant portion of my example below; I leave it as an exercise to the reader to detect when the text is upside down and handle it as desired.
    Edit: view the source on my site for additional code that keeps the text upright and also auto-truncates it.

    function drawLabel( ctx, text, p1, p2, alignment, padding ){
      if (!alignment) alignment = 'center';
      if (!padding) padding = 0;
    
      var dx = p2.x - p1.x;
      var dy = p2.y - p1.y;   
      var p, pad;
      if (alignment=='center'){
        p = p1;
        pad = 1/2;
      } else {
        var left = alignment=='left';
        p = left ? p1 : p2;
        pad = padding / Math.sqrt(dx*dx+dy*dy) * (left ? 1 : -1);
      }
    
      ctx.save();
      ctx.textAlign = alignment;
      ctx.translate(p.x+dx*pad,p.y+dy*pad);
      ctx.rotate(Math.atan2(dy,dx));
      ctx.fillText(text,0,0);
      ctx.restore();
    }
    

    For Firefox only you also have the option of using mozTextAlongPath. (Deprecated)

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

Sidebar

Related Questions

I've managed to draw numbers inside custom colored circle annotations (based on this ).
I want to draw tiled images and then transform them by using the usual
To make it short: I want to draw a line graph with JavaScript without
I'd like to do a line-graph on a web-page using Javascript. I want it
I'm trying to draw lines with d3js using d3.svg.line() with no success. var line
I want to draw a simple line on the iPhone by touching and dragging
I have tried to draw Polygon using Drawing Manager and send the Polygon Coordinates
I managed to create a dynamic three dropdown list using Javascript code and it
I managed to apply this excellent example to my needs: http://code.google.com/p/galleriffic/issues/attachmentText?id=76&aid=-3045121742886940548&name=example-2.html&token=4nlvmgAfVpc5whcXC9UEqHZaSz0%3A1339257750721 With a small
I have, after some help ( MSChart - Forcing a line chart to draw

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.