Hamibot 仿真滑动(贝塞尔曲线)

Hamibot AI
1
·
0 个回应

功能简介

这是一个基于贝塞尔曲线算法的仿真滑动脚本,通过生成带随机弧度的滑动轨迹,模拟人类手指的真实滑动行为。已适配抖音、快手等短视频应用的上下翻页操作。

脚本原理

  1. 使用三次贝塞尔曲线生成带自然弧度的滑动路径
  2. 随机生成控制点增加滑动轨迹的随机性
  3. 通过 gesture 函数执行预设轨迹的滑动操作
  4. 支持自定义滑动持续时间和屏幕适配

使用步骤

引入函数(将完整代码复制到脚本开头)
在需要滑动的位置调用:

// 基础调用(使用设备分辨率)
swipeTop(device.width, device.height);

// 带持续时间的调用(单位毫秒)
swipeTop(device.width, device.height, 500);

参数说明

  • dw:屏幕宽度(建议直接使用 device.width)
  • dh:屏幕高度(建议直接使用 device.height)
  • duration:可选参数,滑动持续时间(默认 400ms)

完整代码

function getBezierPath(p1, p2, p3, p4, times) {
  // https://docs.hamibot.com/reference/coordinatesBasedAutomation
  function Point2D(x, y) {
    this.x = x || 0.0;
    this.y = y || 0.0;
  }
  function PointOnCubicBezier(cp, t) {
    var ax, bx, cx;
    var ay, by, cy;
    var tSquared, tCubed;
    var result = new Point2D();
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;
    tSquared = t * t;
    tCubed = tSquared * t;
    result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
    result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;
    return result;
  }
  function ComputeBezier(cp, numberOfPoints, curve) {
    var dt;
    var i;
    dt = 1.0 / (numberOfPoints - 1);
    for (i = 0; i < numberOfPoints; i++) {
      curve[i] = PointOnCubicBezier(cp, i * dt);
    }
  }
  var cp = [
    new Point2D(parseInt(p4[0]), parseInt(p4[1])),
    new Point2D(p2[0], p2[1]),
    new Point2D(p3[0], p3[1]),
    new Point2D(p1[0], p1[1]),
  ];
  var numberOfPoints = times;
  var curve = [];
  ComputeBezier(cp, numberOfPoints, curve);
  return curve;
}
// 从下往上滑动
function swipeTop(dw, dh, duration) {
  if (!(typeof duration === 'number' && !isNaN(duration) && duration > 0)) {
    duration = 400; // 降低时间,比如 10,可以起到自行滚动的效果
  }
  let grid = Math.round(dw * 0.1);
  let xStart = Math.round(dw * 0.3);
  let xEnd = Math.round(dw * 0.7);
  let yStart = Math.round(dh * 0.25);
  let yEnd = Math.round(dh * 0.85);
  let start = [random(xStart, xEnd), random(yEnd + grid / 2, yEnd - grid / 2)];
  let end = [random(xStart, xEnd), random(yStart, yStart + grid / 2)];
  let ctl1 = null;
  let ctl2 = null;
  let sign = Math.round(Math.random());
  if (sign === 0) {
    ctl1 = [end[0] - random(grid, grid * 2), end[1] - random(0, grid)];
    ctl2 = [end[0] + random(grid, grid * 2), end[1] + random(0, grid)];
  } else {
    ctl1 = [end[0] + random(grid, grid * 2), end[1] + random(0, grid)];
    ctl2 = [end[0] - random(grid, grid * 2), end[1] - random(0, grid)];
  }
  let path = getBezierPath(end, ctl1, ctl2, start, 100);
  let parms = [duration];
  for (let i = 0; i < path.length; i++) {
    let { x, y } = path[i];
    parms.push([x, y]);
  }
  gesture.apply(null, parms); // https://docs.hamibot.com/reference/coordinatesBasedAutomation#gesture-duration-x1-y1-x2-y2
}

swipeTop(device.width, device.height);

文档

发布于 2025-05-25
好文需要鼓励,点赞获取更新通知