logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.26
  • Canvas
    • Introduction
    • Options
    • Built-in objects
    • Coordinate system
    • Scenegraph & Lifecycle
    • Event
    • OffscreenCanvas & Server-side Rendering
    • CustomElementRegistry
    • Frequently Asked Questions
  • Renderer
    • Introduction
    • Canvas Renderer
    • Canvaskit Renderer
    • SVG Renderer
    • WebGL Renderer
    • WebGPU Renderer
    • Custom Renderer
  • Camera
    • Introduction
    • Camera Parameters
    • Camera action
    • Camera animation
  • Event
    • Introduction
    • Event Object
    • Gesture & Drag'n'Drop
    • Frequently Asked Questions
  • Animation
    • Web Animations API
    • Lottie
  • Basic Shapes
    • Basic Concepts
    • DisplayObject
    • Group
    • Text
    • Circle
    • Ellipse
    • Rect
    • Image
    • Line
    • Polygon
    • Polyline
    • Path
    • HTML
  • Style System
    • Introduction
    • CSS Typed OM
    • Inheritance
    • CSS Properties & Values API
    • CSS Layout API
    • Pattern
    • Gradient
  • 3D
    • 材质
    • 几何
    • Mesh
    • 光源
    • 雾
    • 交互
  • Built-in Objects
    • EventTarget
    • Node
    • Element
    • Document
    • MutationObserver
    • Utils
  • GPGPU
    • Introduction
    • Programming Model
    • Kernel API
    • Principles of classical GPGPU implementation
    • webgpu-graph
  • Declarative programming
    • 使用 Web Components
  • Devtools
    • G 开发者工具
    • 内置的渲染统计信息
    • 第三方开发调试工具

Gradient

Previous
Pattern
Next
材质

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

In CSS, gradients are created by functions such as linear-gradient.

background: linear-gradient(#e66465, #9198e5);

We have followed that syntax so that it can be used in properties that support gradients.

rect.style.fill = 'linear-gradient(#e66465, #9198e5)';

In this example we show the currently supported gradient effects, including linear and radial gradients, multiple gradients overlaid, etc.

gradient

linear-gradient

Linear gradients are used to create an image that represents a linear gradient of two or more colors. This tutorial can help you understand the meaning and calculation logic of the linear gradient direction.

The usage is exactly like CSS linear-gradient, but with the following differences.

  • The gradient direction defaults to bottom-to-top in CSS, while we use left-to-right to be consistent with Canvas / SVG.

So a linear gradient with a left-to-right orientation and a rotation angle of 0 would look like this, example.

rect.style.fill = 'linear-gradient(0deg, blue, green 40%, red)';
linear gradient

Finally, consistent with CSS, multiple gradients can be stacked.

rect.style.fill = `linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%)`;

radial-gradient

A radial gradient consists of a gradual transition between two or more colors emanating from the origin.

The usage is exactly like CSS radial-gradient.

So a gradient centered at the center of the shape, with a radial gradient transitioning from red to blue to green as follows, example.

rect.style.fill = 'radial-gradient(circle at center, red, blue, green 100%)';
radial gradient

Caution.

  • Shapes are only supported for circle but not for ellipse
  • Support for specifying circle radius
    • 'closest-side' The gradient's ending shape meets the side of the box closest to its center.
    • 'farthest-corner' The default value, the gradient's ending shape is sized so that it exactly meets the farthest corner of the box from its center.
    • 'closest-corner' The gradient's ending shape is sized so that it exactly meets the closest corner of the box from its center.
    • 'farthest-side' Similar to closest-side, except the ending shape is sized to meet the side of the box farthest from its center (or vertical and horizontal sides).
    • <length> e.g. 'radial-gradient(circle 80px at center, red 100%, blue 100%)'

The following figures show the effect of 'closest-side', 'farthest-side' and 80px respectively.

radial-gradient-closest-sideradial-gradient-farthest-sideradial-gradient-size-80
  • Support specifying the position of the center of the circle and positioning it relative to the upper left corner of the enclosing box, e.g. radial-gradient(circle at 50px 50px, red, blue, green 100%).
    • 'top' Top edge midpoint
    • 'left' Left edge midpoint
    • 'bottom' Bottom edge midpoint
    • 'right' Right edge midpoint
    • 'center' Horizontal and vertical centering
    • 'top left' Left-top corner
    • 'left top' Same as 'top left'
    • 'top right' Right-top corner
    • 'bottom left' Left-bottom corner
    • 'bottom right' Right-bottom corner
    • <length> <length> e.g. '25% 25%' and '50px 50px'

The following figures show the effect of '50px 50px', 'top right' and 'left' respectively.

radial-gradient-center-50-50radial-gradient-center-top-rightradial-gradient-center-left
  • Like linear gradients, it also supports multiple overlays.