Source: animate/mixins/DisplayObject.js

animate/mixins/DisplayObject.js

  1. import utils from '../utils';
  2. //import { DisplayObject } from '../../core/display/DisplayObject';
  3. import { filters } from '../../filters/index';
  4. //import { Graphics } from '../../core/graphics/Graphics';
  5. //import { Sprite } from '../../core/sprites/Sprite';
  6. import * as core from '../../core';
  7. const DisplayObject = core.DisplayObject;
  8. const Graphics = core.Graphics;
  9. const Sprite = core.Sprite;
  10. /**
  11. * Mixins for the PIXI.DisplayObject class.
  12. * @memberof PIXI
  13. * @class DisplayObject
  14. */
  15. const p = DisplayObject.prototype;
  16. // Color Matrix filter
  17. let ColorMatrixFilter;
  18. if (filters) {
  19. ColorMatrixFilter = filters.ColorMatrixFilter;
  20. }
  21. /**
  22. * Function to see if this is renderable or not. Useful for setting masks.
  23. * @method PIXI.DisplayObject#setRenderable
  24. * @param {Boolean} [renderable=false] Make renderable
  25. * @return {PIXI.DisplayObject}
  26. */
  27. /**
  28. * Shortcut to `setRenderable`.
  29. * @method PIXI.DisplayObject#re
  30. * @param {Boolean} [renderable=false] Make renderable
  31. * @return {PIXI.DisplayObject}
  32. */
  33. p.setRenderable = p.re = function(renderable) {
  34. this.renderable = !!renderable;
  35. return this;
  36. };
  37. /**
  38. * Shortcut for `setTransform`.
  39. * @method PIXI.DisplayObject#tr
  40. * @param {Number} x The X position
  41. * @param {Number} y The Y position
  42. * @param {Number} scaleX The X Scale value
  43. * @param {Number} scaleY The Y Scale value
  44. * @param {Number} skewX The X skew value
  45. * @param {Number} skewY The Y skew value
  46. * @param {Number} pivotX The X pivot value
  47. * @param {Number} pivotY The Y pivot value
  48. * @return {PIXI.DisplayObject} Instance for chaining
  49. */
  50. p.t = p.setTransform;
  51. /**
  52. * Setter for mask to be able to chain.
  53. * @method PIXI.DisplayObject#setMask
  54. * @param {PIXI.Graphics} mask The mask shape to use
  55. * @return {PIXI.DisplayObject} Instance for chaining
  56. */
  57. /**
  58. * Shortcut for `setMask`.
  59. * @method PIXI.DisplayObject#ma
  60. * @param {PIXI.Sprite|PIXI.Graphics} mask The mask shape to use
  61. * @return {PIXI.DisplayObject} Instance for chaining
  62. */
  63. p.setMask = p.ma = function(mask) {
  64. // According to PIXI, only Graphics and Sprites can
  65. // be used as mask, let's ignore everything else, like other
  66. // movieclips and displayobjects/containers
  67. if (mask) {
  68. if (!(mask instanceof Graphics) && !(mask instanceof Sprite)) {
  69. if (typeof console !== "undefined" && console.warn) {
  70. console.warn("Warning: Masks can only be PIXI.Graphics or PIXI.Sprite objects.");
  71. }
  72. return this;
  73. }
  74. }
  75. this.mask = mask;
  76. return this;
  77. };
  78. /**
  79. * Setter for the alpha
  80. * @method PIXI.DisplayObject#setAlpha
  81. * @param {Number} alpha The alpha amount to use, from 0 to 1
  82. * @return {PIXI.DisplayObject} Instance for chaining
  83. */
  84. /**
  85. * Shortcut for `setAlpha`.
  86. * @method PIXI.DisplayObject#a
  87. * @param {Number} alpha The alpha amount to use, from 0 to 1
  88. * @return {PIXI.DisplayObject} Instance for chaining
  89. */
  90. p.setAlpha = p.a = function(alpha) {
  91. this.alpha = alpha;
  92. return this;
  93. };
  94. /**
  95. * Set the tint values by color.
  96. * @method PIXI.DisplayObject#setTint
  97. * @param {int} tint The color value to tint
  98. * @return {PIXI.DisplayObject} Object for chaining
  99. */
  100. /**
  101. * Shortcut to `setTint`.
  102. * @method PIXI.DisplayObject#tn
  103. * @param {Number|String} tint The red percentage value
  104. * @return {PIXI.DisplayObject} Object for chaining
  105. */
  106. p.setTint = p.i = function(tint) {
  107. if (typeof tint === "string") {
  108. tint = utils.hexToUint(tint);
  109. }
  110. // this.tint = tint
  111. // return this;
  112. // TODO: Replace with DisplayObject.tint setter
  113. // once the functionality is added to Pixi.js, for
  114. // now we'll use the slower ColorMatrixFilter to handle
  115. // the color transformation
  116. var r = tint >> 16 & 0xFF;
  117. var g = tint >> 8 & 0xFF;
  118. var b = tint & 0xFF;
  119. return this.c(r / 255, 0, g / 255, 0, b / 255, 0);
  120. };
  121. /**
  122. * Set additive and multiply color, tinting
  123. * @method PIXI.DisplayObject#setColorTransform
  124. * @param {Number} r The multiply red value
  125. * @param {Number} rA The additive red value
  126. * @param {Number} g The multiply green value
  127. * @param {Number} gA The additive green value
  128. * @param {Number} b The multiply blue value
  129. * @param {Number} bA The additive blue value
  130. * @return {PIXI.DisplayObject} Object for chaining
  131. */
  132. /**
  133. * Shortcut to `setColor`.
  134. * @method PIXI.DisplayObject#c
  135. * @param {Number} r The multiply red value
  136. * @param {Number} rA The additive red value
  137. * @param {Number} g The multiply green value
  138. * @param {Number} gA The additive green value
  139. * @param {Number} b The multiply blue value
  140. * @param {Number} bA The additive blue value
  141. * @return {PIXI.DisplayObject} Object for chaining
  142. */
  143. p.setColorTransform = p.c = function(r, rA, g, gA, b, bA) {
  144. var filter = this.colorTransformFilter;
  145. filter.matrix[0] = r;
  146. filter.matrix[4] = rA;
  147. filter.matrix[6] = g;
  148. filter.matrix[9] = gA;
  149. filter.matrix[12] = b;
  150. filter.matrix[14] = bA;
  151. this.filters = [filter];
  152. return this;
  153. };
  154. /**
  155. * The current default color transforming filters
  156. * @name {PIXI.filters.ColorMatrixFilter} PIXI.DisplayObject#colorTransformFilter
  157. */
  158. if (!p.hasOwnProperty('colorTransformFilter')) {
  159. Object.defineProperty(p, 'colorTransformFilter', {
  160. set: function(filter) {
  161. this._colorTransformFilter = filter;
  162. },
  163. get: function() {
  164. return this._colorTransformFilter || new ColorMatrixFilter();
  165. }
  166. });
  167. }
  168. /**
  169. * Extend a container
  170. * @method PIXI.DisplayObject.extend
  171. * @static
  172. * @param {PIXI.DisplayObject} child The child function
  173. * @return {PIXI.DisplayObject} THe child
  174. */
  175. /**
  176. * Extend a container (shortcut for `extend`)
  177. * @method PIXI.DisplayObject.e
  178. * @static
  179. * @param {PIXI.DisplayObject} child The child function
  180. * @return {PIXI.DisplayObject} THe child
  181. */
  182. DisplayObject.extend = DisplayObject.e = function(child) {
  183. child.prototype = Object.create(p);
  184. child.prototype.__parent = p;
  185. child.prototype.constructor = child;
  186. return child;
  187. };