1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 * The period of time in milliseconds to wait between updating the viewport
10 * position by the scroll velocity.
12 ViewportScroller.DRAG_TIMER_INTERVAL_MS_ = 100;
16 * The maximum drag scroll distance per DRAG_TIMER_INTERVAL in pixels.
18 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_ = 100;
21 * Creates a new ViewportScroller.
22 * A ViewportScroller scrolls the page in response to drag selection with the
24 * @param {Object} viewport The viewport info of the page.
25 * @param {Object} plugin The PDF plugin element.
26 * @param {Object} window The window containing the viewer.
28 function ViewportScroller(viewport, plugin, window) {
29 this.viewport_ = viewport;
30 this.plugin_ = plugin;
31 this.window_ = window;
32 this.mousemoveCallback_ = null;
34 this.scrollVelocity_ = null;
35 this.lastFrameTime_ = 0;
38 ViewportScroller.prototype = {
41 * Start scrolling the page by |scrollVelocity_| every
42 * |DRAG_TIMER_INTERVAL_MS_|.
44 startDragScrollTimer_: function() {
45 if (this.timerId_ === null) {
47 this.window_.setInterval(this.dragScrollPage_.bind(this),
48 ViewportScroller.DRAG_TIMER_INTERVAL_MS_);
49 this.lastFrameTime_ = Date.now();
55 * Stops the drag scroll timer if it is active.
57 stopDragScrollTimer_: function() {
58 if (this.timerId_ !== null) {
59 this.window_.clearInterval(this.timerId_);
61 this.lastFrameTime_ = 0;
67 * Scrolls the viewport by the current scroll velocity.
69 dragScrollPage_: function() {
70 var position = this.viewport_.position;
71 var currentFrameTime = Date.now();
72 var timeAdjustment = (currentFrameTime - this.lastFrameTime_) /
73 ViewportScroller.DRAG_TIMER_INTERVAL_MS_;
74 position.y += (this.scrollVelocity_.y * timeAdjustment);
75 position.x += (this.scrollVelocity_.x * timeAdjustment);
76 this.viewport_.position = position;
77 this.lastFrameTime_ = currentFrameTime;
82 * Calculate the velocity to scroll while dragging using the distance of the
83 * cursor outside the viewport.
84 * @param {Object} event The mousemove event.
85 * @return {Object} Object with x and y direction scroll velocity.
87 calculateVelocity_: function(event) {
88 var x = Math.min(Math.max(-event.offsetX,
89 event.offsetX - this.plugin_.offsetWidth, 0),
90 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) *
91 Math.sign(event.offsetX);
92 var y = Math.min(Math.max(-event.offsetY,
93 event.offsetY - this.plugin_.offsetHeight, 0),
94 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) *
95 Math.sign(event.offsetY);
104 * Handles mousemove events. It updates the scroll velocity and starts and
105 * stops timer based on scroll velocity.
106 * @param {Object} event The mousemove event.
108 onMousemove_: function(event) {
109 this.scrollVelocity_ = this.calculateVelocity_(event);
110 if (!this.scrollVelocity_.x && !this.scrollVelocity_.y)
111 this.stopDragScrollTimer_();
112 else if (!this.timerId_)
113 this.startDragScrollTimer_();
117 * Sets whether to scroll the viewport when the mouse is outside the
119 * @param {boolean} isSelecting Represents selection status.
121 setEnableScrolling: function(isSelecting) {
123 if (!this.mousemoveCallback_)
124 this.mousemoveCallback_ = this.onMousemove_.bind(this);
125 this.plugin_.addEventListener('mousemove', this.mousemoveCallback_,
128 this.stopDragScrollTimer_();
129 if (this.mousemoveCallback_) {
130 this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_,