1 // Copyright 2013 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.
5 // This file provides the PinchAction object, which zooms into or out of a
6 // page by a given scale factor:
7 // 1. var action = new __PinchAction(callback)
8 // 2. action.start(pinch_options)
13 function PinchGestureOptions(opt_options) {
15 this.element_ = opt_options.element;
16 this.left_anchor_ratio_ = opt_options.left_anchor_ratio;
17 this.top_anchor_ratio_ = opt_options.top_anchor_ratio;
18 this.scale_factor_ = opt_options.scale_factor;
19 this.speed_ = opt_options.speed;
21 this.element_ = document.body;
22 this.left_anchor_ratio_ = 0.5;
23 this.top_anchor_ratio_ = 0.5;
24 this.scale_factor_ = 2.0;
29 function supportedByBrowser() {
30 return !!(window.chrome &&
31 chrome.gpuBenchmarking &&
32 chrome.gpuBenchmarking.pinchBy &&
33 chrome.gpuBenchmarking.visualViewportHeight &&
34 chrome.gpuBenchmarking.visualViewportWidth);
37 // This class zooms into or out of a page, given a number of pixels for
38 // the synthetic pinch gesture to cover.
39 function PinchAction(opt_callback) {
42 this.beginMeasuringHook = function() {};
43 this.endMeasuringHook = function() {};
45 this.callback_ = opt_callback;
48 PinchAction.prototype.start = function(opt_options) {
49 this.options_ = new PinchGestureOptions(opt_options);
51 requestAnimationFrame(this.startPass_.bind(this));
54 PinchAction.prototype.startPass_ = function() {
55 this.beginMeasuringHook();
57 var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
59 rect.left + rect.width * this.options_.left_anchor_ratio_;
61 rect.top + rect.height * this.options_.top_anchor_ratio_;
62 chrome.gpuBenchmarking.pinchBy(this.options_.scale_factor_,
63 anchor_left, anchor_top,
64 this.onGestureComplete_.bind(this),
65 this.options_.speed_);
68 PinchAction.prototype.onGestureComplete_ = function() {
69 this.endMeasuringHook();
75 window.__PinchAction = PinchAction;
76 window.__PinchAction_SupportedByBrowser = supportedByBrowser;