{"id":3080,"date":"2026-06-26T06:37:05","date_gmt":"2026-06-25T22:37:05","guid":{"rendered":"http:\/\/www.ofaruque.com\/blog\/?p=3080"},"modified":"2026-06-26T06:37:05","modified_gmt":"2026-06-25T22:37:05","slug":"how-to-draw-a-rectangle-with-rounded-corners-on-a-canvas-4aca-196691","status":"publish","type":"post","link":"http:\/\/www.ofaruque.com\/blog\/2026\/06\/26\/how-to-draw-a-rectangle-with-rounded-corners-on-a-canvas-4aca-196691\/","title":{"rendered":"How to draw a rectangle with rounded corners on a Canvas?"},"content":{"rendered":"<p>Hey there! I&#8217;m from a Canvas supplier, and today I wanna share with you how to draw a rectangle with rounded corners on a Canvas. It&#8217;s a pretty cool skill that can add a touch of elegance to your designs. Whether you&#8217;re a graphic designer, a web developer, or just someone who loves to create, this guide will walk you through the process step by step. <a href=\"https:\/\/www.shengruntextile.com\/fabric\/canvas\/\">Canvas<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.shengruntextile.com\/uploads\/202017142\/small\/cozy-rural-style-embroidery-tablecloth-table13233539605.jpg\"><\/p>\n<h3>Getting Started with Canvas<\/h3>\n<p>First things first, let&#8217;s talk a bit about Canvas. Canvas is a super handy HTML element that allows you to draw graphics on the fly using JavaScript. It&#8217;s like a blank canvas (pun intended) where you can bring your ideas to life. If you&#8217;re new to Canvas, don&#8217;t worry! It&#8217;s not as complicated as it sounds.<\/p>\n<p>To start using Canvas, you need to create a <code>&lt;canvas&gt;<\/code> element in your HTML file. Here&#8217;s a simple example:<\/p>\n<pre><code class=\"language-html\">&lt;canvas id=&quot;myCanvas&quot; width=&quot;400&quot; height=&quot;200&quot;&gt;&lt;\/canvas&gt;\n<\/code><\/pre>\n<p>This code creates a Canvas element with an ID of <code>myCanvas<\/code> and a width of 400 pixels and a height of 200 pixels. You can adjust these values to fit your needs.<\/p>\n<p>Next, you need to get a reference to the Canvas element in your JavaScript code. You can do this using the <code>getElementById<\/code> method:<\/p>\n<pre><code class=\"language-javascript\">const canvas = document.getElementById('myCanvas');\nconst ctx = canvas.getContext('2d');\n<\/code><\/pre>\n<p>The <code>getContext('2d')<\/code> method returns a 2D rendering context, which is what you&#8217;ll use to draw on the Canvas.<\/p>\n<h3>Drawing a Rectangle with Rounded Corners<\/h3>\n<p>Now that you have your Canvas set up, let&#8217;s get to the fun part: drawing a rectangle with rounded corners. The basic idea is to draw four arcs for the corners and four straight lines for the sides.<\/p>\n<p>Here&#8217;s the code to draw a rounded rectangle:<\/p>\n<pre><code class=\"language-javascript\">function drawRoundedRectangle(ctx, x, y, width, height, radius) {\n    ctx.beginPath();\n    ctx.moveTo(x + radius, y);\n    ctx.lineTo(x + width - radius, y);\n    ctx.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2);\n    ctx.lineTo(x + width, y + height - radius);\n    ctx.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5);\n    ctx.lineTo(x + radius, y + height);\n    ctx.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI);\n    ctx.lineTo(x, y + radius);\n    ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);\n    ctx.closePath();\n    ctx.stroke();\n}\n\n\/\/ Usage\ndrawRoundedRectangle(ctx, 50, 50, 300, 100, 20);\n<\/code><\/pre>\n<p>Let&#8217;s break down what&#8217;s happening in this code:<\/p>\n<ol>\n<li><code>ctx.beginPath()<\/code>: This method starts a new path. A path is a series of connected lines and shapes.<\/li>\n<li><code>ctx.moveTo(x + radius, y)<\/code>: This method moves the starting point of the path to the specified coordinates. In this case, we&#8217;re moving to the top-left corner of the rectangle, offset by the radius.<\/li>\n<li><code>ctx.lineTo(x + width - radius, y)<\/code>: This method draws a straight line from the current point to the specified coordinates. Here, we&#8217;re drawing the top side of the rectangle.<\/li>\n<li><code>ctx.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2)<\/code>: This method draws an arc. The first two parameters are the center of the arc, the third parameter is the radius, and the last two parameters are the starting and ending angles in radians. We&#8217;re using this method to draw the top-right corner of the rectangle.<\/li>\n<li>The remaining lines of code follow a similar pattern, drawing the remaining sides and corners of the rectangle.<\/li>\n<li><code>ctx.closePath()<\/code>: This method closes the path by drawing a line from the current point back to the starting point.<\/li>\n<li><code>ctx.stroke()<\/code>: This method strokes the path with the current stroke style.<\/li>\n<\/ol>\n<h3>Customizing Your Rounded Rectangle<\/h3>\n<p>Now that you know how to draw a basic rounded rectangle, you can customize it to fit your needs. Here are a few ways you can do that:<\/p>\n<h4>Changing the Color<\/h4>\n<p>You can change the color of the rectangle by setting the <code>strokeStyle<\/code> property of the rendering context. For example:<\/p>\n<pre><code class=\"language-javascript\">ctx.strokeStyle = 'blue';\ndrawRoundedRectangle(ctx, 50, 50, 300, 100, 20);\n<\/code><\/pre>\n<p>This code will draw a blue rounded rectangle.<\/p>\n<h4>Filling the Rectangle<\/h4>\n<p>You can also fill the rectangle with a color by using the <code>fill<\/code> method instead of the <code>stroke<\/code> method. For example:<\/p>\n<pre><code class=\"language-javascript\">function drawRoundedRectangle(ctx, x, y, width, height, radius) {\n    ctx.beginPath();\n    ctx.moveTo(x + radius, y);\n    ctx.lineTo(x + width - radius, y);\n    ctx.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2);\n    ctx.lineTo(x + width, y + height - radius);\n    ctx.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5);\n    ctx.lineTo(x + radius, y + height);\n    ctx.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI);\n    ctx.lineTo(x, y + radius);\n    ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);\n    ctx.closePath();\n    ctx.fillStyle = 'green';\n    ctx.fill();\n}\n\n\/\/ Usage\ndrawRoundedRectangle(ctx, 50, 50, 300, 100, 20);\n<\/code><\/pre>\n<p>This code will draw a green filled rounded rectangle.<\/p>\n<h4>Changing the Line Width<\/h4>\n<p>You can change the width of the lines by setting the <code>lineWidth<\/code> property of the rendering context. For example:<\/p>\n<pre><code class=\"language-javascript\">ctx.lineWidth = 5;\ndrawRoundedRectangle(ctx, 50, 50, 300, 100, 20);\n<\/code><\/pre>\n<p>This code will draw a rounded rectangle with a line width of 5 pixels.<\/p>\n<h3>Why Choose Our Canvas?<\/h3>\n<p>As a Canvas supplier, we offer high-quality Canvas products that are perfect for all your drawing needs. Our Canvas is made from durable materials that can withstand repeated use and provide a smooth surface for your designs.<\/p>\n<p>Here are some of the benefits of choosing our Canvas:<\/p>\n<ul>\n<li><strong>High Quality<\/strong>: Our Canvas is made from top-quality materials that ensure a long-lasting and professional-looking finish.<\/li>\n<li><strong>Versatile<\/strong>: Our Canvas can be used for a variety of applications, including graphic design, web development, and art projects.<\/li>\n<li><strong>Easy to Use<\/strong>: Our Canvas is easy to integrate into your existing projects and comes with detailed documentation and support.<\/li>\n<\/ul>\n<p>If you&#8217;re interested in learning more about our Canvas products or have any questions, please don&#8217;t hesitate to contact us. We&#8217;d love to hear from you and help you find the perfect Canvas for your needs.<\/p>\n<h3>Conclusion<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.shengruntextile.com\/uploads\/202017142\/small\/white-table-cloth-with-embroidery-lace26234638370.jpg\"><\/p>\n<p>Drawing a rectangle with rounded corners on a Canvas is a fun and useful skill that can add a touch of elegance to your designs. By following the steps outlined in this guide, you can easily create your own rounded rectangles and customize them to fit your needs.<\/p>\n<p><a href=\"https:\/\/www.shengruntextile.com\/fabric\/wax-print-fabric\/\">Wax Print Fabric<\/a> Remember, our Canvas products are here to help you bring your ideas to life. If you&#8217;re looking for a high-quality Canvas that&#8217;s easy to use and versatile, look no further. Contact us today to learn more about our products and start creating!<\/p>\n<h3>References<\/h3>\n<ul>\n<li>HTML Canvas API documentation<\/li>\n<li>JavaScript reference guides<\/li>\n<\/ul>\n<hr>\n<p><a href=\"https:\/\/www.shengruntextile.com\/\">Shandong Shengrun Textile Co., Ltd.<\/a><br \/>With over 15 years of experience, Shandong Shengrun Textile Co., Ltd. is one of the most professional canvas manufacturers and suppliers in China. Please rest assured to buy or wholesale durable canvas in stock here from our factory.<br \/>Address: 9th Floor, Hui Ji Business Tower, Ren Cheng District, Ji Ning, Shan Dong, China<br \/>E-mail: liang@shengrungroup.com<br \/>WebSite: <a href=\"https:\/\/www.shengruntextile.com\/\">https:\/\/www.shengruntextile.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there! I&#8217;m from a Canvas supplier, and today I wanna share with you how to &hellip; <a title=\"How to draw a rectangle with rounded corners on a Canvas?\" class=\"hm-read-more\" href=\"http:\/\/www.ofaruque.com\/blog\/2026\/06\/26\/how-to-draw-a-rectangle-with-rounded-corners-on-a-canvas-4aca-196691\/\"><span class=\"screen-reader-text\">How to draw a rectangle with rounded corners on a Canvas?<\/span>Read more<\/a><\/p>\n","protected":false},"author":528,"featured_media":3080,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3043],"class_list":["post-3080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-canvas-41ea-199983"],"_links":{"self":[{"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/posts\/3080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/users\/528"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/comments?post=3080"}],"version-history":[{"count":0,"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/posts\/3080\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/posts\/3080"}],"wp:attachment":[{"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/media?parent=3080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/categories?post=3080"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ofaruque.com\/blog\/wp-json\/wp\/v2\/tags?post=3080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}