lottie/expressions: support wiggle effect

usage: wiggle(freq, amp, octaves=1, amp_mult=.5, t=time)

issue: https://github.com/thorvg/thorvg/issues/1640
This commit is contained in:
Hermet Park 2025-03-27 22:27:17 +09:00 committed by Hermet Park
parent 1e1be53136
commit 11f59c03ae
3 changed files with 42 additions and 5 deletions

View file

@ -26,8 +26,8 @@
/* ThorVG Drawing Contents */
/************************************************************************/
#define NUM_PER_ROW 4
#define NUM_PER_COL 4
#define NUM_PER_ROW 5
#define NUM_PER_COL 5
struct UserExample : tvgexam::Example
{
@ -122,5 +122,5 @@ struct UserExample : tvgexam::Example
int main(int argc, char **argv)
{
return tvgexam::main(new UserExample, argc, argv, false, 800, 800, 0, true);
return tvgexam::main(new UserExample, argc, argv, false, 1024, 1024, 0, true);
}

File diff suppressed because one or more lines are too long

View file

@ -68,6 +68,12 @@ static ExpContent* _expcontent(LottieExpression* exp, float frameNo, LottieObjec
}
static float _rand()
{
return (float)(rand() % 10000001) * 0.0000001f;
}
static jerry_value_t _point2d(const Point& pt)
{
auto obj = jerry_object();
@ -513,7 +519,7 @@ static jerry_value_t _length(const jerry_call_info_t* info, const jerry_value_t
static jerry_value_t _random(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt)
{
return jerry_number((float)(rand() % 10000001) * 0.0000001f);
return jerry_number(_rand());
}
@ -722,6 +728,32 @@ static jerry_value_t _speedAtTime(const jerry_call_info_t* info, const jerry_val
}
static jerry_value_t _wiggle(const jerry_call_info_t* info, const jerry_value_t args[], const jerry_length_t argsCnt)
{
auto data = static_cast<ExpContent*>(jerry_object_get_native_ptr(info->function, &freeCb));
auto freq = jerry_value_as_number(args[0]);
auto amp = jerry_value_as_number(args[1]);
auto octaves = (argsCnt > 2) ? jerry_value_as_int32(args[2]) : 1;
auto ampm = (argsCnt > 3) ? jerry_value_as_number(args[3]) : 5.0f;
auto time = (argsCnt > 4) ? jerry_value_as_number(args[4]) : data->exp->comp->timeAtFrame(data->frameNo);
Point result = {100.0f, 100.0f};
for (int o = 0; o < octaves; ++o) {
auto repeat = int(time * freq);
auto frac = (time * freq - float(repeat)) * 1.25f;
for (int i = 0; i < repeat; ++i) {
result.x += (_rand() * 2.0f - 1.0f) * amp * frac;
result.y += (_rand() * 2.0f - 1.0f) * amp * frac;
}
freq *= 2.0f;
amp *= ampm;
}
return _point2d(result);
}
static bool _loopOutCommon(LottieExpression* exp, const jerry_value_t args[], const jerry_length_t argsCnt)
{
exp->loop.mode = LottieExpression::LoopMode::OutCycle;
@ -945,7 +977,11 @@ static void _buildProperty(float frameNo, jerry_value_t context, LottieExpressio
jerry_object_set_native_ptr(speedAtTime, nullptr, exp);
jerry_value_free(speedAtTime);
//wiggle(freq, amp, octaves=1, amp_mult=.5, t=time)
auto wiggle = jerry_function_external(_wiggle);
jerry_object_set_sz(context, "wiggle", wiggle);
jerry_object_set_native_ptr(wiggle, &freeCb, _expcontent(exp, frameNo, exp->object));
jerry_value_free(wiggle);
//temporalWiggle(freq, amp, octaves=1, amp_mult=.5, t=time)
//smooth(width=.2, samples=5, t=time)