Skip to content

Shapes

Rendx 内置 14 种形状,全部通过 Node.create(type, attrs) 创建。

形状一览

类型from() 参数说明
text(x, y, text)文本
circle(cx, cy, r)圆形
rect(x, y, width, height)矩形
line(x1, y1, x2, y2)线段
image(src, x, y, width, height)图片
path自由路径自定义路径
curve曲线数据曲线
area面积数据面积图
polygon多边形数据多边形
sector扇形参数扇形
arc弧线参数弧线
symbol符号参数符号标记
round圆角矩形参数圆角矩形
rectBuffer批量矩形批量矩形(性能优化)

使用示例

typescript
// 圆形
const circle = Node.create('circle', { fill: '#ff0000' });
circle.shape.from(200, 200, 50);

// 矩形
const rect = Node.create('rect', { fill: '#0066ff', opacity: 0.8 });
rect.shape.from(100, 100, 200, 150);

// 文本
const text = Node.create('text', {
  fill: '#333',
  fontSize: 24,
  fontFamily: 'sans-serif'
});
text.shape.from(100, 50, 'Hello Rendx!');

// 线段
const line = Node.create('line', { stroke: '#999', strokeWidth: 2 });
line.shape.from(0, 0, 200, 200);

// 图片
const img = Node.create('image');
img.shape.from('/path/to/image.png', 0, 0, 200, 150);

Shape 基类

所有形状的公共基类,核心 API:

方法说明
from(...args)设置形状参数
build()构建路径并计算包围盒
path()返回 SVG 路径字符串
path2d()返回 Path2D 对象(缓存)
hit(point, attrs)命中检测
useTransform()启用形状动画
boundingBox形状包围盒
dispose()释放资源

Attributes

视觉属性容器,常用属性:

属性类型说明
fillstring填充颜色
strokestring描边颜色
opacitynumber不透明度
strokeWidthnumber描边宽度
fontSizenumber字号
fontFamilystring字体
typescript
// 设置方式
const node = Node.create('rect', { fill: '#ff0000' }); // 创建时
node.attrs.set('fill', '#00ff00');                      // 单独设置
node.attrs.from({ fill: '#ff0000', opacity: 0.5 });     // 批量设置
node.attrs.merge({ stroke: '#000' });                   // 合并