Samantha Groves
Coder
Hello I have this code and it is supposed to be a drawing app with the ability to choose between up to 3 colors:
The conversion of the coordinates is correct ,the problematic part is this:
which I dont know why this is the case since it is a copypasta from the Internet.Help me out please!
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Draw a picture</title>
</head>
<body>
<script>
let color = "";
let mouseDownVar = 0;
const canvas = document.getElementById("myCanvas");
window.addEventListener("mousemove",(e)=>
{
if(e.clientX<1024)
{
if(e.clientY>100&&e.clientY<612)
{
if(mouseDownVar)
console.log(e.clientX + ":" + (e.clientY - 100));
drawLine(e.clientX,e.clientY-100,e.clientX+10,e.clientY-90);
}
}
}
);
function red() {
color = "#FF0000";
}
function green()
{
color = "#00FF00";
}
function blue()
{
color = "#0000FF";
drawLine(10,10,1000,1000);
}
function drawLine(x1,y1,x2,y2)
{
c = canvas.getContext("2d");
c.color = color;
c.beginPath();
c.moveTo(x1,y1);
c.lineTo(x2,y2);
c.stroke();
c.closePath();
}
function mouseDown()
{
mouseDownVar = 1;
console.log(1);
}
function mouseUp()
{
mouseDownVar=0;
console.log(0);
}
</script>
<button style="background-image:url(circle-64.jpg);height:64px;width:64px; border-color:whitesmoke;border-radius:96px;position:fixed;top:0;left:0;" onclick="red()"></button>
<button style="background-image:url(circle-64B.jpg);height:64px;width:64px; border-color:whitesmoke;border-radius:96px;position:fixed;top:0;left:100px;" onclick="blue()"></button>
<button style="background-image:url(circle-64G.jpg);height:64px;width:64px; border-color:whitesmoke;border-radius:96px;position:fixed;top:0;left:200px;" onclick="green()"></button>
<canvas id="myCanvas" style="background-color:white; width:1024px; height:512px;position:fixed;top:100px;left:0" onmousedown="mouseDown()" onmouseup="mouseUp()">
</canvas>
The conversion of the coordinates is correct ,the problematic part is this:
Code:
function drawLine(x1,y1,x2,y2)
{
c = canvas.getContext("2d");
c.color = color;
c.beginPath();
c.moveTo(x1,y1);
c.lineTo(x2,y2);
c.stroke();
c.closePath();
}
which I dont know why this is the case since it is a copypasta from the Internet.Help me out please!