easing.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Easings</title>
  5. <script src="../dev-lib/sea.js"></script>
  6. <script>
  7. seajs.config({
  8. base: '../src'
  9. });
  10. define('start', function (require) {
  11. var Paper = require('graphic/paper');
  12. var Group = require('graphic/group');
  13. var Path = require('graphic/path');
  14. var Color = require('graphic/color');
  15. var Text = require('graphic/text');
  16. var Matrix = require('graphic/matrix');
  17. var Animator = require('animate/animator');
  18. var easingTable = require('animate/easing');
  19. var YMarker = require('core/class').createClass({
  20. base: Path,
  21. constructor: function( x, w, h ) {
  22. this.callBase();
  23. this.draw( x, w, h );
  24. },
  25. draw: function( x, w, h ) {
  26. var drawer = this.getDrawer();
  27. var hh = h / 2;
  28. drawer.moveTo( x, 0 );
  29. drawer.lineTo( x + hh, hh );
  30. drawer.lineTo( x + w, hh );
  31. drawer.lineTo( x + w, -hh );
  32. drawer.lineTo( x + hh, -hh );
  33. drawer.close();
  34. },
  35. mark: function( y ) {
  36. this.setTransform(new Matrix().translate(0, y));
  37. }
  38. });
  39. var MarkerAnimator = require('core/class').createClass({
  40. base: Animator,
  41. constructor: function( beginValue, finishValue ) {
  42. this.callBase( beginValue, finishValue, function( marker, value ) {
  43. marker.mark( value );
  44. } );
  45. }
  46. });
  47. var EasingGraph = require('core/class').createClass({
  48. base: Paper,
  49. constructor: function( container, easing ) {
  50. this.callBase(container);
  51. this.setViewBox(0, 0, 200, 120);
  52. var x = [0, 150], y = [30, 90];
  53. this.draw( easing, x, y );
  54. this.bind( easing, x, y );
  55. },
  56. draw: function( easing, x, y ) {
  57. this.coordinate = new Group().setAnchor(0, 60).scale(1, -1); // flip over y-alix
  58. this.coordinate.addShape(new Path().pipe(function() {
  59. var d = this.getDrawer();
  60. d.moveTo(x[0], y[0]).lineTo(x[1], y[0]);
  61. d.moveTo(x[0], y[1]).lineTo(x[1], y[1]);
  62. this.fill('none')
  63. this.stroke('#CCC')
  64. }));
  65. this.coordinate.addShape(new Path().pipe(function() {
  66. var d = this.getDrawer();
  67. d.moveTo(x[0], y[0]);
  68. for(var t = x[0]; t <= x[1]; t += 1) {
  69. d.lineTo(t, easing(t, y[0], y[1] - y[0], x[1] - x[0]));
  70. }
  71. this.fill('none');
  72. this.stroke('red');
  73. }));
  74. this.addShape(this.coordinate);
  75. },
  76. bind: function( easing, x, y ) {
  77. var marker = new YMarker( x[1] + 5, 25, 10 );
  78. marker.mark( y[0] );
  79. this.coordinate.addShape( marker );
  80. marker.fill('none');
  81. var animator = new MarkerAnimator( y[0], y[1] );
  82. var timeline = animator.create( marker, 1000, easing );
  83. var animateTimeout;
  84. this.on('mouseover', function() {
  85. marker.fill('red');
  86. timeline.play();
  87. });
  88. this.on('mouseout', function() {
  89. marker.fill('none');
  90. timeline.stop();
  91. timeline.reset();
  92. });
  93. }
  94. });
  95. for(var name in easingTable) {
  96. var div = document.createElement('div');
  97. var graph = new EasingGraph( div, easingTable[name] );
  98. graph.setWidth(200);
  99. graph.setHeight(120);
  100. document.body.appendChild(div);
  101. div.appendChild(document.createTextNode(name));
  102. }
  103. });
  104. seajs.use('start');
  105. </script>
  106. <style>
  107. div {
  108. width: 200px;
  109. height: 150px;
  110. float: left;
  111. color: blue;
  112. }
  113. </style>
  114. </head>
  115. <body>
  116. </body>
  117. </html>