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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:07:05+00:00 2026-05-25T13:07:05+00:00

For one of my application I would need to draw a dashed curves on

  • 0

For one of my application I would need to draw a dashed curves on the bezier path in Html5 canvas… The dash’ length and gaps in between should be variable… It is achivable in JavaFx, see this link… I would like to achieve same effect using Html5 canvas. I know how to draw dashed straight lines, but not curved lines along the bezier…

Though I am not an expert, I know the bezier drawing algorithm, problem I see with this algorithm is, it allows you to identify coordinates on the bezier using the time parameter which ranges from 0 to 1…

This is not sufficient because to draw a dashed bezier, I would need to draw many small beziers, with specified length parameter and at given gap distance, on the main bezier path. There must be some algorithm which is used by JavaFx. If anyone can help me out that would be great.

  • 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-25T13:07:05+00:00Added an answer on May 25, 2026 at 1:07 pm

    I would presume that JavaFX is using a general technique for drawing any dashed curve and just happens to be using it on a bezier in that example.

    The hard part is figuring out where to start and stop each dash, which requires knowing the arc length of your bezier curve at various points along it.

    There is an analytic approach, but I would suggest the following:

    var bezier = function(controlPoints, t) {
      /* your code here, I'll presume it returns a 2-element array of x and y. */
    };
    
    //just figure out the coordinates of all the points in each dash, don't draw.
    //returns an array of arrays, each sub-array will have an even number of nu-
    //merical elements, to wit, x and y pairs.
    
    //Argument dashPattern should be an array of alternating dash and space
    //lengths, e.g., [10, 10] would be dots, [30, 10] would be dashes,
    //[30, 10, 10, 10] would be 30-length dash, 10-length spaces, 10-length dash
    // and 10-length space.
    var calculateDashedBezier = function(controlPoints, dashPattern) {
      var step = 0.001; //this really should be set by an intelligent method,
                        //rather than using a constant, but it serves as an
                        //example.
    
      //possibly gratuitous helper functions
      var delta = function(p0, p1) {
        return [p1[0] - p0[0], p1[1] - p0[1]];
      };
      var arcLength = function(p0, p1) {
        var d = delta(p0, p1);
        return Math.sqrt(d[0]*d[0] + d[1] * d[1]);
      };
    
      var subPaths = [];
      var loc = bezier(controlPoints, 0);
      var lastLoc = loc;
    
      var dashIndex = 0;
      var length = 0;
      var thisPath = [];
      for(var t = step; t <= 1; t += step) {
        loc = bezier(controlPoints, t);
        length += arcLength(lastLoc, loc);
        lastLoc = loc;
    
        //detect when we come to the end of a dash or space
        if(length >= dashPattern[dashIndex]) {
    
          //if we are on a dash, we need to record the path.
          if(dashIndex % 2 == 0)
            subPaths.push(thisPath);
    
          //go to the next dash or space in the pattern
          dashIndex = (dashIndex + 1) % dashPattern.length;
    
          //clear the arclength and path.
          thisPath = [];
          length = 0;
        }
    
        //if we are on a dash and not a space, add a point to the path.
        if(dashIndex % 2 == 0) {
          thisPath.push(loc[0], loc[1]);
        }
      }
      if(thisPath.length > 0)
        subPaths.push(thisPath);
      return subPaths;
    };
    
    //take output of the previous function and build an appropriate path
    var pathParts = function(ctx, pathParts) {
      for(var i = 0; i < pathParts.length; i++) {
        var part = pathParts[i];
        if(part.length > 0)
          ctx.moveTo(part[0], part[1]);
        for(var j = 1; j < part.length / 2; j++) {
          ctx.lineTo(part[2*j], part[2*j+1]);
        }
      }
    };
    
    //combine the above two functions to actually draw a dashed curve.
    var drawDashedBezier = function(ctx, controlPoints, dashPattern) {
      var dashes = calculateDashedBezier(controlPoints, dashPattern);
      ctx.beginPath();
      ctx.strokeStyle = /* ... */
      ctx.lineWidth = /* ... */
      pathParts(ctx, dashes);
      ctx.stroke();
    };
    

    The main problem with this approach is its unintelligent granularity. When step is too big for your (small) dashes or (big) curve, the step size will not work well and dash boundaries will not fall exactly where you want them to. When step is too small, you may end up doing lineTo()s on points that are a sub-pixel distance away from each other, making for AA artifacts sometimes. Filtering out sub-pixel distance coordinates is not hard, but it is inefficient to generate more ‘vertices’ than you really need. Coming up with a better step size is actually something I’d consider attacking more analytically.

    There is one bonus to using this approach: if you replace bezier(controlPoints, t) with anything else that evaluates to a curve, you’ll be drawing dashed whatevers!– again with the same potential problems listed in the previous paragraph. But a really good solution to the granularity problem could work for all ‘well-behaved’ curves.

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

Sidebar

Related Questions

I would like to multi-thread an application, however one library i'm using is not
When I started my universal application, I thought the one target option would be
iam developing one application.In that i need to get the music files from the
Iam developing one application.In that iam using the uilabels.And my problem is i need
I have an application where I draw polygons on inkCanvas. I would like to
In my previous applications when I used linq-to-sql I would always use one class
Actually I am developing one application in that when application run first time it
Iam developing one application.In that iam placing the radio buttons(uiimageview) on table view and
I am developing one application.In that i am using the imageviews .SO before changeing
i am working on one application which requires the feature of adding the custom

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.