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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:58:42+00:00 2026-06-07T11:58:42+00:00

I want to draw a multiline (I mean some consecutive lines that share one

  • 0

I want to draw a multiline (I mean some consecutive lines that share one coordinate with the previous and one with the next line) on mouse clicks. I have currently achieved this through mousemove event in jQuery:

var worksheetCanvas = $('#worksheet-canvas');

var context = worksheetCanvas.get(0).getContext("2d");

var mouse = {
     x: -1,
     y: -1
}

var parentOffset = $('#canvas-holder').parent().offset();
worksheetCanvas.click(function(e) {

    mouse.x = e.pageX - parentOffset.left;
    mouse.y = e.pageY - parentOffset.top;

    context.beginPath();
    context.moveTo(mouse.x, mouse.y);

    $(this).mousemove(function(k) {

        context.strokeStyle = "rgb(180,800,95)";

        context.lineTo(k.pageX - parentOffset.left, k.pageY - parentOffset.top);
        context.closePath();
        context.stroke();

    })

})​

But as I can show you in this example, when I move my mouse cursor, it leaves a trail of lines, which somehow I need to delete to create the impression of one single movable line which needs another point to be defined. I have tried to clear the unneeded lines, but the 2d context only has an clearRect() method, whereas I’m looking for something like clearPath().

  • 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-06-07T11:58:44+00:00Added an answer on June 7, 2026 at 11:58 am

    There is not clearPath method according to the canvas reference

    However, you are not going to need it anyways. If my assumptions are right, all you need to do is to store the values of previous strokes into an array:

    I made a jsfiddle here so you can see it working and play around with it.

    Clearing the canvas on every move is very effective and no performance issue.

    Source Code with Explanations

    $(function() {
    
    var worksheetCanvas = $('#worksheet-canvas');
    
    var context = worksheetCanvas.get(0).getContext("2d");
    var clicked = false;
    
    // maximum number of consecutive lines to be drawn
    var maxLines = 5;
    
    // The array of stored lines
    var storedLines = [];
    
    // The object for the last stored line
    var storedLine = {};
    var mouse = {
        x: -1,
        y: -1
    }
    
    var parentOffset = $('#canvas-holder').parent().offset();
    worksheetCanvas.click(function(e) {
        clicked = true;
    
        mouse.x = e.pageX - parentOffset.left;
        mouse.y = e.pageY - parentOffset.top;
    
        context.moveTo(mouse.x, mouse.y);
    
        // Push last line to the stored lines
    
        if (clicked) {
            storedLines.push({
                startX: storedLine.startX,
                startY: storedLine.startY,
                endX: mouse.x,
                endY: mouse.y
            });
    
        }
    
        // set last line coordinates
    
        storedLine.startX = mouse.x;
        storedLine.startY = mouse.y;
    
        $(this).mousemove(function(k) {
            if (storedLines.length > maxLines) {
                storedLines.shift();
            }
    
            // clear the canvas
    
            context.clearRect(0, 0, 960, 500);
            context.beginPath();
            context.strokeStyle = "rgb(180,800,95)";
    
            // draw the stored lines
    
            for (var i = 0; i < storedLines.length; i++) {
                var v = storedLines[i];
                context.moveTo(v.startX, v.startY);
                context.lineTo(v.endX, v.endY);
                context.stroke();
            }
            context.moveTo(mouse.x, mouse.y);
            context.lineTo(k.pageX - parentOffset.left, k.pageY - parentOffset.top);
            context.stroke();
    
            context.closePath();
    
    
        })
    
    })
    
    })​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to draw some lines and rectangles on a panel. Sometimes it does
I want to draw a line in Crystal report. I can do that from
I want to draw some items on screen, each item is in one of
I want to draw line in table view cell so that I can place
I want to draw line on UIImageView. I already draw line on UIImageView but
I want to draw some listview items disabled and would like to mimic the
I want to draw this in my view to draw this line, I have
I want to draw a graph in my application.can any one tell me which
I want to draw some hlines and vlines snapped to occupy whole pixels on
I want to draw on top of a SurfaceTexture that is connected to a

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.