Skip to content

Commit 7090811

Browse files
added the canvas project
1 parent 01002b1 commit 7090811

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

medium/Canvas/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Hacktoberfest 2022 - JavaScript Easy Programs
2+
3+
## Canvas
4+
5+
### Description
6+
The Canvas API provides a means for drawing graphics via JavaScript and the HTML <canvas> element. Among other things, it can be used for animation, game graphics, data visualization, photo manipulation, and real-time video processing.
7+
In this project I've created the canvas on which anyone can draw something with their cursor. While drawing, the color and thickness of the pen changes automatically.
8+
9+
10+
11+
### Maintainer
12+
- [Anamaya](https://www.linkedin.com/in/anamaya1729/)
13+
- [Vaibhav](https://https://www.linkedin.com/in/vaibhava17/)
14+
15+
### License
16+
**This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../LICENSE) file for details**
17+
18+
### Happy Coding! :smile:

medium/Canvas/app.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const canvas = document.querySelector('#draw');
2+
const ctx = canvas.getContext('2d');
3+
canvas.width = window.innerWidth;
4+
canvas.height = window.innerHeight;
5+
ctx.strokeStyle = '#BADA55';
6+
ctx.lineJoin = 'round';
7+
ctx.lineCap = 'round';
8+
ctx.lineWidth = 100;
9+
// ctx.globalCompositeOperation = 'multiply';
10+
11+
let isDrawing = false;
12+
let lastX = 0;
13+
let lastY = 0;
14+
let hue = 0;
15+
let direction = true;
16+
17+
function draw(e) {
18+
if (!isDrawing) return; // stop the fn from running when they are not moused down
19+
console.log(e);
20+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
21+
ctx.beginPath();
22+
// start from
23+
ctx.moveTo(lastX, lastY);
24+
// go to
25+
ctx.lineTo(e.offsetX, e.offsetY);
26+
ctx.stroke();
27+
[lastX, lastY] = [e.offsetX, e.offsetY];
28+
29+
hue++;
30+
if (hue >= 360) {
31+
hue = 0;
32+
}
33+
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) {
34+
direction = !direction;
35+
}
36+
37+
if(direction) {
38+
ctx.lineWidth++;
39+
} else {
40+
ctx.lineWidth--;
41+
}
42+
43+
}
44+
45+
canvas.addEventListener('mousedown', (e) => {
46+
isDrawing = true;
47+
[lastX, lastY] = [e.offsetX, e.offsetY];
48+
});
49+
50+
51+
canvas.addEventListener('mousemove', draw);
52+
canvas.addEventListener('mouseup', () => isDrawing = false);
53+
canvas.addEventListener('mouseout', () => isDrawing = false);

medium/Canvas/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>HTML5 Canvas</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
9+
<body>
10+
11+
<canvas id="draw" width="800" height="800"></canvas>
12+
13+
<script src="app.js"></script>
14+
</body>
15+
</html>

medium/Canvas/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
html, body {
2+
margin: 0;
3+
}

0 commit comments

Comments
 (0)