Skip to content

Commit c6d7ee8

Browse files
committed
Error handling nad canvas
1 parent 4063798 commit c6d7ee8

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

assets/pic_the_scream.jpeg

29.3 KB
Loading

canvas.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<canvas id="myCanvas" width="400px" height="300px"></canvas>"
10+
<!-- /// -->
11+
12+
<p>Image to use:</p>
13+
14+
<img
15+
id="scream"
16+
width="220"
17+
height="277"
18+
src="/assets/pic_the_scream.jpeg"
19+
alt="The Scream"
20+
/>
21+
22+
<p>Canvas:</p>
23+
24+
<canvas
25+
id="myCanvas2"
26+
width="240"
27+
height="297"
28+
style="border: 1px solid grey"
29+
></canvas>
30+
<!-- //// -->
31+
<canvas
32+
id="myCanvas3"
33+
width="200"
34+
height="120"
35+
style="border: 1px solid #d3d3d3"
36+
></canvas>
37+
38+
<!-- /// -->
39+
<script src="scripts/canvas.js"></script>
40+
</body>
41+
</html>

errorHandling.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<title>Document</title>
77
</head>
88
<body>
9+
<p>Enter a number between 5-10:</p>
10+
<input type="text" name="" id="numTextFied" />
11+
<button id="checkBtn">Check</button>
12+
913
<script src="/scripts/errorHandling.js"></script>
1014
</body>
1115
</html>

scripts/canvas.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const canva = document.querySelector('#myCanvas');
2+
var canvaTxt = canva.getContext('2d');
3+
4+
canvaTxt.strokeStyle = 'black';
5+
canvaTxt.strokeRect(10, 10, 382, 282);
6+
7+
canvaTxt.lineWidth = 3;
8+
canvaTxt.fillStyle = 'green';
9+
canvaTxt.fillRect(12, 12, 378, 278);
10+
11+
var centerX = canva.width / 2;
12+
var centerY = canva.height / 2;
13+
14+
canvaTxt.beginPath();
15+
canvaTxt.arc(centerX, centerY, 80, 0, 2 * Math.PI, false);
16+
canvaTxt.fillStyle = 'red';
17+
canvaTxt.fill();
18+
canvaTxt.stroke();
19+
//
20+
21+
const canvas = document.querySelector('#myCanvas2');
22+
const ctx = canvas.getContext('2d');
23+
const img = document.querySelector('#scream');
24+
ctx.drawImage(img, 10, 10);
25+
26+
///
27+
const canvas2 = document.querySelector('#myCanvas3');
28+
const ctx2 = canvas2.getContext('2d');
29+
ctx2.beginPath();
30+
ctx2.arc(100, 50, 50, 0, Math.PI);
31+
ctx2.stroke();

scripts/errorHandling.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,38 @@
22
//try ... catch handle run time errors(program which is)
33
//Error object
44
//The FInally statement -> execute code, after try and catch
5+
//The throw statement -> create custom errors.
6+
7+
console.clear();
8+
// try {
9+
// //code
10+
// alert('Hi everyone');
11+
// alert(x);
12+
// } catch (err) {
13+
// //handle error
14+
// alert('Inside Catch block');
15+
// console.log(err);
16+
// console.log(err.name);
17+
// console.log(err.message);
18+
// } finally {
19+
// alert('Bye Everyone');
20+
// }
21+
22+
// alert('Hi everyone');
23+
// alert(x);
24+
// alert('Bye Everyone');
25+
26+
document.querySelector('#checkBtn').addEventListener('click', function () {
27+
// alert('button is clicked');
28+
let num = document.querySelector('#numTextFied').value;
29+
console.log(num);
30+
try {
31+
if (num < 5) {
32+
throw 'Input is too low';
33+
} else if (num > 10) {
34+
throw 'Input is too high';
35+
}
36+
} catch (err) {
37+
console.log(err);
38+
}
39+
});

0 commit comments

Comments
 (0)