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 开发者工具
    • 内置的渲染统计信息
    • 第三方开发调试工具

Rect

Previous
Ellipse
Next
Image

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...

You can refer to the <rect> element of SVG.

The following example defines a rounded rectangle with the top left vertex at (200, 100).

const rect = new Rect({
style: {
x: 200,
y: 100,
width: 300,
height: 200,
fill: '#1890FF',
stroke: '#F04864',
lineWidth: 4,
radius: 8,
},
});

Inherited from

Inherits style property from DisplayObject.

anchor

The default value is [0, 0]. For details, see DisplayObject's anchor.

transformOrigin

The default value is left top. For details, see DisplayObject's transformOrigin.

Additional Properties

x

The x-axis coordinate of the top-left vertex of the rectangle in the local coordinate system.

https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/x

Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-noyes<percentage> <length>

y

The y-axis coordinate of the top-left vertex of the rectangle in the local coordinate system.

https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/y

Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-noyes<percentage> <length>

width

The width of the rectangle. Supports taking negative numbers with the effect of reversing along the Y-axis, example.

negative width of rect

This is consistent with the Canvas2D API, see. In the SVG spec, it is noted that the <rect> width and height attribute is not displayed when it is negative, for example, in Chrome this results in the following error: Error: <rect> attribute height: A negative value is not valid. ("-100"):

The width and height properties define the overall width and height of the rectangle. A negative value for either property is illegal and must be ignored as a parsing error. A computed value of zero for either dimension disables rendering of the element.

We circumvent this problem by using <path> instead of <rect> for drawing in g-svg.

https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/width

Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-noyes<percentage> <length>

height

The height of the rectangle. Supports taking negative numbers with the effect of reversing along the X-axis, example.

negative height of rect

https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/height

Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-noyes<percentage> <length>

radius

Corner radius, unlike SVG <rect> which only supports cx/cy uniform settings, here you can specify the radius of each of the four corners, example.

rounded rect
rect.style.radius = [0, 4, 8, 16];
rect.style.radius = '0 4px 8px 16px';

The following values are supported, set in the order of top left, top right, bottom right, bottom left.

  • number Uniform setting of four rounded corner radii
  • number[] Setting the four corner radii separately will make up the default fraction of.
    • [ 1 ] equals [ 1, 1, 1, 1 ]
    • [ 1, 2 ] equals [ 1, 2, 1, 2 ]
    • [ 1, 2, 3 ] equals [ 1, 2, 3, 2 ]
    • [ 1, 2, 3, 4 ]
  • string Similar to the CSS padding property, using spaces to separate

When actually drawn, the maximum value of the radius of the rounded corners is limited to half the maximum value of the width and height of the rectangle.

const [tlr, trr, brr, blr] = radius.map((r) =>
clamp(
r.value,
0,
Math.min(Math.abs(width.value) / 2, Math.abs(height.value) / 2),
),
);
Initial valueApplicable elementsInheritableAnimatableComputed value
'0'-noyes<percentage> <length>