skip to Main Content

i want to let my users to draw their signatures to approve that they accept my user agreements. I created canvas and javascript code, but instead of drawing under the mouse, it draws next to it.

You can see how it works here:

https://streamable.com/lz9h1z

JS CODE:

// Select the canvas element
const canvas = document.getElementById('signature-pad');
const ctx = canvas.getContext('2d');



// Set up event listeners
let isDrawing = false;
let lastX = 0;
let lastY = 0;

canvas.addEventListener('mousedown', e => {
    isDrawing = true;
    lastX = e.offsetX;
    lastY = e.offsetY;
});

canvas.addEventListener('mousemove', e => {
    if (isDrawing) {
        ctx.beginPath();
        ctx.moveTo(lastX, lastY);
        ctx.lineTo(e.offsetX, e.offsetY);
        ctx.stroke();
        lastX = e.offsetX;
        lastY = e.offsetY;
    }
});

canvas.addEventListener('mouseup', () => {
    isDrawing = false;
});

canvas.addEventListener('mouseout', () => {
    isDrawing = false;
});

// Set up button event listeners
const clearButton = document.getElementById('clear-button');
clearButton.addEventListener('click', () => {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
});

const saveButton = document.getElementById('save-button');
saveButton.addEventListener('click', () => {
    const dataURL = canvas.toDataURL();
    console.log(dataURL);
});

HTML CODE:

                        <canvas id="signature-pad" width="400" height="200"></canvas>

I was trying to do something with offsets but still no idea.

2

Answers


  1. You can get the mouse position using this code

    function getMouse(e) {
      var rect = canvas.getBoundingClientRect();
      mouseX = (e.clientX - rect.left) / (rect.right - rect.left) * canvas.width,
      mouseY = (e.clientY - rect.top) / (rect.bottom - rect.top) * canvas.height;
    }
    

    Check out the replit here: https://VeneratedJovialCryptos.hackinggo306.repl.co

    Login or Signup to reply.
  2. I have create an html file with the JS that is related just to the canvas, and everything works as expected. Here is the code:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <canvas id="signature-pad" width="400" height="200"></canvas>
        
        <script>
            // Select the canvas element
            const canvas = document.getElementById('signature-pad');
            const ctx = canvas.getContext('2d');
    
            // Set up event listeners
            let isDrawing = false;
            let lastX = 0;
            let lastY = 0;
    
            canvas.addEventListener('mousedown', e => {
                isDrawing = true;
                lastX = e.offsetX;
                lastY = e.offsetY;
            });
            canvas.addEventListener('mousemove', e => {
                if (isDrawing) {
                    ctx.beginPath();
                    ctx.moveTo(lastX, lastY);
                    ctx.lineTo(e.offsetX, e.offsetY);
                    ctx.stroke();
                    lastX = e.offsetX;
                    lastY = e.offsetY;
                }
            });
    
            canvas.addEventListener('mouseup', () => {
                isDrawing = false;
            });
    
            canvas.addEventListener('mouseout', () => {
                isDrawing = false;
            });
        </script>
    </body>
    
    </html>
    

    Here is the result of running this html file: https://streamable.com/69bwa9

    So you can try to place this code in a separate file, and see if everything works- if yes, then you should look for a problem in other parts of the code, if still no, maybe if is something about differences between physical machines, I am trying this on Mac.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search