In a HTML5 canvas, I can stroke a shape with a radial gradient, see https://jsfiddle.net/s03gz7wy/1/
var cnv = document.createElement("canvas"),
ctx = cnv.getContext("2d");
document.body.appendChild(cnv);
ctx.lineWidth = 10;
ctx.rect(20, 20, 100, 100);
var g = ctx.strokeStyle = ctx.createRadialGradient(70, 70, 0, 70, 70, 100);
g.addColorStop(0.4, "blue");
g.addColorStop(0.7, "red");
//ctx.translate(70,70); ctx.scale(0.5,1); ctx.translate(-70,-70);
ctx.stroke();
I would like the gradient to be "squeezed" like an ellipse, so I apply a transform to a canvas (line 10 - try to uncomment it). However, it also transforms the stroke (it is thinner at certain places).
Is there any way to "stroke with an elliptical gradient" in the 2D context? It is possible in SVG and PDF, but I can not do it on a canvas.
This is indeed a missing feature of the Canvas 2D API, the good news is that it's actively discussed that the same
setTransform()method thatCanvasPatternobjects have will be added to theCanvasGradientinterface (along with other missing gradient features, like defining the spread method, or the color-space used for interpolation, and maybe others). Given the latest discussions on this subject, I'd expect it to be one of the 2023 additions to the API.But for the time being, I'm afraid we're stuck with compositing.
One of the most flexible solutions is to use another canvas to do that compositing and then draw that canvas on the visible one. The steps could be:
globalCompositeOperationmode to"source-in".CanvasGradientas fill rule.(You could reorder this list by changing the gCO mode).