GradientProperty.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {
  2. extendPrototype,
  3. } from '../functionExtensions';
  4. import DynamicPropertyContainer from '../helpers/dynamicProperties';
  5. import {
  6. createTypedArray,
  7. } from '../helpers/arrays';
  8. import PropertyFactory from '../PropertyFactory';
  9. function GradientProperty(elem, data, container) {
  10. this.data = data;
  11. this.c = createTypedArray('uint8c', data.p * 4);
  12. var cLength = data.k.k[0].s ? (data.k.k[0].s.length - data.p * 4) : data.k.k.length - data.p * 4;
  13. this.o = createTypedArray('float32', cLength);
  14. this._cmdf = false;
  15. this._omdf = false;
  16. this._collapsable = this.checkCollapsable();
  17. this._hasOpacity = cLength;
  18. this.initDynamicPropertyContainer(container);
  19. this.prop = PropertyFactory.getProp(elem, data.k, 1, null, this);
  20. this.k = this.prop.k;
  21. this.getValue(true);
  22. }
  23. GradientProperty.prototype.comparePoints = function (values, points) {
  24. var i = 0;
  25. var len = this.o.length / 2;
  26. var diff;
  27. while (i < len) {
  28. diff = Math.abs(values[i * 4] - values[points * 4 + i * 2]);
  29. if (diff > 0.01) {
  30. return false;
  31. }
  32. i += 1;
  33. }
  34. return true;
  35. };
  36. GradientProperty.prototype.checkCollapsable = function () {
  37. if (this.o.length / 2 !== this.c.length / 4) {
  38. return false;
  39. }
  40. if (this.data.k.k[0].s) {
  41. var i = 0;
  42. var len = this.data.k.k.length;
  43. while (i < len) {
  44. if (!this.comparePoints(this.data.k.k[i].s, this.data.p)) {
  45. return false;
  46. }
  47. i += 1;
  48. }
  49. } else if (!this.comparePoints(this.data.k.k, this.data.p)) {
  50. return false;
  51. }
  52. return true;
  53. };
  54. GradientProperty.prototype.getValue = function (forceRender) {
  55. this.prop.getValue();
  56. this._mdf = false;
  57. this._cmdf = false;
  58. this._omdf = false;
  59. if (this.prop._mdf || forceRender) {
  60. var i;
  61. var len = this.data.p * 4;
  62. var mult;
  63. var val;
  64. for (i = 0; i < len; i += 1) {
  65. mult = i % 4 === 0 ? 100 : 255;
  66. val = Math.round(this.prop.v[i] * mult);
  67. if (this.c[i] !== val) {
  68. this.c[i] = val;
  69. this._cmdf = !forceRender;
  70. }
  71. }
  72. if (this.o.length) {
  73. len = this.prop.v.length;
  74. for (i = this.data.p * 4; i < len; i += 1) {
  75. mult = i % 2 === 0 ? 100 : 1;
  76. val = i % 2 === 0 ? Math.round(this.prop.v[i] * 100) : this.prop.v[i];
  77. if (this.o[i - this.data.p * 4] !== val) {
  78. this.o[i - this.data.p * 4] = val;
  79. this._omdf = !forceRender;
  80. }
  81. }
  82. }
  83. this._mdf = !forceRender;
  84. }
  85. };
  86. extendPrototype([DynamicPropertyContainer], GradientProperty);
  87. export default GradientProperty;