Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6381045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:20:45+00:00 2026-05-25T02:20:45+00:00

How can I draw a rectangle with a gradient effect like the one pictured

  • 0

How can I draw a rectangle with a gradient effect like the one pictured below using the HTML5 canvas element?

sample

Edit: Thanks for all the feedback. Yes, I have already tried many methods. For example, can I use the createRadialGradient method as @Loktar suggests? Here is some sample code:

<html>
  <head>
    <title>test</title>
      <script type="application/x-javascript">
        function draw() {
          var canvas = document.getElementById("canvas"),
          ctx = canvas.getContext("2d");

          var grad1 = ctx.createRadialGradient(50, 50, 0, 50, 50, 50);
          grad1.addColorStop(0, 'rgba(255, 252, 0, 1)');
          grad1.addColorStop(1, 'rgba(68, 205, 37, 1)');

          ctx.fillStyle = grad1;
          ctx.fillRect(0, 0, 100, 100);
       }
    </script>
  </head>
  <body onload="draw();">
    <div>
      <canvas id="canvas" width="100" height="100"></canvas>
    </div>
  </body>
</html>

But the result is not quite what I want:

result

This should be done easily with a method like PathGradientBrush provided by GDI+.
I’m not sure is it possible with the HTML5 canvas element.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:20:46+00:00Added an answer on May 25, 2026 at 2:20 am

    You could try to use a combination of linear gradient and clipping. Demo. Code:

    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    
    var outerColor = 'rgba(68,205,37,1)';
    var innerColor = 'rgba(255,252,0,1)';
    
    var w = 200;
    var h = 50;
    canvas.width = w;
    canvas.height = h;
    
    function gradient(dir) {
        var grad = ctx.createLinearGradient(dir[0], dir[1], dir[2], dir[3]);
    
        grad.addColorStop(0, outerColor);
        grad.addColorStop(0.5, innerColor);
        grad.addColorStop(1.0, outerColor);
    
        return grad;
    }
    
    // idea: render background gradient and a clipped "bow"
    function background() {
        ctx.fillStyle = gradient([0, 0, 0, h]);
        ctx.fillRect(0, 0, w, h);
    }
    
    function bow() {
        ctx.save();
    
        ctx.beginPath();
        ctx.moveTo(0, 0);
        ctx.lineTo(w, h);
        ctx.lineTo(w, 0);
        ctx.lineTo(0, h);
        ctx.clip();
    
        ctx.fillStyle = gradient([0, 0, w, 0]);
        ctx.fillRect(0, 0, w, h);
    
        ctx.restore();
    }
    
    background();
    bow();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I can draw rectangle in canvas using a mousedown and mousemove events I
I can make a obj to use the canvas to draw like this: MyObj.myDiv
I want to draw a rectangle using canvas which change its size with different
How can I draw a filled rectangle in an applet with half opacity/transparency?
I can draw many things using this : NSString *imagePath = [[NSBundle mainBundle] pathForResource:@dummy2.png
Any idea how I can draw Bullet Points using Flex? instead of using image
the pixman image library can draw radial color gradients between two circles. I'd like
I had made one module in my project on which user can draw anything
Can I draw rectangle with text in EditText? The text in rectangle should be
Draw rectangle using C# and I need to draw the arc in every edges

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.