Write a program to demonstrate the Delayed Circle Drawing Using JavaScript.

1
2
    <!DOCTYPE html>
3
    <html lang="en">
4
        
5
    <head>
6
        <meta charset="UTF-8">
7
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
        <title>Canvas Circle Drawer</title>
9
        <style>
10
            #myCanvas {
11
                border: 1px solid #d3d3d3;
12
            }
13
        </style>
14
    </head>
15
        
16
    <body>
17
        <h3>Draw a Circle on Canvas</h3>
18
        <button onclick="startDrawing()">Draw Circle After 3 Seconds</button>
19
        
20
        <canvas id="myCanvas" width="300" height="150">
21
            Your browser does not support the HTML5 canvas tag.
22
        </canvas>
23
        
24
        <script>
25
            function startDrawing() {
26
                // Provide user feedback
27
                alert('Drawing will start in 3 seconds...');
28
                
29
                // Set timeout to draw the circle after 3 seconds (3000 ms)
30
                setTimeout(drawCircle, 3000);
31
            }
32
        
33
            function drawCircle() {
34
                // Get the canvas element and context
35
                var canvas = document.getElementById("myCanvas");
36
                var ctx = canvas.getContext("2d");
37
        
38
                // Clear the canvas before drawing (in case it has existing content)
39
                ctx.clearRect(0, 0, canvas.width, canvas.height);
40
        
41
                // Set styles for the circle
42
                ctx.lineWidth = 3;
43
                ctx.strokeStyle = 'blue';
44
        
45
                // Begin drawing the circle
46
                ctx.beginPath();
47
                ctx.arc(150, 75, 50, 0, 2 * Math.PI);
48
                ctx.stroke();
49
        
50
                // Display an alert once the circle is drawn
51
                alert('Circle drawn!');
52
            }
53
        </script>
54
    </body>
55
        
56
    </html>
57
58
59
    

Output

1
2
        No Output Available
3
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

Core Concept of Delayed Circle Drawing Using JavaScript

In this code, the <canvas> element of HTML 5 and the setTimeout method of JavaScript are used.

Working of this Program

  • When the user clicks on the “Draw Circle After 3 Seconds” button, an alert is shown, which triggers the setTimeout(drawCircle, 3000) method.
  • After 3 Seconds (3000 milliseconds), the drawCircle() method is executed.
  • Then the drawCircle method uses the canvas context and draws the circle.

Why is it used?

  • To control the graphics, shapes, and animations, the Canvas API is used.
  • With the help of setTimeout, any code can be executed asynchronously.

Real-life Applications

  • Interactive games: With the help of canvas and shapes, 2D & 3D games like snake game, physics sandbox, etc. are built, and according to the time delay, the various objects or characters are rendered.
  • Data Visualization & Charts: To draw animated charts like pie chart, bar charts etc. are rendered with the help of this canvas.
  • Drawing tools and whiteboards: To draw shapes in Paint or any design web app like Canva.

Benefits of this Program

  • High-Performance Graphics: Uses the canvas shapes the animation perform better than div or span elements.
  • Precise Control: With the help of (x, y, radius, angles), the shapes can be drawn precisely.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.