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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:20:06+00:00 2026-06-07T17:20:06+00:00

I have already seen this question Draw a line with two mouse clicks on

  • 0

I have already seen this question

Draw a line with two mouse clicks on HTML5 canvas?

What I want to achieve is practically the same, but using Jquery.

I have currently tried this, but I guess I can’t use two nested click events.

 $(function() {

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

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


context.strokeStyle = "rgb(251, 243, 243)";


 worksheetCanvas.click(function(e) {

    context.beginPath();


    var xCoordFirst = e.pageX;
    var yCoordFirst = e.pageY;

    context.moveTo(xCoordFirst, yCoordFirst);


    worksheetCanvas.click(function(f) {

        var xCoordSecond = f.pageX;
        var yCoordSecond = f.pageY;

        context.lineTo(xCoordSecond , yCoordSecond );
        context.closePath();
        context.stroke();

     })

  })


 })​

Also when I put some static values in lineTo(), I get this, which recognizes the coordinates for the second point, thus drawing the line, even though it is useless if the user doesn’t set by themselves the second point.

 $(function() {

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

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


context.strokeStyle = "rgb(251, 243, 243)";


worksheetCanvas.click(function(e) {

    context.beginPath();


    var xCoordFirst = e.pageX;
    var yCoordFirst = e.pageY;

    context.moveTo(xCoordFirst, yCoordFirst);


    worksheetCanvas.click(function(f) {

        var xCoordSecond = f.pageX;
        var yCoordSecond = f.pageY;

        context.lineTo(20 , 30);
        context.closePath();
        context.stroke();

    })

 })


})​
  • 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-07T17:20:08+00:00Added an answer on June 7, 2026 at 5:20 pm
    var mouse = {
        x: -1,
        y: -1
    };
    
    $(document).ready(function(){
        var cvs = $("canvas")[0].getContext("2d");
        $("canvas").click(function(e){
            if(mouse.x != -1 && mouse.y != -1){
                cvs.beginPath();
                cvs.moveTo(mouse.x, mouse.y);
                cvs.lineTo(e.pageX, e.pageY);
                cvs.closePath();
                cvs.stroke();
                mouse.x = -1;
                mouse.y = -1;
            }else{
                mouse.x = e.pageX;
                mouse.y = e.pageY;
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this question/answer: How do you draw text on a <canvas> tag
I've seen that a few instances of this problem have been raised already. However,
I want to combine AND OR mysql queries in CI. I have already seen
I have seen the related question here: An object with the same key already
I have already download and install this package to support HTML5 input type http://support.microsoft.com/kb/2468871
I have seen this question asked in a couple of different ways on SO
I already seen some question from here (stackoverflow) and THIS post, but I still
I have seen this question floating around the internet, but I haven't found a
I know this question has been asked before and I have seen a plethora
I've already seen this question but it's been asked over a year ago. Perhaps

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.