views:

175

answers:

2

I've been following the lessons about transparency and gradients on mozillas site https://developer.mozilla.org/en/Canvas_tutorial/Applying_styles_and_colors but I have not been able to figure this one out.

I know I can achieve these effects with a png image; however, in the program I am working on the gradient will chance constantly according to where the image is moved.

Here's an example of the effect I'm looking for. http://home.insightbb.com/~epyonxl1/gradientex.jpg

A: 

If you need to make an image transparent set the ctx.globalAlpha to whatever you need (1, no transparency, is default). Then reset it after you draw your image. This URL probably will help as well https://developer.mozilla.org/en/Canvas_tutorial%3aCompositing.

qw3n
A: 

I've aded some code here http://code.google.com/p/canvasimagegradient/ that adds a drawImageGradient function to the CanvasRenderingContext2D. You can draw an image with a linear or radial gradient. It doesn't work in IE, even with excanvas, due to the lack of getImageData/putImageData support.

The following code for example will draw an image with a radial gradient (context retrieve and image load not shown):

var radGrad = ctx.createRadialGradient(
    img.width / 2, img.height / 2, 10, 
    img.width / 2, img.height / 2, img.width/2);
radGrad.addColorStop(0, "transparent");
radGrad.addColorStop(1, "#000");

ctx.drawImageGradient(img, 112.5, 130, radGrad);

The code works as follows:

  1. Create a canvas element dynamically and draw the image on it.
  2. Retrieve the imageData for this new canvas.
  3. Retrieve the imageData for the location on the canvas you want to draw the image on to.
  4. Iterate through the destination imageData and update each pixel adding together a percentage (derived from the gradient transparency value) of the image and destination pixel values.
  5. Finally put the updated image data back onto the destination canvas.

Obviously performance is an issue as images get larger. The image on http://code.google.com/p/canvasimagegradient/ it takes about 6-10ms to draw. A 1024x768 image takes about 100ms-250ms to draw. Still usable though as long as you're not animating.

Castrohenge
Ah, I knew after I forgot to specify I already know how to create a gradient such as this https://developer.mozilla.org/samples/canvas-tutorial/4_9_canvas_lineargradient.html that I would get this answer. Unfortunately these only apply to specific colors. If its an image(non-solid color) on the other hand this method simply does not work.
pcmike
I went back and took a look at the image and see what you want now. Good question. I'm thinking if you out the image in a seperate canvas and retrieve the image data you could apply a gradient affect to it and then overlay it over the original canvas. I'll have a play around and see what I can come up with.
Castrohenge
Sounds good, won't have to much time over the weekend to work on it myself but I'll see what I come up with as well. Keep me posted on anything you figure out also. I definitely want to solve this thing.
pcmike
I've got something working where an image can be overlayed on another image with semi-transparency. There's no gradient at the moment though, still working at it.
Castrohenge
I've finally got this working. I just need to tidy the code before I post it.
Castrohenge
Getting further than I am. I managed to get it work using the png image trick just to see if it would work but the furthest ive gotten doing it the *right* way is with just blending two images together no gradient-transparency.Before your next post I just wanted to go ahead and thank you in advance for taking the time to help me out with this, much respect.
pcmike
No problem. It's been fun just playing about. A good learning experience as well.
Castrohenge