Samantha Groves
Coder
Hi I want to create my own drawing app in HTML,CSS and Javascript.I am familiar with C# and Windows Forms Apps and how keyboard or mouse inputs work there:
I have this HTML code:
and it doesnt work.I have several questions:
a)The canvas element is simply a container for graphics in order to draw something , you need Javascript.So what does the .stroke() function do?Does it update the graphics?
Unless we call it , the output isnt updated right?
And b)How does the mouse event work?Im suprised it didnt work because I used to do similarly in WPA application.A boolean value must hold the value if any mousekey is pressed and if that constant is true ( or '1' in my case) then you can draw.
I have this HTML code:
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");
function red() {
color = "#FF0000";
}
function green()
{
color = "#00FF00";
}
function blue()
{
color = "#0000FF";
}
function drawLine(x1,y1,x2,y2)
{
c = canvas.getContext("2d");
c.color = color;
c.moveTo(x1,y1);
c.lineTo(x2,y2);
c.stroke();
}
function mouseDown()
{
mouseDownVar = 1;
}
canvas.onmousemove = function mouseMove(e)
{
if(mouseDownVar==1)
{
drawLine(e.x,e.y,e.x-10,e.y-10);
}
}
</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:aliceblue; width:1024px; height:512px;position:fixed;top:100px;left:0" onmousedown="mouseDown()">
</canvas>
and it doesnt work.I have several questions:
a)The canvas element is simply a container for graphics in order to draw something , you need Javascript.So what does the .stroke() function do?Does it update the graphics?
Unless we call it , the output isnt updated right?
And b)How does the mouse event work?Im suprised it didnt work because I used to do similarly in WPA application.A boolean value must hold the value if any mousekey is pressed and if that constant is true ( or '1' in my case) then you can draw.