UNPKG

bindingx-web-polyfill

Version:

The web polyfill for BindingX.

210 lines (171 loc) 4.89 kB
/** Copyright 2018 Alibaba Group Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ 'use strict'; import _ from 'simple-lodash'; import assign from 'object-assign'; import {pxTo750} from './utils'; const {abs} = Math; const DEFAULT_CONFIG = { pointers:2, threshold:2 }; export default class MultiTouchGesture { lastScale = -1; lastRotation = -1; rotation = -1; lastDistance = 0; deltaX = 0; deltaY = 0; events = { 'pinchstart':[], 'pinch':[], 'pinchend':[], 'pinchcancel':[], /////// 'rotationstart':[], 'rotation':[], 'rotationend':[], 'rotationcancel':[] }; constructor(el, config) { this.el = el; this.config = assign(DEFAULT_CONFIG, config); this.el.addEventListener('touchstart', this.onTouchStart); this.el.addEventListener('touchmove', this.onTouchMove); this.el.addEventListener('touchend', this.onTouchEnd); this.el.addEventListener('touchcancel', this.onTouchCancel); } onTouchStart = (e) => { //no-op } handleGestureStart = (e) => { e.preventDefault(); let x0 = pxTo750(e.touches[0].pageX); let y0 = pxTo750(e.touches[0].pageY); let x1 = pxTo750(e.touches[1].pageX); let y1 = pxTo750(e.touches[1].pageY); this.lastDistance = this.distanceBetweenTwoPoints(x0,x1,y0,y1); this.lastScale = 1; this.lastRotation = this.getAngle(x0,y0,x1,y1); this.events.pinchstart.forEach((handler) => { handler({ scale: this.lastScale }); }); this.events.rotationstart.forEach((handler) => { handler({ rotation: 0 }); }); } getPointerNum = (e)=>{ return e.touches.length; } distanceBetweenTwoPoints = (x0, x1, y0, y1)=>{ return Math.hypot(x1 - x0, y1 - y0); } getAngle(originX, originY, projectionX, projectionY) { let rad = Math.atan2(projectionY - originY, projectionX - originX); return rad*180/Math.PI; } onTouchMove = (e) => { let {pointers,threshold} = this.config; if(this.getPointerNum(e) !== pointers) { return; } if(this.lastScale < 0 || this.lastRotation < 0) { this.handleGestureStart(e); return; } let x0 = pxTo750(e.touches[0].pageX); let y0 = pxTo750(e.touches[0].pageY); let x1 = pxTo750(e.touches[1].pageX); let y1 = pxTo750(e.touches[1].pageY); let curDistance = this.distanceBetweenTwoPoints(x0,x1,y0,y1); // let deltaDistance = curDistance - this.lastDistance; this.lastScale = curDistance / this.lastDistance; let curRotation = this.getAngle(x0,y0,x1,y1); this.rotation = curRotation-this.lastRotation; this.events.pinch.forEach((handler) => { handler({ scale:this.lastScale }); }); this.events.rotation.forEach((handler) => { handler({ rotation:this.rotation }); }); } onTouchEnd = (e) => { this.events.pinchend.forEach((handler) => { handler({ scale:this.lastScale }); }); this.events.rotationend.forEach((handler) => { handler({ scale:this.rotation }); }); } onTouchCancel = (e) => { this.events.pinchcancel.forEach((handler) => { handler({ scale:this.lastScale }); }); this.events.rotationcancel.forEach((handler) => { handler({ scale:this.rotation }); }); } on(fn, handler) { if (!this.events[fn]) return; this.events[fn].push(handler); } destroy() { this.el.removeEventListener('touchstart', this.onTouchStart); this.el.removeEventListener('touchmove', this.onTouchMove); this.el.removeEventListener('touchend', this.onTouchEnd); this.el.removeEventListener('touchcancel', this.onTouchCancel); this.offAll(); this.lastDistance = 0; this.lastScale = -1; this.lastRotation = -1; this.rotation = -1; } offAll() { _.map(this.events, (handlers, fn) => { _.forEach(handlers, (handler) => { this.off(fn, handler); }); }); } off(fn, handler) { if (!fn) return; if (fn && this.events[fn] && this.events[fn].length) { if (!handler) return; let h = _.find(this.events[fn], (o) => { return o === handler; }); let i = _.findIndex(this.events[fn], (o) => { return o === handler; }); if (h) { this.events[fn].splice(i, 1); } } } }