Appearance
Class: Graphics
图形绘制
ts
const node = app.stage.addChild(new Transform(Graphics));
node.size.set(1280, 720);
const g = node.getComponent(Graphics)!;
// 绘制多边形
g.begin();
g.drawPolygon([0, 0, 100, 0, 100, 100, 50, 50, 0, 100]);
g.fill('#00ffff');
g.stroke('#00ff00');
// 绘制线条
g.begin();
g.moveTo(120, 20)
g.lineTo(220, 20);
g.lineTo(220, 100);
// 闭合路径
g.closePath();
g.stroke('#00ff00', 5);
// 绘制线条
g.begin();
g.moveTo(120, 20)
g.lineTo(220, 20);
g.stroke('#00ff00', 5);
// 绘制圆弧
g.begin();
g.arc(340, 60, 50, 0, Math.PI);
g.stroke('#0000FF', 5);
// 绘制圆
g.begin();
g.drawCircle(460, 60, 50);
g.stroke('#0000FF', 5);
// 绘制圆角矩形
g.begin();
g.drawRect(560, 10, 100, 100, 8);
g.fill('#FF00FF');
g.stroke('#0000FF', 5);
// 绘制椭圆路径
g.begin();
g.ellipse(120, 200, 100, 80, 0, 0, Math.PI);
g.fill('#FF0000');
g.stroke('#00FF00', 5);
g.begin();
g.drawCircle(50, 210, 5);
g.fill('#000000');
g.begin();
g.drawCircle(80, 210, 5);
g.fill('#000000');
g.begin();
g.drawCircle(110, 210, 5);
g.fill('#000000');
// 绘制椭圆
g.begin();
g.drawEllipse(140, 210, 10, 5);
g.fill('#000000');
Hierarchy
↳
Graphics
Table of contents
Constructors
Properties
Methods
- arc
- begin
- clear
- closePath
- destroy
- drawCircle
- drawEllipse
- drawPolygon
- drawRect
- ellipse
- fill
- lineTo
- moveTo
- onDestroy
- pushTask
- start
- stroke
- update
Constructors
constructor
• new Graphics()
组件不能单独实例化,必须通过Transform.addComponent创建
ts
const node = new Transform();
const componet = node.addComponent(Component);
Inherited from
Defined in
src/core/component/Component.ts:37
Properties
emitter
• emitter: EventEmitter
<string
| symbol
, any
>
事件处理器
Inherited from
Defined in
src/core/component/Component.ts:27
id
• id: number
用于表示组件的唯一标识
Inherited from
Defined in
src/core/component/Component.ts:12
name
• name: string
= ''
组件名
Inherited from
Defined in
src/core/component/Component.ts:17
node
组件挂载节点
Inherited from
Defined in
src/core/component/Component.ts:22
redraw
• redraw: boolean
= true
是否需要重绘
Defined in
src/core/component/Graphics.ts:78
tasks
• tasks: any
[] = []
记录要执行的操作记录
Defined in
src/core/component/Graphics.ts:73
texture
• Optional
texture: Texture
<TextureResource
>
图片纹理,储存图片的相关信息
Defined in
src/core/component/Graphics.ts:69
Methods
arc
▸ arc(x
, y
, radians
, startAngle
, endAngle
): void
绘制圆弧
Parameters
Name | Type | Description |
---|---|---|
x | number | 圆心位置x坐标 |
y | number | 圆心位置y坐标 |
radians | number | 半径 |
startAngle | number | 圆弧起始位置 |
endAngle | number | 圆弧结束位置 |
Returns
void
Defined in
src/core/component/Graphics.ts:193
begin
▸ begin(): void
开启新的绘制
Returns
void
Defined in
src/core/component/Graphics.ts:111
clear
▸ clear(): void
清空画布(清空操作指令历史)
Returns
void
Defined in
src/core/component/Graphics.ts:105
closePath
▸ closePath(): void
闭合路径,会将起始点与结束点用直线连接
Returns
void
Defined in
src/core/component/Graphics.ts:118
destroy
▸ destroy(): void
消毁
Returns
void
Inherited from
Defined in
src/core/component/Component.ts:55
drawCircle
▸ drawCircle(x
, y
, radians
): void
绘制一个圆
Parameters
Name | Type | Description |
---|---|---|
x | number | 圆心位置x坐标 |
y | number | 圆心位置y坐标 |
radians | number | 半径 |
Returns
void
Defined in
src/core/component/Graphics.ts:203
drawEllipse
▸ drawEllipse(x
, y
, radiusX
, radiusY
): void
绘制一个椭圆
Parameters
Name | Type | Description |
---|---|---|
x | number | 椭圆圆心的 x 轴坐标。 |
y | number | - |
radiusX | number | 椭圆长轴的半径。 |
radiusY | number | 椭圆短轴的半径。 |
Returns
void
Defined in
src/core/component/Graphics.ts:229
drawPolygon
▸ drawPolygon(points
): void
绘制一个多边形
Parameters
Name | Type | Description |
---|---|---|
points | number [] | 多边形每个点的坐标([x1, y2, x2, y2, x3, y3, ...]) |
Returns
void
Defined in
src/core/component/Graphics.ts:237
drawRect
▸ drawRect(x
, y
, width
, height
, radius?
): void
绘制一个矩形,可带圆角
Parameters
Name | Type | Default value | Description |
---|---|---|---|
x | number | undefined | 起始位置x坐标 |
y | number | undefined | 起始位置y坐标 |
width | number | undefined | 宽 |
height | number | undefined | 高 |
radius | number | 0 | 圆角大小 |
Returns
void
Defined in
src/core/component/Graphics.ts:162
ellipse
▸ ellipse(x
, y
, radiusX
, radiusY
, rotation
, startAngle
, endAngle
, anticlockwise?
): void
绘制一个椭圆
Parameters
Name | Type | Default value | Description |
---|---|---|---|
x | number | undefined | 椭圆圆心的 x 轴坐标。 |
y | number | undefined | - |
radiusX | number | undefined | 椭圆长轴的半径。 |
radiusY | number | undefined | 椭圆短轴的半径。 |
rotation | number | undefined | 椭圆的旋转角度,以弧度表示 (非角度度数)。 |
startAngle | number | undefined | 将要绘制的起始点角度,从 x 轴测量,以弧度表示 (非角度度数)。 |
endAngle | number | undefined | 椭圆将要绘制的结束点角度,以弧度表示 (非角度度数)。 |
anticlockwise | boolean | false | Boolean 选项,如果为 true,逆时针方向绘制椭圆(逆时针),反之顺时针方向绘制。 |
Returns
void
Defined in
src/core/component/Graphics.ts:218
fill
▸ fill(color
): void
填充颜色
Parameters
Name | Type | Description |
---|---|---|
color | string | 颜色 |
Returns
void
Defined in
src/core/component/Graphics.ts:126
lineTo
▸ lineTo(x
, y
): void
绘制一条直线到某点
Parameters
Name | Type | Description |
---|---|---|
x | number | x坐标 |
y | number | y坐标 |
Returns
void
Defined in
src/core/component/Graphics.ts:150
moveTo
▸ moveTo(x
, y
): void
移动起始点
Parameters
Name | Type |
---|---|
x | number |
y | number |
Returns
void
Defined in
src/core/component/Graphics.ts:141
onDestroy
▸ onDestroy(): void
组件被销毁时触发
Returns
void
Overrides
Defined in
src/core/component/Graphics.ts:260
pushTask
▸ pushTask(name
, ...args
): void
新增一条绘制指令
Parameters
Name | Type | Description |
---|---|---|
name | string | 指令名 |
...args | (string | number )[] | 指令参数 |
Returns
void
Defined in
src/core/component/Graphics.ts:94
start
▸ start(): void
组件被挂载到节点时触发
Returns
void
Overrides
Defined in
src/core/component/Graphics.ts:80
stroke
▸ stroke(color
, width?
): void
设置边框或线条
Parameters
Name | Type | Default value | Description |
---|---|---|---|
color | string | undefined | 颜色 |
width | number | 1 | - |
Returns
void
Defined in
src/core/component/Graphics.ts:133
update
▸ Optional
update(time
): void
每帧调用
Parameters
Name | Type | Description |
---|---|---|
time | number | 和上一帧的间隔时间 ms |
Returns
void