Calculator Tools Calculator Tools
Create

AI Drawing Canvas

Nov 12, 2023

About this app

A fun drawing canvas web app.

AI Drawing Canvas

Related apps

Put this on your site

Source

                    <html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>AI Drawing Canvas</title>
<meta name="description" content="A fun drawing canvas web app.">
<meta name="keywords" content="web app, drawing, canvas">

<style>
/* App CSS Goes Here */
body {
background: linear-gradient(to right, #ff9966, #ff5e62);
font-family: 'Pacifico', cursive;
}
#canvas {
border: 1px solid #000;
cursor: crosshair;
}
#main-container {
text-align: center;
margin-top: 50px;
}
</style>

<link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet">

<script type="text/javascript">
try {
// App Javascript Goes Here. Place your entire script content inside the try block for error handling.

let canvas, ctx, flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false;

let x = "black",
y = 2;

function init() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext("2d");
w = canvas.width;
h = canvas.height;

canvas.addEventListener("mousemove", function (e) {
findxy('move', e)
}, false);
canvas.addEventListener("mousedown", function (e) {
findxy('down', e)
}, false);
canvas.addEventListener("mouseup", function (e) {
findxy('up', e)
}, false);
canvas.addEventListener("mouseout", function (e) {
findxy('out', e)
}, false);
}

function draw() {
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.strokeStyle = x;
ctx.lineWidth = y;
ctx.stroke();
ctx.closePath();
}

function findxy(res, e) {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;

flag = true;
dot_flag = true;
if (dot_flag) {
ctx.beginPath();
ctx.fillStyle = x;
ctx.fillRect(currX, currY, 2, 2);
ctx.closePath();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
draw();
}
}
}

// This will run when the DOM is ready.
document.addEventListener("DOMContentLoaded", function() {
init();
});

} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>
</head>
<body>
<div id="main-container" class="container">
<!-- App HTML Goes Here -->
<canvas id="canvas" width="700" height="400"></canvas>
</div>
</body>
</html>