Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS Help with my Canvas - mouse shift

Asylums

New Coder
Hi,

i encounter a trouble with my Canvas, i mean : when i draw my line on it, it work, but with a shift
as exemple : if i clic on top 150 left 50.
the stroke will appear at top 100 left 0.

i can use my code on a new html / js file and it works well on "new page" as a full screen canvas.
But i want to use it in a website inside a div with the same dimension as the div

the plan is to draw on an image ( as background of .canvas-place)

can someone help me with this ?
to resume : i want my canvas to fill all my div name .canvas-place with a background and no mousse shift when i clic :)
thanks

PART HTML :

HTML:
      <div class="main-left">
        <h2>PLACEMENT</h2>
        <div class="canvas-place">
           <canvas id="canvas"></canvas>
        </div>
       
      </div>

PART CSS :

CSS:
.main-left {
  width: 60%;
  height: 60vh;
  text-align: center;

}
.main-left h2 {
  font-size: 3em;
}
.canvas-place {
  border: 2px solid lightgreen;
  border-radius: 10%;
  width: 90%;
  height: 80%;
  background:url(/picture/content/corps.webp);
  background-color: white;
  background-size:contain;
  background-repeat: no-repeat;
  background-position: center;
  cursor: pointer;
  margin-left: 60px;
 
}

PART JS :
JavaScript:
window.addEventListener("load",()=>{
    const canvas = document.getElementById("canvas");

    const ctx = canvas.getContext("2d");
   
    canvas.height =window.innerHeight;
    canvas.width =window.innerWidth;

    let painting = false;

    function startPosition(e){
        painting = true;
        draw(e);
    }
    function finishedPosition(){
        painting = false;
        ctx.beginPath();
    }

    function draw(e){
        if (!painting) return;
        ctx.lineWidth=5;
        ctx.lineCap ="round";
        ctx.lineTo(e.clientX,e.clientY);
        ctx.stroke();
        ctx.beginPath();
        ctx.moveTo(e.clientX,e.clientY);
    }

    canvas.addEventListener("mousedown",startPosition);
    canvas.addEventListener("mouseup",finishedPosition);
    canvas.addEventListener("mousemove",draw);

});
 

New Threads

Buy us a coffee!

Back
Top Bottom