CanvasRenderingContext2D
.createLinearGradient()
method of the Canvas 2D API creates a gradient along the line connecting two given coordinates.
This method returns a linear
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.createLinearGradient(x0, y0, x1, y1);
createLinearGradient()
method is specified by four parameters defining the start and end points of the gradient line.
x0
The x-axis coordinate of the start point.
y0
The y-axis coordinate of the start point.
x1
The x-axis coordinate of the end point.
y1
The y-axis coordinate of the end point.
CanvasGradient
CanvasGradient
initialized with the specified line.
This example initializes a linear gradient using the
createLinearGradient()
method. Three color stops between the gradient's start and end points are then created. Finally, the gradient is assigned to the canvas context, and is rendered to a filled rectangle.
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// Create a linear gradient
// The start gradient point is at x=20, y=0
// The end gradient point is at x=220, y=0
var gradient = ctx.createLinearGradient(20,0, 220,0);
// Add three color stops
gradient.addColorStop(0, 'green');
gradient.addColorStop(.5, 'cyan');
gradient.addColorStop(1, 'green');
// Set the fill style and draw a rectangle
ctx.fillStyle = gradient;
ctx.fillRect(20, 20, 200, 100);
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D.createLinearGradient' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
createLinearGradient
|
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 |
完整支持
NOT_SUPPORTED_ERR
而不是
SYNTAX_ERR
.
CanvasRenderingContext2D
CanvasRenderingContext2D.createRadialGradient()
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()