Trim Path 按百分比截取路径的一段进行绘制,是实现路径描边动画的核心技术。
| 函数 | 说明 |
|---|---|
int xgeShapeExTrimPath(xge_shape_ex pShape, float fBegin, float fEnd, int bSimultaneous) | 设置 Trim 区间 |
int xgeShapeExTrimPathGet(xge_shape_ex pShape, float* pBegin, float* pEnd, int* pSimultaneous, int* pEnabled) | 获取 Trim 参数 |
int xgeShapeExTrimClear(xge_shape_ex pShape) | 清除 Trim(恢复完整路径) |
gcc -O2 -I. -o ch51.exe ch51_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch51.exe
static float s_fProgress = 0.0f;
void OnFrame(void) {
s_fProgress += 0.005f;
if (s_fProgress > 1.0f) s_fProgress = 0.0f;
// 从 0% 到 progress% 绘制路径
xgeShapeExTrimPath(g_shape, 0.0f, s_fProgress, 1);
xgeShapeExDraw(g_shape, 0.25f);
}xge_shape_ex ring = NULL;
xgeShapeExCreate(&ring);
xgeShapeExAppendCircle(ring, 400, 300, 150, 150, 1);
xgeShapeExStrokeColor(ring, 0x4FD8C2FF);
xgeShapeExStrokeWidth(ring, 10.0f);
xgeShapeExStrokeCap(ring, XGE_SHAPE_EX_CAP_ROUND);
// 显示 75% 进度
xgeShapeExTrimPath(ring, 0.0f, 0.75f, 1);
xgeShapeExDraw(ring, 0.25f);将以下代码保存为 ch51_main.c,编译后运行即可看到 Trim Path 路径描边动画与进度环效果:
#include "xge.h"
static float s_fProgress = 0.0f;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
s_fProgress += 0.005f;
if (s_fProgress > 1.0f) s_fProgress = 0.0f;
// 心形路径描边动画
xge_shape_ex heart = NULL;
xgeShapeExCreate(&heart);
xgeShapeExMoveTo(heart, 400, 200);
xgeShapeExCubicTo(heart, 400,160, 340,100, 280,140);
xgeShapeExCubicTo(heart, 200,200, 280,320, 400,400);
xgeShapeExCubicTo(heart, 520,320, 600,200, 520,140);
xgeShapeExCubicTo(heart, 460,100, 400,160, 400,200);
xgeShapeExStrokeColor(heart, 0xFF6B8AFF);
xgeShapeExStrokeWidth(heart, 4.0f);
xgeShapeExStrokeCap(heart, XGE_SHAPE_EX_CAP_ROUND);
xgeShapeExTrimPath(heart, 0.0f, s_fProgress, 1);
xgeShapeExDraw(heart, 0.25f);
xgeShapeExDestroy(heart);
// 进度环:75%
xge_shape_ex ring = NULL;
xgeShapeExCreate(&ring);
xgeShapeExAppendCircle(ring, 400, 500, 60, 60, 1);
xgeShapeExStrokeColor(ring, 0x4FD8C2FF);
xgeShapeExStrokeWidth(ring, 8.0f);
xgeShapeExStrokeCap(ring, XGE_SHAPE_EX_CAP_ROUND);
xgeShapeExTrimPath(ring, 0.0f, 0.75f, 1);
xgeShapeExDraw(ring, 0.25f);
xgeShapeExDestroy(ring);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch51 - Trim Path";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}