pixi.js - Pixi.js - 如何为线条制作动画
问题描述
我一直在到处寻找,但找不到在 Pixi.js 中为线条设置动画的方法。
鉴于此代码:
var line = new PIXI.Graphics();
line.lineStyle(1, 0xff0000);
line.moveTo(0,window.innerHeight/2);
line.lineTo(window.innerWidth/2, 0);
line.lineTo(window.innerWidth, window.innerHeight/2);
app.stage.addChild(line);
它画了这个宏伟的jsfiddle
我想实现这个非常简单的线条动画:
第1步
第2步
当然我猜这不应该很复杂,但我不知道我错过了什么......
任何帮助将不胜感激!
解决方案
在 API 术语中,在图形对象内部绘图与在没有 Pixi 的情况下绘制到 Canvas 非常相似。
需要一个渲染循环,在每个循环中清除和重绘画布。Pixi 提供了一个有用的 Ticker,可用于在循环中运行函数。
这是一个示例(在本例中为无限动画)和一个jsfiddle 示例:
var line = new PIXI.Graphics(),
centerY = 0,
increment = 2;
app.stage.addChild(line);
app.ticker.add(() => {
// clear the graphics object ('wipe the blackboard')
line.clear();
// redraw a new line
drawLine(centerY);
// calculate the next position
centerY = (centerY < window.innerHeight) ? centerY = centerY + increment : 0;
});
function drawLine(centerY) {
line.lineStyle(1, 0xff0000);
line.moveTo(0,window.innerHeight/2);
line.lineTo(window.innerWidth/2, centerY);
line.lineTo(window.innerWidth, window.innerHeight/2);
}
推荐阅读
- node.js - 在 nextjs 中使用带有 nodejs api 的会话
- c# - 如何在 SmartArt PowerPoint C#/VBA 中填充图片?
- list - 尝试在颤动的BottomSheet中构建动态列表
- apache-kafka - 在 kafka 中哪里存储可重用数据?
- java - Flutter Insecure HTTP is not allowed by platform Error
- ios - 我怎样才能有一个“点击”到父视图的子视图
- wpf - Windows10 NT Shell ,使用 WPF Application 替换 Explorer.exe
- python - pandas - 删除 MultiIndex DataFrame 中的重复行
- node.js - ExpressJS: req.hostname 属性是否足以将其用作针对域白名单的安全性验证器?
- google-sheets - 将列转入行并添加计数