单指:
直线滑动:
swipeToPoint(x,y ex,ey, swipeTime)
模拟手指微微曲线滑动:
function SwipeRnd() {
this.step = 0.08
}
SwipeRnd.prototype._bezier_curves = function (cp, t) {
let 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
return {
"x": (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x,
"y": (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y
}
}
SwipeRnd.prototype._gesture = function (swipeList, time, time1) {
let touch1 = [{"action": 0, "x": swipeList[0][0], "y": swipeList[0][1], "pointer": 1, "delay": time}]
for (let i = 1; i < swipeList.length - 1; i++) {
touch1.push({"action": 2, "x": swipeList[i][0], "y": swipeList[i][1], "pointer": 1, "delay": time});
}
touch1.push({
"action": 1,
"x": swipeList[swipeList.length - 1][0],
"y": swipeList[swipeList.length - 1][1],
"pointer": 1,
"delay": time
})
// 实现手指移动
return multiTouch(touch1, null, null, time1);
}
SwipeRnd.prototype._rndSwipe = function (qx, qy, zx, zy) {
let xxyy = [],
xxy = [],
point = [],
dx = [{
"x": random(qx, qx + 50),
"y": random(qy, qy + 50)
}, {
"x": random(qx - 100, qx + 100),
"y": random(qy, qy + 50)
}, {
"x": random(zx - 100, zx + 100),
"y": random(zy, zy + 50),
}, {
"x": zx,
"y": zy
}]
for (let i = 0; i < dx.length; i++) {
point.push(dx[i])
}
for (let i = 0; i < 1; i += this.step) {
// _bezier_curves曲线
xxyy = [~~(this._bezier_curves(point, i).x), ~~(this._bezier_curves(point, i).y)]
xxy.push(xxyy);
}
return xxy
}
SwipeRnd.prototype.rndSwipe = function (startX, startY, endX, endY,
timeStart, timeEnd, timeOut, step) {
// 默认50开始,100结束,超时2000,step0.08
timeStart = timeStart || 50
timeEnd = timeEnd || timeStart + 50
timeOut = timeOut || 2 * 1000
this.step = step || this.step
return this._gesture(this._rndSwipe(startX, startY, endX, endY), random(timeStart, timeEnd), timeOut)
}
使用:
const rSwipe = new SwipeRnd()
rSwipe.step = 0.05
rSwipe.rndSwipe(x,y,ex,ey)