Home | History | Annotate | Download | only in actions
      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.
      4 
      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)
      9 'use strict';
     10 
     11 (function() {
     12 
     13   function supportedByBrowser() {
     14     return !!(window.chrome &&
     15               chrome.gpuBenchmarking &&
     16               chrome.gpuBenchmarking.pinchBy);
     17   }
     18 
     19   /**
     20     * Performs a single vertical pinch gesture to zoom in or out, anchored
     21     * in the center of the window.
     22     * Only works if pinchBy gesture is available.
     23     * @constructor
     24     */
     25   function PinchGesture(zoom_in) {
     26     this.zoom_in_ = zoom_in;
     27   };
     28 
     29   PinchGesture.prototype.start = function(pixels_to_move, callback) {
     30     this.callback_ = callback;
     31 
     32     // The anchor point of the gesture is the center of the window.
     33     var anchor_x = window.innerWidth / 2;
     34     var anchor_y = window.innerHeight / 2;
     35 
     36     chrome.gpuBenchmarking.pinchBy(this.zoom_in_, pixels_to_move,
     37                                    anchor_x, anchor_y,
     38                                    function() { callback(); });
     39   };
     40 
     41   // This class zooms into or out of a page, given a number of pixels for
     42   // the synthetic pinch gesture to cover.
     43   function PinchAction(opt_callback) {
     44     var self = this;
     45 
     46     this.beginMeasuringHook = function() {}
     47     this.endMeasuringHook = function() {}
     48 
     49     this.callback_ = opt_callback;
     50   };
     51 
     52   PinchAction.prototype.start = function(zoom_in, pixels_to_move) {
     53     this.zoom_in_ = zoom_in;
     54     this.pixels_to_move_ = pixels_to_move;
     55 
     56     requestAnimationFrame(this.startPass_.bind(this));
     57   };
     58 
     59   PinchAction.prototype.startPass_ = function() {
     60     this.beginMeasuringHook();
     61 
     62     this.gesture_ = new PinchGesture(this.zoom_in_);
     63     this.gesture_.start(this.pixels_to_move_,
     64                         this.onGestureComplete_.bind(this));
     65   };
     66 
     67   PinchAction.prototype.onGestureComplete_ = function() {
     68     this.endMeasuringHook();
     69 
     70     if (this.callback_)
     71       this.callback_();
     72   };
     73 
     74   window.__PinchAction = PinchAction;
     75   window.__PinchAction_SupportedByBrowser = supportedByBrowser;
     76 })();
     77