Calculator Tools Calculator Tools
Create

Fun Drawing App

Nov 12, 2023

About this app

A fun, interactive drawing app.

Fun Drawing App

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>Fun Drawing App</title>
<meta name="description" content="A fun, interactive drawing app.">
<meta name="keywords" content="drawing, app, interactive, fun, colorful">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script type="text/javascript">
try {
var canvas, ctx, flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false,
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 color(obj) {
switch (obj.id) {
case "green":
x = "green";
break;
case "blue":
x = "blue";
break;
case "red":
x = "red";
break;
case "yellow":
x = "yellow";
break;
case "orange":
x = "orange";
break;
case "black":
x = "black";
break;
case "white":
x = "white";
break;
}
if (x == "white") y = 14;
else y = 2;
}

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();
}
}
}

document.addEventListener("DOMContentLoaded", function() {
init();
});
} catch (error) {
throw error;
}
</script>

<style>
/* App CSS Goes Here */
#canvas {border:2px solid #000;}
.color {width:30px; height:30px; cursor:pointer;}
</style>
</head>
<body>
<div id="main-container" class="container">
<div style="margin:20px;">
<canvas id="canvas" width="500" height="500" style="position:absolute; border:1px solid;"></canvas>
<div style="position:relative; margin-top:550px;">
<div id="green" class="color" style="background:green;"></div>
<div id="blue" class="color" style="background:blue;"></div>
<div id="red" class="color" style="background:red;"></div>
<div id="yellow" class="color" style="background:yellow;"></div>
<div id="orange" class="color" style="background:orange;"></div>
<div id="black" class="color" style="background:black;"></div>
<div id="white" class="color" style="background:white; border:1px solid #000;"></div>
</div>
</div>
</div>
</body>
</html>