All files / packages/tools/src/eventListeners/touch touchStartListener.ts

8.08% Statements 8/99
0% Branches 0/41
0% Functions 0/12
8.16% Lines 8/98

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503                                    1x                   1x                                                                       1x             1x               1x                                                                 1x                               1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
import { getEnabledElement, triggerEvent } from '@cornerstonejs/core';
import Events from '../../enums/Events';
import { Swipe } from '../../enums/Touch';
 
import { EventTypes, ITouchPoints, IPoints, IDistance } from '../../types';
 
import getTouchEventPoints from './getTouchEventPoints';
import {
  copyPoints,
  copyPointsList,
  getDeltaDistanceBetweenIPoints,
  getDeltaDistance,
  getDeltaPoints,
  getMeanTouchPoints,
  // getRotation
} from '../../utilities/touch';
import { Settings } from '@cornerstonejs/core';
 
const runtimeSettings = Settings.getRuntimeSettings();
 
const {
  TOUCH_START,
  TOUCH_START_ACTIVATE,
  TOUCH_PRESS,
  TOUCH_DRAG,
  TOUCH_END,
  TOUCH_TAP,
  TOUCH_SWIPE,
} = Events;
 
interface ITouchTapListenerState {
  element: HTMLDivElement;
  renderingEngineId: string;
  viewportId: string;
  startPointsList: ITouchPoints[];
  tapMaxDistance: number;
  tapTimeout: ReturnType<typeof setTimeout>;
  taps: number;
  tapToleranceMs: number;
}
 
interface ITouchStartListenerState {
  element: HTMLDivElement;
  renderingEngineId: string;
  viewportId: string;
  startPointsList: ITouchPoints[];
  lastPointsList: ITouchPoints[];
 
  // only trigger one touch event in the case the user puts down multiple fingers
  isTouchStart: boolean;
  startTime: Date;
 
  // handle long press
  pressTimeout: ReturnType<typeof setTimeout>;
  pressDelay: number;
  pressMaxDistance: number;
  accumulatedDistance: IDistance;
 
  // handle swipes
  swipeDistanceThreshold: number;
  swiped: boolean;
  swipeToleranceMs: number;
}
 
const zeroIPoint: IPoints = {
  page: [0, 0],
  client: [0, 0],
  canvas: [0, 0],
  world: [0, 0, 0],
};
 
const zeroIDistance: IDistance = {
  page: 0,
  client: 0,
  canvas: 0,
  world: 0,
};
 
// STATE
const defaultState: ITouchStartListenerState = {
  renderingEngineId: undefined,
  viewportId: undefined,
  element: null,
  startPointsList: [
    {
      ...zeroIPoint,
      touch: null,
    },
  ],
  lastPointsList: [
    {
      ...zeroIPoint,
      touch: null,
    },
  ],
  isTouchStart: false,
  startTime: null,
 
  pressTimeout: null,
  pressDelay: 700,
  pressMaxDistance: 5,
  accumulatedDistance: zeroIDistance,
 
  swipeDistanceThreshold: 48,
  swiped: false,
  swipeToleranceMs: 300, // user has 300ms to swipe after touch start or no swipe will trigger
};
 
// TODO: these values should be configurable to handle different use cases such
// as pen, left/right handed, index finger vs thumb, etc. These current values
// assume thumb usage for single finger and index/middle finger for two finger
// gestures in an attempt to cover the 90% use case.
const defaultTapState: ITouchTapListenerState = {
  renderingEngineId: undefined,
  viewportId: undefined,
  element: null,
  startPointsList: [
    {
      ...zeroIPoint,
      touch: null,
    },
  ],
  taps: 0,
  tapTimeout: null,
  tapMaxDistance: 24,
  tapToleranceMs: 300,
};
 
let state: ITouchStartListenerState = JSON.parse(JSON.stringify(defaultState));
let tapState: ITouchTapListenerState = JSON.parse(
  JSON.stringify(defaultTapState)
);
 
function triggerEventCallback(ele, name, eventDetail) {
  return triggerEvent(ele, name, eventDetail);
}
 
/**
 * Listens to touch events from the DOM (touchstart, touchmove, touchend)
 * and depending on interaction and further interaction can emit the
 * following touch events:
 *
 * - TOUCH_START
 * - TOUCH_START_ACTIVATE
 * - TOUCH_PRESS
 * - TOUCH_DRAG (move while down)
 * - TOUCH_SWIPE
 * - TOUCH_END (also an end for multi touch)
 *
 * - TOUCH_TAP
 *
 * @param evt - The Touch event (touchstart).
 * @private
 */
function touchStartListener(evt: TouchEvent) {
  // if a user adds an extra finger when a touch/multi touch has already started
  // don't trigger another touch.
  state.element = <HTMLDivElement>evt.currentTarget;
  const enabledElement = getEnabledElement(state.element);
  const { renderingEngineId, viewportId } = enabledElement;
  state.renderingEngineId = renderingEngineId;
  state.viewportId = viewportId;
  // this prevents multiple start firing
  if (state.isTouchStart) {
    return;
  }
  // this will clear on touchstart and touchend
  clearTimeout(state.pressTimeout);
  state.pressTimeout = setTimeout(() => _onTouchPress(evt), state.pressDelay);
 
  _onTouchStart(evt);
  document.addEventListener('touchmove', _onTouchDrag); // also checks for swipe
  document.addEventListener('touchend', _onTouchEnd); // also checks for tap
}
 
/**
 * _onTouchPress - Handle emission of touchstart events which are held down for a longer
 * period of time
 *
 * @private
 * @param evt - The touch event (touchstart)
 */
function _onTouchPress(evt: TouchEvent) {
  const totalDistance = state.accumulatedDistance.canvas;
  if (totalDistance > state.pressMaxDistance) {
    return;
  }
  const eventDetail: EventTypes.TouchPressEventDetail = {
    event: evt, // touchstart native event
    eventName: TOUCH_PRESS,
    renderingEngineId: state.renderingEngineId,
    viewportId: state.viewportId,
    camera: {},
    element: state.element,
    startPointsList: copyPointsList(state.startPointsList),
    lastPointsList: copyPointsList(state.lastPointsList),
    startPoints: copyPoints(getMeanTouchPoints(state.startPointsList)),
    lastPoints: copyPoints(getMeanTouchPoints(state.lastPointsList)),
  };
  triggerEventCallback(eventDetail.element, TOUCH_PRESS, eventDetail);
}
 
/**
 * _onTouchStart - Handle emission of touchstart events.
 *
 * @private
 * @param evt - The touch event (touchstart)
 */
function _onTouchStart(evt: TouchEvent) {
  state.isTouchStart = true;
  state.startTime = new Date();
  const startPointsList = getTouchEventPoints(evt, state.element);
  const startPoints = getMeanTouchPoints(startPointsList);
  const deltaPoints = zeroIPoint;
  const deltaDistance = zeroIDistance;
  // deltaRotation same as deltaDistance but values are theta
  const eventDetail: EventTypes.TouchStartEventDetail = {
    event: evt,
    eventName: TOUCH_START,
    element: state.element,
    renderingEngineId: state.renderingEngineId,
    viewportId: state.viewportId,
    camera: {},
    startPointsList: startPointsList,
    lastPointsList: startPointsList,
    currentPointsList: startPointsList,
    startPoints: startPoints,
    lastPoints: startPoints,
    currentPoints: startPoints,
    deltaPoints,
    deltaDistance,
    // deltaRotation
  };
 
  state.startPointsList = copyPointsList(eventDetail.startPointsList);
  state.lastPointsList = copyPointsList(eventDetail.lastPointsList);
  // by triggering TOUCH_START it checks if this is toolSelection, handle modification etc.
  // of already existing tools
  const eventDidPropagate = triggerEventCallback(
    eventDetail.element,
    TOUCH_START,
    eventDetail
  );
 
  // if no tools responded to this event and prevented its default propagation behavior,
  // create a new tool
  if (eventDidPropagate) {
    triggerEventCallback(
      eventDetail.element,
      TOUCH_START_ACTIVATE,
      eventDetail
    );
  }
}
 
/**
 * _onTouchDrag - Handle emission of drag events whilst the touch is depressed.
 *
 * @private
 * @param evt - The touch event (touchmove)
 */
function _onTouchDrag(evt: TouchEvent) {
  const currentPointsList = getTouchEventPoints(evt, state.element);
  const lastPointsList = _updateTouchEventsLastPoints(
    state.element,
    state.lastPointsList
  );
 
  const deltaPoints =
    currentPointsList.length === lastPointsList.length
      ? getDeltaPoints(currentPointsList, lastPointsList)
      : zeroIPoint;
 
  const deltaDistance =
    currentPointsList.length === lastPointsList.length
      ? getDeltaDistanceBetweenIPoints(currentPointsList, lastPointsList)
      : zeroIDistance;
 
  const totalDistance =
    currentPointsList.length === lastPointsList.length
      ? getDeltaDistance(currentPointsList, state.lastPointsList)
      : zeroIDistance;
 
  state.accumulatedDistance = {
    page: state.accumulatedDistance.page + totalDistance.page,
    client: state.accumulatedDistance.client + totalDistance.client,
    canvas: state.accumulatedDistance.canvas + totalDistance.canvas,
    world: state.accumulatedDistance.world + totalDistance.world,
  };
 
  /**
   * this is can be uncommented to make dragging smoother. In the future, these values
   * should be in a configuration file. There may also need to be different
   * profiles for left handed and right handed thumb use. These values
   * are currently optimized for left handed use.
   *
   * const clamp = (num) => Math.min(Math.max(num, -15), 10);
   * const deltaDistanceClamped = \{
   *     page: clamp(deltaDistance.page),
   *     client: clamp(deltaDistance.client),
   *     canvas: clamp(deltaDistance.canvas),
   *     world: clamp(deltaDistance.world),
   * \};
   */
 
  const eventDetail: EventTypes.TouchDragEventDetail = {
    event: evt,
    eventName: TOUCH_DRAG,
    renderingEngineId: state.renderingEngineId,
    viewportId: state.viewportId,
    camera: {},
    element: state.element,
    startPoints: getMeanTouchPoints(state.startPointsList),
    lastPoints: getMeanTouchPoints(lastPointsList),
    currentPoints: getMeanTouchPoints(currentPointsList),
    startPointsList: copyPointsList(state.startPointsList),
    lastPointsList: copyPointsList(lastPointsList),
    currentPointsList,
    deltaPoints: deltaPoints,
    deltaDistance: deltaDistance,
  };
 
  triggerEventCallback(state.element, TOUCH_DRAG, eventDetail);
 
  // check for swipe events
  _checkTouchSwipe(evt, deltaPoints);
 
  // Update the last points
  state.lastPointsList = copyPointsList(currentPointsList);
}
 
/**
 * _onTouchEnd - Handle emission of touch end events
 *
 * @private
 * @param evt - The touch event.
 */
function _onTouchEnd(evt: TouchEvent): void {
  // in case it was a tap event we don't want to fire the cornerstone normalized
  // touch end event if the touch start never happend
  clearTimeout(state.pressTimeout);
  const currentPointsList = getTouchEventPoints(evt, state.element);
  const lastPointsList = _updateTouchEventsLastPoints(
    state.element,
    state.lastPointsList
  );
  const deltaPoints =
    currentPointsList.length === lastPointsList.length
      ? getDeltaPoints(currentPointsList, lastPointsList)
      : getDeltaPoints(currentPointsList, currentPointsList);
  const deltaDistance =
    currentPointsList.length === lastPointsList.length
      ? getDeltaDistanceBetweenIPoints(currentPointsList, lastPointsList)
      : getDeltaDistanceBetweenIPoints(currentPointsList, currentPointsList);
  const eventDetail: EventTypes.TouchEndEventDetail = {
    event: evt,
    eventName: TOUCH_END,
    element: state.element,
    renderingEngineId: state.renderingEngineId,
    viewportId: state.viewportId,
    camera: {},
    startPointsList: copyPointsList(state.startPointsList),
    lastPointsList: copyPointsList(lastPointsList),
    currentPointsList,
    startPoints: getMeanTouchPoints(state.startPointsList),
    lastPoints: getMeanTouchPoints(lastPointsList),
    currentPoints: getMeanTouchPoints(currentPointsList),
    deltaPoints,
    deltaDistance,
  };
 
  triggerEventCallback(eventDetail.element, TOUCH_END, eventDetail);
  _checkTouchTap(evt);
 
  // reset to default state
  state = JSON.parse(JSON.stringify(defaultState));
  document.removeEventListener('touchmove', _onTouchDrag);
  document.removeEventListener('touchend', _onTouchEnd);
}
 
function _checkTouchTap(evt: TouchEvent): void {
  const currentTime = new Date().getTime();
  const startTime = state.startTime.getTime();
  if (currentTime - startTime > tapState.tapToleranceMs) {
    return;
  }
 
  // first tap, initialize the state
  if (tapState.taps === 0) {
    tapState.element = state.element;
    tapState.renderingEngineId = state.renderingEngineId;
    tapState.viewportId = state.viewportId;
    tapState.startPointsList = state.startPointsList;
  }
 
  // subsequent tap is on a different element
  if (
    tapState.taps > 0 &&
    !(
      tapState.element == state.element &&
      tapState.renderingEngineId == state.renderingEngineId &&
      tapState.viewportId == state.viewportId
    )
  ) {
    return;
  }
 
  const currentPointsList = getTouchEventPoints(evt, tapState.element);
  const distanceFromStart = getDeltaDistance(
    currentPointsList,
    tapState.startPointsList
  ).canvas;
 
  // if the tap is too far from starting tap, we can ignore it.
  // TODO: in the case the user means to tap in two separate areas within the
  // tapTolerance (300ms), the second tap will not trigger. This is because it
  // is ignored below for simplicity to track multiple taps (double, triple etc)
  // in order to support two separate single taps that occur < 300ms on the
  // screen. One can create the concept of "TapChains". Our current implementation
  // only supports a single tap chain on the screen. You can think of it as a
  // region where the user has the option to perform unlimited multitaps as long
  // as they are < the tapToleranceMs value. So a tap somewhere else on the screen
  // that is > the tapMaxDistance will start a separate and new "TapChain".
  if (distanceFromStart > tapState.tapMaxDistance) {
    return;
  }
 
  clearTimeout(tapState.tapTimeout);
  tapState.taps += 1;
 
  tapState.tapTimeout = setTimeout(() => {
    const eventDetail: EventTypes.TouchTapEventDetail = {
      event: evt,
      eventName: TOUCH_TAP,
      element: tapState.element,
      renderingEngineId: tapState.renderingEngineId,
      viewportId: tapState.viewportId,
      camera: {},
      currentPointsList,
      currentPoints: getMeanTouchPoints(currentPointsList),
      taps: tapState.taps,
    };
    triggerEventCallback(eventDetail.element, TOUCH_TAP, eventDetail);
    tapState = JSON.parse(JSON.stringify(defaultTapState));
  }, tapState.tapToleranceMs);
}
 
function _checkTouchSwipe(evt: TouchEvent, deltaPoints: IPoints) {
  const currentTime = new Date().getTime();
  const startTime = state.startTime.getTime();
  if (state.swiped || currentTime - startTime > state.swipeToleranceMs) {
    return;
  }
  const [x, y] = deltaPoints.canvas;
  const eventDetail: EventTypes.TouchSwipeEventDetail = {
    event: evt,
    eventName: TOUCH_SWIPE,
    renderingEngineId: state.renderingEngineId,
    viewportId: state.viewportId,
    camera: {},
    element: state.element,
    swipe: null,
  };
  if (Math.abs(x) > state.swipeDistanceThreshold) {
    eventDetail.swipe = x > 0 ? Swipe.RIGHT : Swipe.LEFT;
    triggerEventCallback(eventDetail.element, TOUCH_SWIPE, eventDetail);
    state.swiped = true;
  }
 
  if (Math.abs(y) > state.swipeDistanceThreshold) {
    eventDetail.swipe = y > 0 ? Swipe.DOWN : Swipe.UP;
    triggerEventCallback(eventDetail.element, TOUCH_SWIPE, eventDetail);
    state.swiped = true;
  }
}
 
/**
 * Recalculates the last world coordinate, as the linear transform from client
 * to world could be different if the camera was updated.
 * @param element - The HTML element
 * @param lastPoints - The last points
 */
function _updateTouchEventsLastPoints(
  element: HTMLDivElement,
  lastPoints: ITouchPoints[]
): ITouchPoints[] {
  const { viewport } = getEnabledElement(element);
  // Need to update the world point to be calculated from the current reference frame,
  // Which might have changed since the last interaction.
  return lastPoints.map((lp) => {
    const world = viewport.canvasToWorld(lp.canvas);
    return {
      page: lp.page,
      client: lp.client,
      canvas: lp.canvas,
      world,
      touch: lp.touch,
    };
  });
}
 
export default touchStartListener;