Home | History | Annotate | Download | only in animations
      1 <!--
      2 @license
      3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
      4 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
      5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
      6 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
      7 Code distributed by Google as part of the polymer project is also
      8 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
      9 -->
     10 
     11 <link rel="import" href="../../polymer/polymer.html">
     12 <link rel="import" href="../neon-shared-element-animation-behavior.html">
     13 <link rel="import" href="../web-animations.html">
     14 
     15 <!--
     16 `<hero-animation>` is a shared element animation that scales and transform an element such that it
     17 appears to be shared between two pages. Use this in `<neon-animated-pages>`. The source page
     18 should use this animation in an 'exit' animation and set the `fromPage` configuration property to
     19 itself, and the destination page should use this animation in an `entry` animation and set the
     20 `toPage` configuration property to itself. They should also define the hero elements in the
     21 `sharedElements` property (not a configuration property, see
     22 `Polymer.NeonSharedElementAnimatableBehavior`).
     23 
     24 Configuration:
     25 ```
     26 {
     27   name: 'hero-animation',
     28   id: <shared-element-id>,
     29   timing: <animation-timing>,
     30   toPage: <node>, /* define for the destination page */
     31   fromPage: <node>, /* define for the source page */
     32 }
     33 ```
     34 -->
     35 
     36 <script>
     37 
     38   Polymer({
     39 
     40     is: 'hero-animation',
     41 
     42     behaviors: [
     43       Polymer.NeonSharedElementAnimationBehavior
     44     ],
     45 
     46     configure: function(config) {
     47       var shared = this.findSharedElements(config);
     48       if (!shared) {
     49         return;
     50       }
     51 
     52       var fromRect = shared.from.getBoundingClientRect();
     53       var toRect = shared.to.getBoundingClientRect();
     54 
     55       var deltaLeft = fromRect.left - toRect.left;
     56       var deltaTop = fromRect.top - toRect.top;
     57       var deltaWidth = fromRect.width / toRect.width;
     58       var deltaHeight = fromRect.height / toRect.height;
     59 
     60       this._effect = new KeyframeEffect(shared.to, [
     61         {'transform': 'translate(' + deltaLeft + 'px,' + deltaTop + 'px) scale(' + deltaWidth + ',' + deltaHeight + ')'},
     62         {'transform': 'none'}
     63       ], this.timingFromConfig(config));
     64 
     65       this.setPrefixedProperty(shared.to, 'transformOrigin', '0 0');
     66       shared.to.style.zIndex = 10000;
     67       shared.from.style.visibility = 'hidden';
     68 
     69       return this._effect;
     70     },
     71 
     72     complete: function(config) {
     73       var shared = this.findSharedElements(config);
     74       if (!shared) {
     75         return null;
     76       }
     77       shared.to.style.zIndex = '';
     78       shared.from.style.visibility = '';
     79     }
     80 
     81   });
     82 
     83 </script>
     84