This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The CanvasRenderingContext2D.createRadialGradient() method of the Canvas 2D API creates a radial gradient using the size and coordinates of two circles.
This method returns a CanvasGradient. To be applied to a shape, the gradient must first be assigned to the fillStyle or strokeStyle properties.
Note: Gradient coordinates are global, i.e., relative to the current coordinate space. When applied to a shape, the coordinates are NOT relative to the shape's coordinates.
createRadialGradient(x0, y0, r0, x1, y1, r1)
The createRadialGradient() method is specified by six parameters, three defining the gradient's start circle, and three defining the end circle.
x0The x-axis coordinate of the start circle.
y0The y-axis coordinate of the start circle.
r0The radius of the start circle. Must be non-negative and finite.
x1The x-axis coordinate of the end circle.
y1The y-axis coordinate of the end circle.
r1The radius of the end circle. Must be non-negative and finite.
A radial CanvasGradient initialized with the two specified circles.
NotSupportedError DOMException
Thrown when non-finite values are passed in parameter.
IndexSizeError DOMException
Thrown when a negative radius is passed in parameter.
This example initializes a radial gradient using the createRadialGradient() method. Three color stops between the gradient's two circles are then created. Finally, the gradient is assigned to the canvas context, and is rendered to a filled rectangle.
<canvas id="canvas" width="200" height="200"></canvas>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// Create a radial gradient
// The inner circle is at x=110, y=90, with radius=30
// The outer circle is at x=100, y=100, with radius=70
const gradient = ctx.createRadialGradient(110, 90, 30, 100, 100, 70);
// Add three color stops
gradient.addColorStop(0, "pink");
gradient.addColorStop(0.9, "white");
gradient.addColorStop(1, "green");
// Set the fill style and draw a rectangle
ctx.fillStyle = gradient;
ctx.fillRect(20, 20, 160, 160);
| Specification |
|---|
| HTML> # dom-context-2d-createradialgradient-dev> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
createRadialGradient |
1 | 12 | 1.5 | ≤12.1 | 2 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
CanvasRenderingContext2D
CanvasRenderingContext2D.createLinearGradient()CanvasRenderingContext2D.createConicGradient()
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createRadialGradient