Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
How to calibrate the pencil line with the canvas?

I am using html canvas in my sketchpad project and when i use try to draw on the canvas my line is forming at other coordinates inside the canvas different from my mouse pointer coordinates . According to my guess this bug has something to do with the position or width or height of my canvas because at the top-left corner the line and the pointer are at the same coordinates . I just started using js , can anyone help me solve out this problem ?

heres my js code

pencil.onclick = function(){
    console.log('pencil clicked');
    function pencil_draw(){
        console.log('pencil selected');
        var isDrawing = false;
        var x = 0;
        var y = 0;
        canvas.addEventListener('mousedown',e=>{
            console.log('mousedown activated');
            x = e.offsetX;
            y = e.offsetY;
            isDrawing = true;
        });
        canvas.addEventListener('mousemove',e=>{
            if(isDrawing == true){
                console.log('mousemove activated');
                drawline(context,x,y,e.offsetX,e.offsetY);
                x = e.offsetX;
                y = e.offsetY;
            }
        });
        canvas.addEventListener('mouseup',e=>{
            if(isDrawing == true){
                console.log('mouseup activated');
                drawline(context,x,y,e.offsetX,e.offsetY);
                x=0;
                y=0;
                isDrawing = false;
            }
        })
        function drawline(context,x,y,x1,y1){
            context.beginPath();
            context.strokeStyle = "black";
            context.lineWidth = "2";
            context.moveTo(x, y);
            context.lineTo(x1, y1);
            context.stroke();
            context.closePath();
        }
    }
    pencil_draw();
}

heres my css for the canvas

.canvasbox #canvas{
    width:500px;
    height:500px;
}

HTML of canvas

<section class="canvasbox">
        <canvas id="canvas"></canvas>
    </section>

heres my project link if anyone wants to check the whole code , its very early so there are no media query in it https://github.com/sharmaachintya08/sketchpad/tree/calibrate

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda