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'd like to do a line-graph on a web-page using Javascript. I want it
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 get authentication to work by following the tutorial from this page
I need to draw an arc using GraphicsPath and having initial, median and final
I've a running graph library developed with onDraw plotting the graph using canvas.drawLine methods.
I'm working with images like: https://i.stack.imgur.com/zmQr0.jpg I've managed to find a vertical line between
I have, after some help ( MSChart - Forcing a line chart to draw
I'd previously managed to incorporate OpenStreetMaps into my application using osmdroid-android-3.0.1.jar with the great

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.