paperjs - 如何停止和启动 view.onFrame
问题描述
我对paperjs很陌生。我的动画为此工作我使用以下javascript:
view.onFrame = function () {
drawYellowBlock();
}
该函数drawYellowBlock
绘制了一个黄色块,但这是动画的。动画完成后,我想停止 view.onFrame,因为我觉得没有必要让它继续运行,而不再发生任何事情。然后,当单击按钮时,我应该能够再次激活 onFrame。
这是可能的和必要的吗?
所以我希望我的绘图功能是这样的:
var scale = 0;
function drawYellowBlock() {
scale = scale + 0.1
//animate block
if(scale < = 1){
//make block grow
}
else{
//stop onFrame
}
$('button').click(function(){
scale = 0;
//start onFrame and Animation
});
解决方案
您可以简单地设置onFrame
方法中使用的标志来检查是否应该设置动画。
这是演示解决方案的草图。
// Draw the item with a small initial scale.
var item = new Path.Rectangle({
from: view.center - 100,
to: view.center + 100,
fillColor: 'orange',
applyMatrix: false
});
item.scaling = 0.1;
// Draw instructions.
new PointText({
content: 'Press space to start animation',
point: view.center + [0, -80],
justification: 'center'
});
// Create a flag that we will use to know wether we should animate or not.
var animating = false;
// On space key pressed...
function onKeyDown(event) {
if (event.key === 'space') {
// ...start animation.
animating = true;
}
}
// On frame...
function onFrame() {
// ...if animation has started...
if (animating) {
// ...scale up the item.
item.scaling += 0.05;
// When item is totally scaled up...
if (item.scaling.x >= 1) {
// ...stop animation.
animating = false;
}
}
}
推荐阅读
- python - ipython 中 python exec() 函数的意外行为
- python - 使用 facebook graph api 在 facebook 群组中发布
- javascript - 路由在使用 webpack 的反应应用程序中不起作用
- mongodb - 如何从 mongo 实例导出数据并导入到另一个实例?
- python - 使用 url 将 spark 连接到 hive
- c++ - 为什么我不能将带有不可复制类参数的 lambda 转换为 std::function?
- javascript - 当我关闭 SSH 选项卡时,如何防止 Discord 机器人脱机?
- python - python asyncio WebSocket客户端中的“'coroutine'对象不可调用”
- json - AWS CLI - 将 JSON 对象传递给 --tags 会出错
- mysql - 带有引号的MYSQL过程中的错误(准备好的语句)