bezier_test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.math.BezierTest');
  15. goog.setTestOnly('goog.math.BezierTest');
  16. goog.require('goog.math');
  17. goog.require('goog.math.Bezier');
  18. goog.require('goog.math.Coordinate');
  19. goog.require('goog.testing.jsunit');
  20. function testEquals() {
  21. var input = new goog.math.Bezier(1, 2, 3, 4, 5, 6, 7, 8);
  22. assert(input.equals(input));
  23. }
  24. function testClone() {
  25. var input = new goog.math.Bezier(1, 2, 3, 4, 5, 6, 7, 8);
  26. assertNotEquals('Clone returns a new object', input, input.clone());
  27. assert('Contents of clone match original', input.equals(input.clone()));
  28. }
  29. function testFlip() {
  30. var input = new goog.math.Bezier(1, 1, 2, 2, 3, 3, 4, 4);
  31. var compare = new goog.math.Bezier(4, 4, 3, 3, 2, 2, 1, 1);
  32. var flipped = input.clone();
  33. flipped.flip();
  34. assert('Flipped behaves as expected', compare.equals(flipped));
  35. flipped.flip();
  36. assert('Flipping twice gives original', input.equals(flipped));
  37. }
  38. function testGetPoint() {
  39. var input = new goog.math.Bezier(0, 1, 1, 2, 2, 3, 3, 4);
  40. assert(
  41. goog.math.Coordinate.equals(
  42. input.getPoint(0), new goog.math.Coordinate(0, 1)));
  43. assert(
  44. goog.math.Coordinate.equals(
  45. input.getPoint(1), new goog.math.Coordinate(3, 4)));
  46. assert(
  47. goog.math.Coordinate.equals(
  48. input.getPoint(0.5), new goog.math.Coordinate(1.5, 2.5)));
  49. }
  50. function testGetPointX() {
  51. var input = new goog.math.Bezier(0, 1, 1, 2, 2, 3, 3, 4);
  52. assert(goog.math.nearlyEquals(input.getPointX(0), 0));
  53. assert(goog.math.nearlyEquals(input.getPointX(1), 3));
  54. assert(goog.math.nearlyEquals(input.getPointX(0.5), 1.5));
  55. }
  56. function testGetPointY() {
  57. var input = new goog.math.Bezier(0, 1, 1, 2, 2, 3, 3, 4);
  58. assert(goog.math.nearlyEquals(input.getPointY(0), 1));
  59. assert(goog.math.nearlyEquals(input.getPointY(1), 4));
  60. assert(goog.math.nearlyEquals(input.getPointY(0.5), 2.5));
  61. }
  62. function testSubdivide() {
  63. var input = new goog.math.Bezier(0, 1, 1, 2, 2, 3, 3, 4);
  64. input.subdivide(1 / 3, 2 / 3);
  65. assert(goog.math.nearlyEquals(1, input.x0));
  66. assert(goog.math.nearlyEquals(2, input.y0));
  67. assert(goog.math.nearlyEquals(2, input.x3));
  68. assert(goog.math.nearlyEquals(3, input.y3));
  69. }
  70. function testSolvePositionFromXValue() {
  71. var eps = 1e-6;
  72. var bezier = new goog.math.Bezier(0, 0, 0.25, 0.1, 0.25, 1, 1, 1);
  73. var pt = bezier.getPoint(0.5);
  74. assertRoughlyEquals(0.3125, pt.x, eps);
  75. assertRoughlyEquals(0.5375, pt.y, eps);
  76. assertRoughlyEquals(
  77. 0.321, bezier.solvePositionFromXValue(bezier.getPoint(0.321).x), eps);
  78. }
  79. function testSolveYValueFromXValue() {
  80. var eps = 1e-6;
  81. // The following example is taken from
  82. // http://www.netzgesta.de/dev/cubic-bezier-timing-function.html.
  83. // The timing values shown in that page are 1 - <value> so the
  84. // bezier curves in this test are constructed with 1 - ctrl points.
  85. // E.g. ctrl points (0, 0, 0.25, 0.1, 0.25, 1, 1, 1) become
  86. // (1, 1, 0.75, 0, 0.75, 0.9, 0, 0) here. Since chanding the order of
  87. // the ctrl points does not affect the shape of the curve, once can also
  88. // have (0, 0, 0.75, 0.9, 0.75, 0, 1, 1).
  89. // netzgesta example.
  90. var bezier = new goog.math.Bezier(1, 1, 0.75, 0.9, 0.75, 0, 0, 0);
  91. assertRoughlyEquals(0.024374631, bezier.solveYValueFromXValue(0.2), eps);
  92. assertRoughlyEquals(0.317459494, bezier.solveYValueFromXValue(0.6), eps);
  93. assertRoughlyEquals(0.905205002, bezier.solveYValueFromXValue(0.9), eps);
  94. // netzgesta example with ctrl points in the reverse order so that 1st and
  95. // last ctrl points are (0, 0) and (1, 1). Note the result is exactly the
  96. // same.
  97. bezier = new goog.math.Bezier(0, 0, 0.75, 0, 0.75, 0.9, 1, 1);
  98. assertRoughlyEquals(0.024374631, bezier.solveYValueFromXValue(0.2), eps);
  99. assertRoughlyEquals(0.317459494, bezier.solveYValueFromXValue(0.6), eps);
  100. assertRoughlyEquals(0.905205002, bezier.solveYValueFromXValue(0.9), eps);
  101. // Ease-out css animation timing in webkit.
  102. bezier = new goog.math.Bezier(0, 0, 0, 0, 0.58, 1, 1, 1);
  103. assertRoughlyEquals(0.308366667, bezier.solveYValueFromXValue(0.2), eps);
  104. assertRoughlyEquals(0.785139061, bezier.solveYValueFromXValue(0.6), eps);
  105. assertRoughlyEquals(0.982973389, bezier.solveYValueFromXValue(0.9), eps);
  106. }