Skip to content

Commit 4c48380

Browse files
committed
added code
1 parent 02a83dd commit 4c48380

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

index.css

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
@import url('https://fonts.googleapis.com/css2?family=Indie+Flower&display=swap');
2+
3+
body {
4+
font-family: 'Indie Flower', cursive;
5+
margin: 0;
6+
}
7+
8+
9+
.drawing-color button {
10+
height: 30px;
11+
width: 30px;
12+
border: transparent;
13+
opacity: 0.9;
14+
}
15+
16+
.drawing-color {
17+
position: absolute;
18+
margin-top: 10px;
19+
}
20+
21+
button#red {
22+
background: red;
23+
border-radius: 60px;
24+
}
25+
26+
button#green{
27+
background: green;
28+
border-radius: 60px;
29+
}
30+
31+
button#blue {
32+
background: blue;
33+
border-radius: 60px;
34+
}
35+
36+
button#yellow {
37+
background: yellow;
38+
border-radius: 60px;
39+
}
40+
41+
button#pink {
42+
background: pink;
43+
border-radius: 60px;
44+
}
45+
46+
button#black {
47+
background: black;
48+
border-radius: 60px;
49+
}
50+
51+
.drawing-color button:hover {
52+
cursor: pointer;
53+
opacity: 1;
54+
}
55+
56+
button#clear {
57+
position: absolute;
58+
right: 0;
59+
top: 10px;
60+
height: 20px;
61+
width: 20px;
62+
border: none;
63+
margin-right: 30px;
64+
transition: 0.3s;
65+
cursor: pointer;
66+
}
67+
68+
button#clear:after {
69+
position: absolute;
70+
top: 0;
71+
left: 0;
72+
height: 100%;
73+
width: 100%;
74+
content: "X";
75+
line-height: 21px;
76+
}

index.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel= "stylesheet" href="./index.css">
8+
<link rel= "icon" href="https://image.flaticon.com/icons/png/128/3199/3199937.png">
9+
<title>Drawing App</title>
10+
</head>
11+
<body>
12+
<section>
13+
<div class="drawing-color">
14+
<button id= "red"></button> <button id= "green"></button> <button id= "blue"></button> <button id= "yellow"></button> <button id= "pink"></button> <button id= "black"></button>
15+
</div>
16+
<div class="other-tools"></div> <button id= "clear"></button></div>
17+
<canvas height="450px" width= "650px" style="border-top: 2px solid #ccc; margin-top: 50px;"></canvas>
18+
</section>
19+
<script src= "./index.js"></script>
20+
</body>
21+
</html>

index.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var canvas = document.querySelector("canvas");
2+
var clear = document.querySelector("#clear");
3+
var red = document.querySelector("#red");
4+
var green = document.querySelector("#green");
5+
var blue = document.querySelector("#blue");
6+
var yellow = document.querySelector("#yellow");
7+
var pink = document.querySelector("#pink");
8+
var black = document.querySelector("#black");
9+
10+
var ctx = canvas.getContext("2d");
11+
var drawing = false;
12+
var oldX = 0;
13+
var oldY = 0;
14+
15+
red.addEventListener("click", () => {
16+
ctx.strokeStyle = "red";
17+
ctx.beginPath();
18+
})
19+
20+
green.addEventListener("click", () => {
21+
ctx.strokeStyle = "green";
22+
ctx.beginPath();
23+
})
24+
25+
blue.addEventListener("click", () => {
26+
ctx.strokeStyle = "blue";
27+
ctx.beginPath();
28+
})
29+
30+
yellow.addEventListener("click", () => {
31+
ctx.strokeStyle = "yellow";
32+
ctx.beginPath();
33+
})
34+
35+
pink.addEventListener("click", () => {
36+
ctx.strokeStyle = "pink";
37+
ctx.beginPath();
38+
})
39+
40+
black.addEventListener("click", () => {
41+
ctx.strokeStyle = "black";
42+
ctx.beginPath();
43+
})
44+
45+
canvas.style.cursor = "crosshair";
46+
canvas.height = window.innerHeight;
47+
canvas.width = window.innerWidth;
48+
canvas.addEventListener("mousedown", e => {
49+
drawing = true;
50+
oldX = e.offsetX;
51+
oldY = e.offsetY;
52+
});
53+
54+
canvas.addEventListener("mouseup", e => {
55+
drawing = false;
56+
});
57+
58+
canvas.addEventListener("mousemove", e => {
59+
if (!drawing){
60+
return false;
61+
}
62+
ctx.lineWidth = 5;
63+
ctx.lineCap = "round";
64+
ctx.moveTo(oldX, oldY);
65+
ctx.lineTo(e.offsetX, e.offsetY);
66+
ctx.stroke();
67+
oldX = e.offsetX;
68+
oldY = e.offsetY;
69+
});
70+
71+
clear.addEventListener ("click", () => {
72+
ctx.clearRect(0, 0, canvas.width, canvas.height);
73+
ctx.beginPath();
74+
})
75+
76+
77+

0 commit comments

Comments
 (0)