Source: core/display/TransformStatic.js

core/display/TransformStatic.js

  1. import { ObservablePoint } from '../math';
  2. import TransformBase from './TransformBase';
  3. /**
  4. * Transform that takes care about its versions
  5. *
  6. * @class
  7. * @extends PIXI.TransformBase
  8. * @memberof PIXI
  9. */
  10. export default class TransformStatic extends TransformBase
  11. {
  12. /**
  13. *
  14. */
  15. constructor()
  16. {
  17. super();
  18. /**
  19. * The coordinate of the object relative to the local coordinates of the parent.
  20. *
  21. * @member {PIXI.ObservablePoint}
  22. */
  23. this.position = new ObservablePoint(this.onChange, this, 0, 0);
  24. /**
  25. * The scale factor of the object.
  26. *
  27. * @member {PIXI.ObservablePoint}
  28. */
  29. this.scale = new ObservablePoint(this.onChange, this, 1, 1);
  30. /**
  31. * The pivot point of the displayObject that it rotates around.
  32. *
  33. * @member {PIXI.ObservablePoint}
  34. */
  35. this.pivot = new ObservablePoint(this.onChange, this, 0, 0);
  36. /**
  37. * The skew amount, on the x and y axis.
  38. *
  39. * @member {PIXI.ObservablePoint}
  40. */
  41. this.skew = new ObservablePoint(this.updateSkew, this, 0, 0);
  42. this._rotation = 0;
  43. this._cx = 1; // cos rotation + skewY;
  44. this._sx = 0; // sin rotation + skewY;
  45. this._cy = 0; // cos rotation + Math.PI/2 - skewX;
  46. this._sy = 1; // sin rotation + Math.PI/2 - skewX;
  47. this._localID = 0;
  48. this._currentLocalID = 0;
  49. }
  50. /**
  51. * Called when a value changes.
  52. *
  53. * @private
  54. */
  55. onChange()
  56. {
  57. this._localID ++;
  58. }
  59. /**
  60. * Called when skew or rotation changes
  61. *
  62. * @private
  63. */
  64. updateSkew()
  65. {
  66. this._cx = Math.cos(this._rotation + this.skew._y);
  67. this._sx = Math.sin(this._rotation + this.skew._y);
  68. this._cy = -Math.sin(this._rotation - this.skew._x); // cos, added PI/2
  69. this._sy = Math.cos(this._rotation - this.skew._x); // sin, added PI/2
  70. this._localID ++;
  71. }
  72. /**
  73. * Updates only local matrix
  74. */
  75. updateLocalTransform()
  76. {
  77. const lt = this.localTransform;
  78. if (this._localID !== this._currentLocalID)
  79. {
  80. // get the matrix values of the displayobject based on its transform properties..
  81. lt.a = this._cx * this.scale._x;
  82. lt.b = this._sx * this.scale._x;
  83. lt.c = this._cy * this.scale._y;
  84. lt.d = this._sy * this.scale._y;
  85. lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c));
  86. lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d));
  87. this._currentLocalID = this._localID;
  88. // force an update..
  89. this._parentID = -1;
  90. }
  91. }
  92. /**
  93. * Updates the values of the object and applies the parent's transform.
  94. *
  95. * @param {PIXI.Transform} parentTransform - The transform of the parent of this object
  96. */
  97. updateTransform(parentTransform)
  98. {
  99. const lt = this.localTransform;
  100. if (this._localID !== this._currentLocalID)
  101. {
  102. // get the matrix values of the displayobject based on its transform properties..
  103. lt.a = this._cx * this.scale._x;
  104. lt.b = this._sx * this.scale._x;
  105. lt.c = this._cy * this.scale._y;
  106. lt.d = this._sy * this.scale._y;
  107. lt.tx = this.position._x - ((this.pivot._x * lt.a) + (this.pivot._y * lt.c));
  108. lt.ty = this.position._y - ((this.pivot._x * lt.b) + (this.pivot._y * lt.d));
  109. this._currentLocalID = this._localID;
  110. // force an update..
  111. this._parentID = -1;
  112. }
  113. if (this._parentID !== parentTransform._worldID)
  114. {
  115. // concat the parent matrix with the objects transform.
  116. const pt = parentTransform.worldTransform;
  117. const wt = this.worldTransform;
  118. wt.a = (lt.a * pt.a) + (lt.b * pt.c);
  119. wt.b = (lt.a * pt.b) + (lt.b * pt.d);
  120. wt.c = (lt.c * pt.a) + (lt.d * pt.c);
  121. wt.d = (lt.c * pt.b) + (lt.d * pt.d);
  122. wt.tx = (lt.tx * pt.a) + (lt.ty * pt.c) + pt.tx;
  123. wt.ty = (lt.tx * pt.b) + (lt.ty * pt.d) + pt.ty;
  124. this._parentID = parentTransform._worldID;
  125. // update the id of the transform..
  126. this._worldID ++;
  127. }
  128. }
  129. /**
  130. * Decomposes a matrix and sets the transforms properties based on it.
  131. *
  132. * @param {PIXI.Matrix} matrix - The matrix to decompose
  133. */
  134. setFromMatrix(matrix)
  135. {
  136. matrix.decompose(this);
  137. this._localID ++;
  138. }
  139. /**
  140. * The rotation of the object in radians.
  141. *
  142. * @member {number}
  143. */
  144. get rotation()
  145. {
  146. return this._rotation;
  147. }
  148. set rotation(value) // eslint-disable-line require-jsdoc
  149. {
  150. if (this._rotation !== value)
  151. {
  152. this._rotation = value;
  153. this.updateSkew();
  154. }
  155. }
  156. }