I’m currently working on a heatmap.js fix and I was wondering whether anyone knows if it’s possible to achive the following effect with <canvas>‘s 2d rendering context.
- I have a radial gradient from black (alpha 0.5) to transparent 40pixel radius. center of the radial gradient is at x=50, y=50
- I have another radial gradient from black (alpha 0.5) to transparent, 40pixel radius. center of the radial gradient is at x=80, y=50
The two gradients are overlapping. My problem now is: the overlapping area gets added up together resulting in a higher alpha value than the radial gradients centers and thus showing wrong data (e.g. hotter areas in a heatmap because of those additions between the gradients)
Have a look at the following gist, by executing it in your console you can see the problem.
Expected behaviour would be:
Darkest areas are the gradients centers, the overlapping area of the two gradients merges but doesn’t add up.
After seeing that none of the globalCompositeOperations resulted in the expected behaviour I tried combinations of those operations.
A way I thought it maybe would be possible was the following:
- draw first gradient
- use compositeOperation ‘destination-out’
- draw second gradient -> substracts overlapping area from the first gradient
- use compositeOperation ‘source-over’
- draw second gradient again
But unfortunately I didn’t find a combination that worked. I’d love to hear your feedback, thanks in advance!
PS: I know this could be done by manipulating the pixels manually, but I was wondering whether there’s an easier, more elegant and faster solution for that.
This is really wacky but it does what you want without getting imageData involved.
The thing that came to mind was that you want the exact functionality that paths themselves have on the canvas when you stroke them. To quote the spec:
You can read more about that here.
Anyway, what you’d want, essentially, is a blurry path of nothing but arcs that you can stroke once and you’d perfectly get the effect you were looking for.
The only problem is that there is no way to make a blurry path in canvas. Or almost no way.
Instead of using the path itself we can use the shadow of a path in order to simulate blurry circles that obey the same rules that paths do.
The only problem there, then, is that you don’t want to see the actual path, you just want to see the shadow of it. Making the stroke transparent won’t work: A shadow will only draw will not draw at a higher opacity than the thing it is shadowing.
But shadows do have the properties
shadowOffsetXandshadowOffsetY, which are typically used to shift the shadow by a pixel or two to make the illusion of a light source.But what if we draw the shadows so far away that you can’t see them? Or rather, what if we draw the paths so far away that you can’t see them, all you can see are the shadows?
Well that happens to do the trick. Here is a quick result, your original code is on the top and the shadows are the second canvas:
It’s not exactly what you had before in terms of gradients and size but its very close and I’m sure that by fiddling with the values you can get it even closer. A couple of console.log’s confirm that the thing we want, an alpha that does not go above 124 (out of 255) is correctly occurring in the places where it used to be 143 and 134 doing it the old way.
The fiddle to see the code in action: http://jsfiddle.net/g54Mz/
So there you have it. Getting the effect of the union of two radial gradients is possible without
imageDataif you use shadows and offset the actual paths so much that they are off the screen.