HTML - Canvas draw a gradient box

Hi..
I have code that places an X  (draws an X) based on x,y coordinates.  How can I do the same but fill the area with a gradient box instead.
Here's the 'X' code.
thx

   var ctx = canvas.getContext('2d');
          ctx.moveTo(x1, y1);
          ctx.lineTo(x2 + 1, y2 + 1);
          ctx.lineWidth = 4;
          ctx.strokeStyle = 'green';
          ctx.stroke();

          ctx.beginPath();
          ctx.moveTo(x2, y1);
          ctx.lineTo(x1 + 1, y2 + 1);
          ctx.lineWidth = 4;
          ctx.strokeStyle = 'red';
          ctx.stroke();
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
Please try this:
          var ctx = canvas.getContext('2d');
          ctx.moveTo(x1, y1);
          ctx.lineTo(x2 + 1, y2 + 1);
          ctx.lineWidth = 4;
          //ctx.strokeStyle = 'green';
          var grd = ctx.createLinearGradient(x1, y1, x2, y2);
          grd.addColorStop(0, "#FF0000");
          grd.addColorStop(0.5, "#00FF00");
          grd.addColorStop(1, "#0000FF");
          ctx.strokeStyle = grd;
          ctx.stroke();

          ctx.beginPath();
          ctx.moveTo(x2, y1);
          ctx.lineTo(x1 + 1, y2 + 1);
          ctx.lineWidth = 4;
          //ctx.strokeStyle = 'red';
          var grd2 = ctx.createLinearGradient(x1, y2, x2, y1);
          grd2.addColorStop(0, "#FF0000");
          grd2.addColorStop(0.5, "#00FF00");
          grd2.addColorStop(1, "#0000FF");
          ctx.strokeStyle = grd2;
          ctx.stroke();

Open in new window

0
JElsterAuthor Commented:
How can I fill in the whole area in gradient.
1 big gradient box.
thx!
0
Robert SchuttSoftware EngineerCommented:
You can use the gradient as strokeStyle or fillStyle just like using a color, for example:
          ctx.rect(0, 0, canvas.width, canvas.height);
          var grd = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
          grd.addColorStop(0, "#FF0000");
          grd.addColorStop(0.5, "#00FF00");
          grd.addColorStop(1, "#0000FF");
          ctx.fillStyle = grd;
          ctx.fill();

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JElsterAuthor Commented:
Sorry I mean the area of the X & Y
thx again
0
Robert SchuttSoftware EngineerCommented:
Ok, perhaps try this then:
          ctx.beginPath();
          ctx.rect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
          var grd4 = ctx.createLinearGradient(x1, y1, x2, y2);
          grd4.addColorStop(0, "#FF0000");
          grd4.addColorStop(0.5, "#00FF00");
          grd4.addColorStop(1, "#0000FF");
          ctx.fillStyle = grd4;
          ctx.fill();

Open in new window

Note the difference between using corners for the gradient and top/left, width/height for rect.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JElsterAuthor Commented:
thx again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.