function playClip(element, framesPerSecond) {
  var playClipData = void 0;
  var playClipTimeouts = void 0;

  if (element === undefined) {
    throw new Error('playClip: element must not be undefined');
  }

  var stackToolData = (0, _toolState.getToolState)(element, 'stack');

  if (!stackToolData || !stackToolData.data || !stackToolData.data.length) {
    return;
  }

  var cornerstone = _externalModules2.default.cornerstone;
  // If we have more than one stack, check if we have a stack renderer defined
  var stackRenderer = void 0;

  if (stackToolData.data.length > 1) {
    var stackRendererData = (0, _toolState.getToolState)(element, 'stackRenderer');

    if (stackRendererData && stackRendererData.data && stackRendererData.data.length) {
      stackRenderer = stackRendererData.data[0];
    }
  }

  var stackData = stackToolData.data[0];

  var playClipToolData = (0, _toolState.getToolState)(element, toolType);

  if (!playClipToolData || !playClipToolData.data || !playClipToolData.data.length) {
    playClipData = {
      intervalId: undefined,
      framesPerSecond: 30,
      lastFrameTimeStamp: undefined,
      frameRate: 0,
      frameTimeVector: undefined,
      ignoreFrameTimeVector: false,
      usingFrameTimeVector: false,
      speed: 1,
      reverse: false,
      loop: true
    };
    (0, _toolState.addToolState)(element, toolType, playClipData);
  } else {
    playClipData = playClipToolData.data[0];
    // Make sure the specified clip is not running before any property update
    stopClipWithData(playClipData);
  }

  // If a framesPerSecond is specified and is valid, update the playClipData now
  if (framesPerSecond < 0 || framesPerSecond > 0) {
    playClipData.framesPerSecond = Number(framesPerSecond);
    playClipData.reverse = playClipData.framesPerSecond < 0;
    // If framesPerSecond is given, frameTimeVector will be ignored...
    playClipData.ignoreFrameTimeVector = true;
  }

  // Determine if frame time vector should be used instead of a fixed frame rate...
  if (playClipData.ignoreFrameTimeVector !== true && playClipData.frameTimeVector && playClipData.frameTimeVector.length === stackData.imageIds.length) {
    playClipTimeouts = getPlayClipTimeouts(playClipData.frameTimeVector, playClipData.speed);
  }

  // This function encapsulates the frame rendering logic...
  var playClipAction = function playClipAction() {

    // Hoisting of context variables
    var loader = void 0,
        startLoadingHandler = void 0,
        endLoadingHandler = void 0,
        errorLoadingHandler = void 0,
        newImageIdIndex = stackData.currentImageIdIndex;

    var imageCount = stackData.imageIds.length;

    if (playClipData.reverse) {
      newImageIdIndex--;
    } else {
      newImageIdIndex++;
    }

    if (!playClipData.loop && (newImageIdIndex < 0 || newImageIdIndex >= imageCount)) {
      stopClipWithData(playClipData);
      triggerStopEvent(element);

      return;
    }

    // Loop around if we go outside the stack
    if (newImageIdIndex >= imageCount) {
      newImageIdIndex = 0;
    }

    if (newImageIdIndex < 0) {
      newImageIdIndex = imageCount - 1;
    }

    if (newImageIdIndex !== stackData.currentImageIdIndex) {

      startLoadingHandler = _loadHandlerManager2.default.getStartLoadHandler();
      endLoadingHandler = _loadHandlerManager2.default.getEndLoadHandler();
      errorLoadingHandler = _loadHandlerManager2.default.getErrorLoadingHandler();

      if (startLoadingHandler) {
        startLoadingHandler(element);
      }

      if (stackData.preventCache === true) {
        loader = cornerstone.loadImage(stackData.imageIds[newImageIdIndex]);
      } else {
        loader = cornerstone.loadAndCacheImage(stackData.imageIds[newImageIdIndex]);
      }

      loader.then(function (image) {
        try {
          stackData.currentImageIdIndex = newImageIdIndex;
          if (stackRenderer) {
            stackRenderer.currentImageIdIndex = newImageIdIndex;
            stackRenderer.render(element, stackToolData.data);
          } else {
            cornerstone.displayImage(element, image);
          }
          if (endLoadingHandler) {
            endLoadingHandler(element, image);
          }
        } catch (error) {
          return;
        }
      }, function (error) {
        var imageId = stackData.imageIds[newImageIdIndex];

        if (errorLoadingHandler) {
          errorLoadingHandler(element, imageId, error);
        }
      });
    }
  };

  // If playClipTimeouts array is available, not empty and its elements are NOT uniform ...
  // ... (at least one timeout is different from the others), use alternate setTimeout implementation
  if (playClipTimeouts && playClipTimeouts.length > 0 && playClipTimeouts.isTimeVarying) {
    playClipData.usingFrameTimeVector = true;
    playClipData.intervalId = setTimeout(function playClipTimeoutHandler() {
      playClipData.intervalId = setTimeout(playClipTimeoutHandler, playClipTimeouts[stackData.currentImageIdIndex]);
      playClipAction();
    }, 0);
  } else {
    // ... otherwise user setInterval implementation which is much more efficient.
    playClipData.usingFrameTimeVector = false;
    playClipData.intervalId = setInterval(playClipAction, 1000 / Math.abs(playClipData.framesPerSecond));
  }
}
/**
 * Starts playing a clip or adjusts the frame rate of an already playing clip.  framesPerSecond is
 * optional and defaults to 30 if not specified.  A negative framesPerSecond will play the clip in reverse.
 * The element must be a stack of images
 * @param element
 * @param framesPerSecond
 */
Starts playing a clip or adjusts the frame rate of an already playing clip.  framesPerSecond is
optional and defaults to 30 if not specified.  A negative framesPerSecond will play the clip in reverse.
The element must be a stack of images
@param element
@param framesPerSecond
Starts playing a clip or adjusts the frame rate of an already playing clip . framesPerSecond is optional and defaults to 30 if not specified . A negative framesPerSecond will play the clip in reverse . The element must be a stack of images @param element @param framesPerSecond
function playClip(element, framesPerSecond) {
  var playClipData = void 0;
  var playClipTimeouts = void 0;
  if (element === undefined) {
    throw new Error('playClip: element must not be undefined');
  }
  var stackToolData = (0, _toolState.getToolState)(element, 'stack');
  if (!stackToolData || !stackToolData.data || !stackToolData.data.length) {
    return;
  }
  var cornerstone = _externalModules2.default.cornerstone;
  var stackRenderer = void 0;
  if (stackToolData.data.length > 1) {
    var stackRendererData = (0, _toolState.getToolState)(element, 'stackRenderer');
    if (stackRendererData && stackRendererData.data && stackRendererData.data.length) {
      stackRenderer = stackRendererData.data[0];
    }
  }
  var stackData = stackToolData.data[0];
  var playClipToolData = (0, _toolState.getToolState)(element, toolType);
  if (!playClipToolData || !playClipToolData.data || !playClipToolData.data.length) {
    playClipData = {
      intervalId: undefined,
      framesPerSecond: 30,
      lastFrameTimeStamp: undefined,
      frameRate: 0,
      frameTimeVector: undefined,
      ignoreFrameTimeVector: false,
      usingFrameTimeVector: false,
      speed: 1,
      reverse: false,
      loop: true
    };
    (0, _toolState.addToolState)(element, toolType, playClipData);
  } else {
    playClipData = playClipToolData.data[0];
    stopClipWithData(playClipData);
  }
  if (framesPerSecond < 0 || framesPerSecond > 0) {
    playClipData.framesPerSecond = Number(framesPerSecond);
    playClipData.reverse = playClipData.framesPerSecond < 0;
    playClipData.ignoreFrameTimeVector = true;
  }
  if (playClipData.ignoreFrameTimeVector !== true && playClipData.frameTimeVector && playClipData.frameTimeVector.length === stackData.imageIds.length) {
    playClipTimeouts = getPlayClipTimeouts(playClipData.frameTimeVector, playClipData.speed);
  }
  var playClipAction = function playClipAction() {
    var loader = void 0,
        startLoadingHandler = void 0,
        endLoadingHandler = void 0,
        errorLoadingHandler = void 0,
        newImageIdIndex = stackData.currentImageIdIndex;
    var imageCount = stackData.imageIds.length;
    if (playClipData.reverse) {
      newImageIdIndex--;
    } else {
      newImageIdIndex++;
    }
    if (!playClipData.loop && (newImageIdIndex < 0 || newImageIdIndex >= imageCount)) {
      stopClipWithData(playClipData);
      triggerStopEvent(element);
      return;
    }
    if (newImageIdIndex >= imageCount) {
      newImageIdIndex = 0;
    }
    if (newImageIdIndex < 0) {
      newImageIdIndex = imageCount - 1;
    }
    if (newImageIdIndex !== stackData.currentImageIdIndex) {
      startLoadingHandler = _loadHandlerManager2.default.getStartLoadHandler();
      endLoadingHandler = _loadHandlerManager2.default.getEndLoadHandler();
      errorLoadingHandler = _loadHandlerManager2.default.getErrorLoadingHandler();
      if (startLoadingHandler) {
        startLoadingHandler(element);
      }
      if (stackData.preventCache === true) {
        loader = cornerstone.loadImage(stackData.imageIds[newImageIdIndex]);
      } else {
        loader = cornerstone.loadAndCacheImage(stackData.imageIds[newImageIdIndex]);
      }
      loader.then(function (image) {
        try {
          stackData.currentImageIdIndex = newImageIdIndex;
          if (stackRenderer) {
            stackRenderer.currentImageIdIndex = newImageIdIndex;
            stackRenderer.render(element, stackToolData.data);
          } else {
            cornerstone.displayImage(element, image);
          }
          if (endLoadingHandler) {
            endLoadingHandler(element, image);
          }
        } catch (error) {
          return;
        }
      }, function (error) {
        var imageId = stackData.imageIds[newImageIdIndex];
        if (errorLoadingHandler) {
          errorLoadingHandler(element, imageId, error);
        }
      });
    }
  };
  if (playClipTimeouts && playClipTimeouts.length > 0 && playClipTimeouts.isTimeVarying) {
    playClipData.usingFrameTimeVector = true;
    playClipData.intervalId = setTimeout(function playClipTimeoutHandler() {
      playClipData.intervalId = setTimeout(playClipTimeoutHandler, playClipTimeouts[stackData.currentImageIdIndex]);
      playClipAction();
    }, 0);
  } else {
    playClipData.usingFrameTimeVector = false;
    playClipData.intervalId = setInterval(playClipAction, 1000 / Math.abs(playClipData.framesPerSecond));
  }
}
function playClip ( element , framesPerSecond ) { var playClipData = void 0 ; var playClipTimeouts = void 0 ; if ( element === undefined ) { throw new Error ( 'playClip: element must not be undefined' ) ; } var stackToolData = ( 0 , _toolState . getToolState ) ( element , 'stack' ) ; if ( ! stackToolData || ! stackToolData . data || ! stackToolData . data . length ) { return ; } var cornerstone = _externalModules2 . default . cornerstone ; var stackRenderer = void 0 ; if ( stackToolData . data . length > 1 ) { var stackRendererData = ( 0 , _toolState . getToolState ) ( element , 'stackRenderer' ) ; if ( stackRendererData && stackRendererData . data && stackRendererData . data . length ) { stackRenderer = stackRendererData . data [ 0 ] ; } } var stackData = stackToolData . data [ 0 ] ; var playClipToolData = ( 0 , _toolState . getToolState ) ( element , toolType ) ; if ( ! playClipToolData || ! playClipToolData . data || ! playClipToolData . data . length ) { playClipData = { intervalId : undefined , framesPerSecond : 30 , lastFrameTimeStamp : undefined , frameRate : 0 , frameTimeVector : undefined , ignoreFrameTimeVector : false , usingFrameTimeVector : false , speed : 1 , reverse : false , loop : true } ; ( 0 , _toolState . addToolState ) ( element , toolType , playClipData ) ; } else { playClipData = playClipToolData . data [ 0 ] ; stopClipWithData ( playClipData ) ; } if ( framesPerSecond < 0 || framesPerSecond > 0 ) { playClipData . framesPerSecond = Number ( framesPerSecond ) ; playClipData . reverse = playClipData . framesPerSecond < 0 ; playClipData . ignoreFrameTimeVector = true ; } if ( playClipData . ignoreFrameTimeVector !== true && playClipData . frameTimeVector && playClipData . frameTimeVector . length === stackData . imageIds . length ) { playClipTimeouts = getPlayClipTimeouts ( playClipData . frameTimeVector , playClipData . speed ) ; } var playClipAction = function playClipAction ( ) { var loader = void 0 , startLoadingHandler = void 0 , endLoadingHandler = void 0 , errorLoadingHandler = void 0 , newImageIdIndex = stackData . currentImageIdIndex ; var imageCount = stackData . imageIds . length ; if ( playClipData . reverse ) { newImageIdIndex -- ; } else { newImageIdIndex ++ ; } if ( ! playClipData . loop && ( newImageIdIndex < 0 || newImageIdIndex >= imageCount ) ) { stopClipWithData ( playClipData ) ; triggerStopEvent ( element ) ; return ; } if ( newImageIdIndex >= imageCount ) { newImageIdIndex = 0 ; } if ( newImageIdIndex < 0 ) { newImageIdIndex = imageCount - 1 ; } if ( newImageIdIndex !== stackData . currentImageIdIndex ) { startLoadingHandler = _loadHandlerManager2 . default . getStartLoadHandler ( ) ; endLoadingHandler = _loadHandlerManager2 . default . getEndLoadHandler ( ) ; errorLoadingHandler = _loadHandlerManager2 . default . getErrorLoadingHandler ( ) ; if ( startLoadingHandler ) { startLoadingHandler ( element ) ; } if ( stackData . preventCache === true ) { loader = cornerstone . loadImage ( stackData . imageIds [ newImageIdIndex ] ) ; } else { loader = cornerstone . loadAndCacheImage ( stackData . imageIds [ newImageIdIndex ] ) ; } loader . then ( function ( image ) { try { stackData . currentImageIdIndex = newImageIdIndex ; if ( stackRenderer ) { stackRenderer . currentImageIdIndex = newImageIdIndex ; stackRenderer . render ( element , stackToolData . data ) ; } else { cornerstone . displayImage ( element , image ) ; } if ( endLoadingHandler ) { endLoadingHandler ( element , image ) ; } } catch ( error ) { return ; } } , function ( error ) { var imageId = stackData . imageIds [ newImageIdIndex ] ; if ( errorLoadingHandler ) { errorLoadingHandler ( element , imageId , error ) ; } } ) ; } } ; if ( playClipTimeouts && playClipTimeouts . length > 0 && playClipTimeouts . isTimeVarying ) { playClipData . usingFrameTimeVector = true ; playClipData . intervalId = setTimeout ( function playClipTimeoutHandler ( ) { playClipData . intervalId = setTimeout ( playClipTimeoutHandler , playClipTimeouts [ stackData . currentImageIdIndex ] ) ; playClipAction ( ) ; } , 0 ) ; } else { playClipData . usingFrameTimeVector = false ; playClipData . intervalId = setInterval ( playClipAction , 1000 / Math . abs ( playClipData . framesPerSecond ) ) ; } }
Starts playing a clip or adjusts the frame rate of an already playing clip.
Starts playing a clip or adjusts the frame rate of an already playing clip .
// If we have more than one stack, check if we have a stack renderer defined // Make sure the specified clip is not running before any property update // If a framesPerSecond is specified and is valid, update the playClipData now // If framesPerSecond is given, frameTimeVector will be ignored... // Determine if frame time vector should be used instead of a fixed frame rate... // This function encapsulates the frame rendering logic... // Hoisting of context variables // Loop around if we go outside the stack // If playClipTimeouts array is available, not empty and its elements are NOT uniform ... // ... (at least one timeout is different from the others), use alternate setTimeout implementation // ... otherwise user setInterval implementation which is much more efficient.