CanvasRenderingContext2D .createRadialGradient() method of the Canvas 2D API creates a radial gradient using the size and coordinates of two circles.

此方法返回 CanvasGradient . To be applied to a shape, the gradient must first be assigned to the fillStyle or strokeStyle 特性。

注意: 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.

句法

CanvasGradient ctx.createRadialGradient(x0, y0, r0, x1, y1, r1);
					

createRadialGradient() method is specified by six parameters, three defining the gradient's start circle, and three defining the end circle.

参数

x0

The x-axis coordinate of the start circle.

y0

The y-axis coordinate of the start circle.

r0

The radius of the start circle. Must be non-negative and finite.

x1

The x-axis coordinate of the end circle.

y1

The y-axis coordinate of the end circle.

r1

The radius of the end circle. Must be non-negative and finite.

返回值

CanvasGradient
A radial CanvasGradient initialized with the two specified circles.

范例

Filling a rectangle with a radial gradient

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.

HTML

<canvas id="canvas" width="200" height="200"></canvas>
					

JavaScript

var canvas = document.getElementById('canvas');
var 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
var gradient = ctx.createRadialGradient(110,90,30, 100,100,70);
// Add three color stops
gradient.addColorStop(0, 'pink');
gradient.addColorStop(.9, 'white');
gradient.addColorStop(1, 'green');
// Set the fill style and draw a rectangle
ctx.fillStyle = gradient;
ctx.fillRect(20, 20, 160, 160);
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.createRadialGradient' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
createRadialGradient Chrome Yes Edge 12 Firefox 1.5 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

Gecko-specific notes

  • Starting with Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1), specifying non-finite values now throws NOT_SUPPORTED_ERR 而不是 SYNTAX_ERR .
  • Starting with Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), specifying a negative radius correctly throws INDEX_SIZE_ERR .

另请参阅

元数据

  • 最后修改: