admin管理员组

文章数量:1323336

I have been experimenting with HTML5 and gradient fades, I would like to use 3 colours and gradually fade them into each other. Currently I can only get it working using two colours.

jsFiddle

My target is something similar to this image:

The colours should slowly fade into each other, i'm happy with the fade effect I currently have working, although would like to add a third colour.

I have tried to add an array of the colours to use, but the colour function was modded from a tutorial (will add link if I can find) and am not quite sure how to do it.

Could anyone offer any adivce?

Thanks in advance.

Am not trying to get it looking exactly the same as the image, but if I could just add another colour to my current version that should be fine.

Edit: Due to the confusion: I'm not looking for a "static" graident, if you view my example closely you will see a suttle fade effect. I want to achieve the same effect, but have the gradient with 3 colours and looking similar to the image above.

It should "shimmer" and a gradually fade. Offering a bounty as I'm struggling with creating this with 3 colours, which I think will be easy for most..

I have been experimenting with HTML5 and gradient fades, I would like to use 3 colours and gradually fade them into each other. Currently I can only get it working using two colours.

jsFiddle

My target is something similar to this image:

The colours should slowly fade into each other, i'm happy with the fade effect I currently have working, although would like to add a third colour.

I have tried to add an array of the colours to use, but the colour function was modded from a tutorial (will add link if I can find) and am not quite sure how to do it.

Could anyone offer any adivce?

Thanks in advance.

Am not trying to get it looking exactly the same as the image, but if I could just add another colour to my current version that should be fine.

Edit: Due to the confusion: I'm not looking for a "static" graident, if you view my example closely you will see a suttle fade effect. I want to achieve the same effect, but have the gradient with 3 colours and looking similar to the image above.

It should "shimmer" and a gradually fade. Offering a bounty as I'm struggling with creating this with 3 colours, which I think will be easy for most..

Share edited Oct 10, 2011 at 20:58 Elliott asked Oct 5, 2011 at 11:39 ElliottElliott 3,86424 gold badges71 silver badges93 bronze badges 7
  • 1 This might help. – Pointy Commented Oct 5, 2011 at 11:42
  • Wait ... you're doing this in a <canvas> and not with the HTML5/CSS3 "linear-gradient" background style ... is that what you prefer? – Pointy Commented Oct 5, 2011 at 11:44
  • I want it to work in as many browsers as possible, and with using ex-canvas I was hoping to add support for IE. Is it possible to do a similar effect in HTML5/CSS3 with fading? – Elliott Commented Oct 5, 2011 at 11:46
  • Well check that gradient generator site - it's got some provisions for IE, and it gives you what appears to be pretty portable CSS. (edit actually for older IE browsers it looks like it can't really do it, with only the old transform "filter" properties.) – Pointy Commented Oct 5, 2011 at 11:47
  • @c69 The question is how to make those colours fade into each other? – Elliott Commented Oct 5, 2011 at 11:59
 |  Show 2 more ments

2 Answers 2

Reset to default 10 +350

Unless you need to do some plex animation, you were doing some extremely unnecessary calculations for your gradient. The point of the gradient is that you give it some seed values and it generates everything in between automatically.

I worked up a sample gradient that looks similar to your reference image: http://jsfiddle/ZFayC/2/

Note that it looks like your reference image might have a small amount of texture to it, and that the gradient definitely isn't linear. If you want to re-create the reference image, you'll have to use some radial gradients and possibly overlay a texture.

Also, note that you were setting the canvas width and height via CSS properties. With the canvas element, CSS width and height control the magnification of the element, while DOM-level attributes control the actual dimensions.


Edit: I pletely missed the fact that you were looking to produce an animated gradient. My apologies for that.

I went back and edited my example to smoothly transition between three pre-defined colors similar to the ones in your reference image. You can view the updated example here:

http://jsfiddle/fkU4Q/

Is this more along the lines of what you're looking for?


Edit: Here's another update that adds full-screen support, a diagonal gradient, and a secondary radial gradient that is overlaid in the middle to help give some variety:

http://jsfiddle/fkU4Q/2/

You might want to customize the colors a bit more to increase the variation, but hopefully the functionality is there now.

I think by applying certain tricks it should not be a big deal using CSS3 properties here is the playground,

http://css3.mikeplate./

本文标签: javascriptltcanvasgt gradient fade with 3 ColoursStack Overflow