import TweenOne, { Tween, Ticker } from 'tween-one'; export interface Props { app: any; camerasScale?: number; scaleNumber?: number; xOpenBool?: boolean; yOpenBool?: boolean; currentScroll: XYProps; closeScrollResistance?: boolean; pageInnerRect: { width: number; height: number }; innerReact?: { width: number; height: number }; useWindowEvent?: boolean; setIsScrollSize?: number; } export interface XYProps { x: number; y: number; } export default class TouchScrollEvent { private startPos?: XYProps; private moveScroll: XYProps = { x: 0, y: 0 }; private prevMoveScroll: XYProps = { x: 0, y: 0 }; private speed: XYProps = { x: 0, y: 0 }; private speedFloat: XYProps = { x: 0.9, y: 0.9 }; private interval: string | null; app: any; dpr: number = window.devicePixelRatio; currentScroll: XYProps; innerRect: Props['pageInnerRect']; resistanceNum: number = 300; // 超出范围返弹 tween?: Tween; pageInnerRect: Props['pageInnerRect']; xOpenBool: boolean = true; yOpenBool: boolean = true; disable: boolean = false; isScroll: boolean = false; setIsScrollSize: number; constructor( { app, pageInnerRect, xOpenBool, yOpenBool, closeScrollResistance, currentScroll = { x: 0, y: 0 }, setIsScrollSize, innerReact, useWindowEvent, }: Props, public callback: (e: XYProps) => void ) { this.app = app; this.callback = callback; this.pageInnerRect = pageInnerRect; this.xOpenBool = xOpenBool ?? this.xOpenBool; this.yOpenBool = yOpenBool ?? this.yOpenBool; this.currentScroll = currentScroll; this.setIsScrollSize = setIsScrollSize || 5; if (closeScrollResistance) { this.resistanceNum = 0; } this.innerRect = innerReact || { width: window.innerWidth, height: window.innerHeight, }; // touch 事件 // delete PIXI.Renderer.__plugins.interaction; app.stage.interactive = true; app.stage.hitArea = app.renderer.scene; this.addEventListener(!!useWindowEvent); this.callback(this.currentScroll); // document.addEventListener('touchstart', this.touchStart); // document.addEventListener('touchmove', this.touchMove); // document.addEventListener('touchend', this.touchEnd); } addEventListener(winEvent: boolean): void { this.app.stage.addEventListener('pointerdown', this.touchStart); if (winEvent) { document.addEventListener('touchmove', this.touchMove); document.addEventListener('touchend', this.touchEnd); } else { this.app.stage.addEventListener('pointermove', this.touchMove); this.app.stage.addEventListener('pointerup', this.touchEnd); } } getResistanceToValue = ( cy: number, value: number, type: 'width' | 'height' ) => { let ty; const ratio = (cy > 0 ? cy : value - this.pageInnerRect[type]) / this.resistanceNum; const factor = 1 - 1 / (ratio + 1); if (cy > 0) { ty = this.resistanceNum * factor; } else { ty = -(this.pageInnerRect[type] - this.innerRect[type]) - this.resistanceNum * factor; } return ty; }; getBackValue = (type: 'x' | 'y') => { const wOrH = type === 'x' ? 'width' : 'height'; return this.currentScroll[type] > 0 ? 0 : -(this.pageInnerRect[wOrH] - this.innerRect[wOrH]); }; touchStart = (e: any) => { e.stopPropagation?.(); if (this.disable) { return; } if (this.tween) { this.tween.kill(); // TweenOne.kill(this.currentScroll); this.tween = undefined; } const x = e.x || e.clientX || e.touches?.[0]?.clientX; const y = e.y || e.clientY || e.touches?.[0]?.clientY; this.startPos = { x, y, }; // 屏幕实际像素转换; this.prevMoveScroll = this.currentScroll; this.moveScroll = this.currentScroll; this.isScroll = false; this.speed = { x: 0, y: 0, }; this.speedFloat = { x: 0.9, y: 0.9 }; if (this.interval) { Ticker.clear(this.interval); } this.interval = Ticker.interval(() => { const currentSpeed = { x: this.moveScroll.x - this.prevMoveScroll.x, y: this.moveScroll.y - this.prevMoveScroll.y, }; this.speed = { x: this.speed.x + (currentSpeed.x - this.speed.x) / 2, y: this.speed.y + (currentSpeed.y - this.speed.y) / 2, }; this.prevMoveScroll = { ...this.moveScroll, }; }, 16.667); }; touchMove = (e: any) => { if (typeof this.startPos === 'undefined' || this.disable) { return; } e.stopPropagation?.(); const ex = e.x || e.clientX || e.touches?.[0]?.clientX; const ey = e.y || e.clientY || e.touches?.[0]?.clientY; const x = (ex) - this.startPos.x; const y = (ey) - this.startPos.y; // x let tx = this.currentScroll.x + x; const rightWidth = Math.abs(tx) + this.innerRect.width; if (tx > 0 || rightWidth > this.pageInnerRect.width) { tx = this.getResistanceToValue(tx, rightWidth, 'width'); } // y let ty = this.currentScroll.y + y; const bottomHeight = Math.abs(ty) + this.innerRect.height; if (ty > 0 || bottomHeight > this.pageInnerRect.height) { ty = this.getResistanceToValue(ty, bottomHeight, 'height'); } this.moveScroll = { x: this.xOpenBool ? tx : this.currentScroll.x, y: this.yOpenBool ? ty : this.currentScroll.y, }; this.isScroll = this.isScroll || Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) > this.setIsScrollSize; this.callback(this.moveScroll); }; backScroll = () => { const rightWidth = Math.abs(this.currentScroll.x) + this.innerRect.width; const bottomHeight = Math.abs(this.currentScroll.y) + this.innerRect.height; // 是否需要启动动画来滑动; let animDuration: any; // 到顶回弹 const toEndScroll: { x?: number; y?: number } = {}; // x到顶 if ( this.xOpenBool && (this.currentScroll.x > 0 || rightWidth > this.pageInnerRect.width) ) { animDuration = { x: 450 }; toEndScroll.x = this.getBackValue('x'); } // y 回顶 if ( this.yOpenBool && (this.currentScroll.y > 0 || bottomHeight > this.pageInnerRect.height) ) { toEndScroll.y = this.getBackValue('y'); animDuration = animDuration || {}; animDuration.y = 450; } if (animDuration) { const animation: any = animDuration.x === animDuration.y ? { ...toEndScroll, duration: animDuration.x, ease: 'easeOutQuart', } : [ animDuration.x ? { x: toEndScroll.x, duration: animDuration.x, ease: 'easeOutQuart', } : undefined, animDuration.y ? { appearTo: 0, y: toEndScroll.y, duration: animDuration.y, ease: 'easeOutQuart', } : undefined, ].filter(c => c); this.tween = TweenOne(this.currentScroll, { animation, onChangeTimeline: c => { this.callback(this.currentScroll); if (c.mode === 'onTimelineComplete') { this.tween = undefined; } }, }) as Tween; } }; inertialScroll = ({x: currentX, y: currentY}: XYProps) => { this.interval = Ticker.interval(() => { this.speed = { x: this.xOpenBool ? this.speed.x * this.speedFloat.x : 0, y: this.yOpenBool ? this.speed.y * this.speedFloat.y : 0, }; let backBool = false; let tx = this.currentScroll.x; if (this.xOpenBool) { tx = currentX = currentX + this.speed.x; const rightWidth = Math.abs(tx) + this.innerRect.width; if (tx > 0 || rightWidth > this.pageInnerRect.width) { tx = this.getResistanceToValue(tx, rightWidth, 'width'); backBool = true; this.speedFloat.x *= 0.85; } } let ty = this.currentScroll.y; if (this.yOpenBool) { ty = currentY = currentY + this.speed.y; const bottomHeight = Math.abs(ty) + this.innerRect.height; if (ty > 0 || bottomHeight > this.pageInnerRect.height) { backBool = true; ty = this.getResistanceToValue(ty, bottomHeight, 'height'); this.speedFloat.y *= 0.85; } } this.currentScroll = { x: tx, y: ty, }; this.callback(this.currentScroll); if ( (Math.abs(this.speed.y) < 0.01 && Math.abs(this.speed.x) < 0.01) && this.interval ) { Ticker.clear(this.interval); this.interval = null; if (backBool) { this.backScroll(); } } }, 16.667); }; touchEnd = (e: any) => { if (typeof this.startPos === 'undefined' || this.disable) { return; } if (this.interval) { Ticker.clear(this.interval); this.interval = null; } e.stopPropagation?.(); this.isScroll = false; if (this.tween) { this.tween.kill(); // TweenOne.kill(this.currentScroll); } // 惯性滑动 if ( (this.yOpenBool && Math.abs(this.speed.y) > 5) || (this.xOpenBool && Math.abs(this.speed.x) > 5) // 惯性滑动大于10像素开启 ) { const ex = e.x || e.clientX || e.changedTouches?.[0]?.clientX; const ey = e.y || e.clientY || e.changedTouches?.[0]?.clientY; const x = (ex) - this.startPos.x; const y = (ey) - this.startPos.y; const currentX = this.currentScroll.x + x; const currentY = this.currentScroll.y + y; // const minX = this.currentScroll.x + this.speed.x > 0 this.inertialScroll({ x: currentX, y: currentY }); } else { this.currentScroll = this.moveScroll; this.backScroll(); } this.startPos = undefined; }; dispose = () => { this.tween?.kill(); this.tween = undefined; this.app.stage.removeEventListener('pointerdown', this.touchStart); this.app.stage.removeEventListener('pointermove', this.touchMove); this.app.stage.removeEventListener('pointerup', this.touchEnd); document.removeEventListener('touchmove', this.touchMove); document.removeEventListener('touchend', this.touchEnd); }; }