|
| 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. |
| 4 | + |
| 5 | +import 'package:newton/newton.dart'; |
| 6 | + |
| 7 | +// Base class for creating Simulations for the animation Timeline. |
| 8 | +abstract class Force { |
| 9 | + Simulation release(double position, double velocity); |
| 10 | +} |
| 11 | + |
| 12 | +class SpringForce extends Force { |
| 13 | + SpringForce(this.spring, {this.left: 0.0, this.right: 1.0}); |
| 14 | + |
| 15 | + final SpringDescription spring; |
| 16 | + // Where to put the spring's resting point when releasing left or right, |
| 17 | + // respectively. |
| 18 | + final double left, right; |
| 19 | + |
| 20 | + Simulation release(double position, double velocity) { |
| 21 | + // Target just past the endpoint, because the animation will stop once the |
| 22 | + // Spring gets within the epsilon, and we want to stop at the endpoint. |
| 23 | + double target = velocity < 0.0 ? |
| 24 | + this.left - _kEpsilon : this.right + _kEpsilon; |
| 25 | + return new SpringSimulation(spring, position, target, velocity); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +final SpringDescription _kDefaultSpringDesc = |
| 30 | + new SpringDescription.withDampingRatio( |
| 31 | + mass: 1.0, springConstant: 500.0, ratio: 1.0); |
| 32 | +final SpringForce kDefaultSpringForce = new SpringForce(_kDefaultSpringDesc); |
| 33 | + |
| 34 | +const double _kEpsilon = 0.001; |
0 commit comments