Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
HTML Canvas Rectangles
Next article icon

HTML Canvas Shapes

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

HTML Canvas Shapes facilitate different shapes, i.e., rectangles, triangles, lines, arcs, and curves to draw on the HTML Canvas. 

For instance, to draw the line in the Canvas, the path will be used that implements the beginPath() method. Setting the path's starting point is achieved with moveTo(), while lineTo() establishes the endpoint. To render the line, we use the stroke() method, with the default stroke color being black.

Syntax

context.beginPath();  context.moveTo();  context.lineTo();  context.stroke();

Methods

  • beginPath(): This method is used to define the new path.
  • moveTo(x,y): This method defines the starting point.
  • lineTo(x,y): This method defines the endpoint. Here, x specifies the x-axis coordinates and y specifies the y-axis coordinates of the line's endpoint.
  • stroke(): This method is used to draw the line. The stroke color is black which is a default property.

Example: In this example, we will implement a code to draw canvas shapes with default property.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"            content="width=device-width,                    initial-scale=1.0">     <title>HTML Canvas Shapes</title>     <style>         @import url( 'https://fonts.googleapis.com/css2?family=Poppins&display=swap');          .text-logo {             font-size: 40px;             color: green;         }          .canvas-element {             border: 1px solid black;         }          body {             font-family: 'Poppins', sans-serif;         }          body {             font-family: 'Poppins', sans-serif;             display: flex;             justify-content: center;             align-items: center;             height: 100vh;             display: flex;             justify-content: center;             align-items: center;          }          .main {             background-color: #fff;             border-radius: 15px;             box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);             padding: 20px;             transition: transform 0.2s;             width: 600px;             display: flex;             justify-content: center;             align-items: center;             flex-direction: column;         }          .main:hover {             transform: scale(1.05);         }     </style> </head>  <body>     <div class="main">         <p class="text-logo">             GeeksforGeeks         </p>         <h1>             HTML Canvas Shapes         </h1>         <canvas id="canvas-id"                  width="200"                  height="200"                  class="canvas-element">         </canvas>     </div>      <script>         const element =               document.getElementById("canvas-id").getContext("2d")          // Define a new path         element.beginPath();          // Start point         element.moveTo(100, 10);         element.lineTo(180, 10);         element.lineTo(180, 150);         element.lineTo(20, 150);         element.lineTo(100, 100);         element.lineTo(100, 10);          // Draw the lines         element.stroke();     </script> </body>  </html> 

Output:

shapesi
 

Example: In this example, we will implement a code to draw canvas shapes with the strokeStyle property.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"            content="width=device-width,                    initial-scale=1.0">     <title>HTML Canvas Shapes</title>     <style>         @import url( 'https://fonts.googleapis.com/css2?family=Poppins&display=swap');          .text-logo {             font-size: 40px;             color: green;         }          .canvas-element {             border: 1px solid black;         }          body {              font-family: 'Poppins', sans-serif;         }          body {             font-family: 'Poppins', sans-serif;             display: flex;             justify-content: center;             align-items: center;             height: 100vh;             display: flex;             justify-content: center;             align-items: center;          }          .main {             background-color: #fff;             border-radius: 15px;             box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);             padding: 20px;             transition: transform 0.2s;             width: 600px;             display: flex;             justify-content: center;             align-items: center;             flex-direction: column;         }          .main:hover {             transform: scale(1.05);         }     </style> </head>  <body>     <div class="main">         <p class="text-logo">             GeeksforGeeks         </p>         <h1>             HTML Canvas Shapes         </h1>         <canvas id="canvas-id"                 width="200"                  height="200"                  class="canvas-element">         </canvas>     </div>      <script>         const element =                document.getElementById("canvas-id").getContext("2d")          // Define a new path         element.beginPath();          // Start point         element.moveTo(100, 10);         element.lineTo(180, 10);         element.lineTo(180, 150);         element.lineTo(20, 150);         element.lineTo(100, 100);         element.lineTo(100, 10);          element.strokeStyle = "blueviolet";          // Draw the lines         element.stroke();     </script> </body>  </html> 

Output:

Screenshot-2023-11-09-125048
 

Next Article
HTML Canvas Rectangles

S

shivanigupta18rk
Improve
Article Tags :
  • HTML
  • HTML5
  • HTML-Canvas

Similar Reads

    HTML Canvas Tutorial
    HTML Canvas facilitates the element <canvas> to draw graphics on Canvas with the help of JavaScript. HTML Canvas offers various methods for drawing different shapes and lines. The HTML Canvas is a rectangular area defined via height and width on an HTML page. By default, HTML Canvas has no bor
    3 min read
    HTML Canvas Basics
    In this article, we will know HTML Canvas Basics, and their implementation through the examples. The HTML "canvas" element is used to draw graphics via JavaScript. The "canvas" element is only a container for graphics. One must use JavaScript to actually draw the graphics. Canvas has several methods
    5 min read
    HTML Canvas Drawing
    HTML Canvas Drawing facilitates the <canvas> element, along with Properties that help to draw on the HTML canvas. The various properties, attributes & events can be used with <canvas> element. Utilizing JavaScript, we can manipulate the canvas element to draw shapes, paths, and image
    2 min read
    HTML Canvas Coordinates
    In this article, we will see the concept of coordinates in HTML Canvas Coordinates. In HTML canvas, the coordinates are very important because, with the understanding of these coordinates, we can easily draw any shapes or lines on the canvas. x-axis coordinates define the coordinates in horizontal a
    2 min read
    HTML Canvas Lines
    In this article, we will learn how to make lines in different styles on Canvas in HTML. There are various methods used to draw a line on canvas like beginPath(), moveTo(), lineTo(), and stroke(). There are also various properties like lineWidth, strokeStyle, etc. can be used to give styles to a line
    3 min read
    HTML Canvas Shapes
    HTML Canvas Shapes facilitate different shapes, i.e., rectangles, triangles, lines, arcs, and curves to draw on the HTML Canvas.  For instance, to draw the line in the Canvas, the path will be used that implements the beginPath() method. Setting the path's starting point is achieved with moveTo(), w
    3 min read
    HTML Canvas Rectangles
    The HTML Canvas Rectangles facilitate the rect() method to draw rectangles on canvas. There are various attributes in the rect(x, y, width, height) method such as x and y defining the coordinates of the upper-left corner of the rectangle, width defining the width of the rectangle, and height definin
    4 min read
    HTML Canvas Circles
    HTML Canvas Circles facilitates the arc() method to make a circle where 0 is defined as the start angle and the end angle is 2*PI. The stroke() method is used to draw an outline of the circle and fill() is used to draw a filled circle we can give color with the fillStyle property. stroke() Method Cr
    2 min read
    HTML Canvas Curves
    HTML Canvas Curves facilitate the arc() method for making a circle or a part of a circle on the canvas. There are various attributes in the arc(x, y, r, s, e) method such as x and y defining the coordinates of the center of the circle, r defining the radius, s defining the start angle, and e definin
    2 min read
    HTML Canvas Gradients
    HTML Canvas Gradients is used to give a gradient effect on Canvas with the help of JavaScript. The different shapes, such as rectangles, circles, lines, text, etc, can be filled with Gradients. To create a gradient on Canvas, we can use two techniques, Linear Gradient and Radial Gradient. Linear Gra
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences