MouseModifier.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import {
  2. extendPrototype,
  3. } from '../functionExtensions';
  4. import {
  5. ShapeModifiers,
  6. ShapeModifier,
  7. } from './ShapeModifiers';
  8. function MouseModifier() {}
  9. extendPrototype([ShapeModifier], MouseModifier);
  10. MouseModifier.prototype.processKeys = function (forceRender) {
  11. if (this.elem.globalData.frameId === this.frameId && !forceRender) {
  12. return;
  13. }
  14. this._mdf = true;
  15. };
  16. MouseModifier.prototype.addShapeToModifier = function () {
  17. this.positions.push([]);
  18. };
  19. MouseModifier.prototype.processPath = function (path, mouseCoords, positions) {
  20. var i;
  21. var len = path.v.length;
  22. var vValues = [];
  23. var oValues = [];
  24. var iValues = [];
  25. var theta;
  26. var x;
  27. var y;
  28. /// / OPTION A
  29. for (i = 0; i < len; i += 1) {
  30. if (!positions.v[i]) {
  31. positions.v[i] = [path.v[i][0], path.v[i][1]];
  32. positions.o[i] = [path.o[i][0], path.o[i][1]];
  33. positions.i[i] = [path.i[i][0], path.i[i][1]];
  34. positions.distV[i] = 0;
  35. positions.distO[i] = 0;
  36. positions.distI[i] = 0;
  37. }
  38. theta = Math.atan2(
  39. path.v[i][1] - mouseCoords[1],
  40. path.v[i][0] - mouseCoords[0]
  41. );
  42. x = mouseCoords[0] - positions.v[i][0];
  43. y = mouseCoords[1] - positions.v[i][1];
  44. var distance = Math.sqrt((x * x) + (y * y));
  45. positions.distV[i] += (distance - positions.distV[i]) * this.data.dc;
  46. positions.v[i][0] = (Math.cos(theta) * Math.max(0, this.data.maxDist - positions.distV[i])) / 2 + (path.v[i][0]);
  47. positions.v[i][1] = (Math.sin(theta) * Math.max(0, this.data.maxDist - positions.distV[i])) / 2 + (path.v[i][1]);
  48. theta = Math.atan2(
  49. path.o[i][1] - mouseCoords[1],
  50. path.o[i][0] - mouseCoords[0]
  51. );
  52. x = mouseCoords[0] - positions.o[i][0];
  53. y = mouseCoords[1] - positions.o[i][1];
  54. distance = Math.sqrt((x * x) + (y * y));
  55. positions.distO[i] += (distance - positions.distO[i]) * this.data.dc;
  56. positions.o[i][0] = (Math.cos(theta) * Math.max(0, this.data.maxDist - positions.distO[i])) / 2 + (path.o[i][0]);
  57. positions.o[i][1] = (Math.sin(theta) * Math.max(0, this.data.maxDist - positions.distO[i])) / 2 + (path.o[i][1]);
  58. theta = Math.atan2(
  59. path.i[i][1] - mouseCoords[1],
  60. path.i[i][0] - mouseCoords[0]
  61. );
  62. x = mouseCoords[0] - positions.i[i][0];
  63. y = mouseCoords[1] - positions.i[i][1];
  64. distance = Math.sqrt((x * x) + (y * y));
  65. positions.distI[i] += (distance - positions.distI[i]) * this.data.dc;
  66. positions.i[i][0] = (Math.cos(theta) * Math.max(0, this.data.maxDist - positions.distI[i])) / 2 + (path.i[i][0]);
  67. positions.i[i][1] = (Math.sin(theta) * Math.max(0, this.data.maxDist - positions.distI[i])) / 2 + (path.i[i][1]);
  68. /// //OPTION 1
  69. vValues.push(positions.v[i]);
  70. oValues.push(positions.o[i]);
  71. iValues.push(positions.i[i]);
  72. /// //OPTION 2
  73. // vValues.push(positions.v[i]);
  74. // iValues.push([path.i[i][0]+(positions.v[i][0]-path.v[i][0]),path.i[i][1]+(positions.v[i][1]-path.v[i][1])]);
  75. // oValues.push([path.o[i][0]+(positions.v[i][0]-path.v[i][0]),path.o[i][1]+(positions.v[i][1]-path.v[i][1])]);
  76. /// //OPTION 3
  77. // vValues.push(positions.v[i]);
  78. // iValues.push(path.i[i]);
  79. // oValues.push(path.o[i]);
  80. /// //OPTION 4
  81. // vValues.push(path.v[i]);
  82. // oValues.push(positions.o[i]);
  83. // iValues.push(positions.i[i]);
  84. }
  85. /// / OPTION B
  86. /* for(i=0;i<len;i+=1){
  87. if(!positions.v[i]){
  88. positions.v[i] = [path.v[i][0],path.v[i][1]];
  89. positions.o[i] = [path.o[i][0],path.o[i][1]];
  90. positions.i[i] = [path.i[i][0],path.i[i][1]];
  91. positions.distV[i] = 0;
  92. }
  93. theta = Math.atan2(
  94. positions.v[i][1] - mouseCoords[1],
  95. positions.v[i][0] - mouseCoords[0]
  96. );
  97. x = mouseCoords[0] - positions.v[i][0];
  98. y = mouseCoords[1] - positions.v[i][1];
  99. var distance = this.data.ss * this.data.mx / Math.sqrt( (x * x) + (y * y) );
  100. positions.v[i][0] += Math.cos(theta) * distance + (path.v[i][0] - positions.v[i][0]) * this.data.dc;
  101. positions.v[i][1] += Math.sin(theta) * distance + (path.v[i][1] - positions.v[i][1]) * this.data.dc;
  102. theta = Math.atan2(
  103. positions.o[i][1] - mouseCoords[1],
  104. positions.o[i][0] - mouseCoords[0]
  105. );
  106. x = mouseCoords[0] - positions.o[i][0];
  107. y = mouseCoords[1] - positions.o[i][1];
  108. var distance = this.data.ss * this.data.mx / Math.sqrt( (x * x) + (y * y) );
  109. positions.o[i][0] += Math.cos(theta) * distance + (path.o[i][0] - positions.o[i][0]) * this.data.dc;
  110. positions.o[i][1] += Math.sin(theta) * distance + (path.o[i][1] - positions.o[i][1]) * this.data.dc;
  111. theta = Math.atan2(
  112. positions.i[i][1] - mouseCoords[1],
  113. positions.i[i][0] - mouseCoords[0]
  114. );
  115. x = mouseCoords[0] - positions.i[i][0];
  116. y = mouseCoords[1] - positions.i[i][1];
  117. var distance = this.data.ss * this.data.mx / Math.sqrt( (x * x) + (y * y) );
  118. positions.i[i][0] += Math.cos(theta) * distance + (path.i[i][0] - positions.i[i][0]) * this.data.dc;
  119. positions.i[i][1] += Math.sin(theta) * distance + (path.i[i][1] - positions.i[i][1]) * this.data.dc;
  120. /////OPTION 1
  121. //vValues.push(positions.v[i]);
  122. // oValues.push(positions.o[i]);
  123. // iValues.push(positions.i[i]);
  124. /////OPTION 2
  125. //vValues.push(positions.v[i]);
  126. // iValues.push([path.i[i][0]+(positions.v[i][0]-path.v[i][0]),path.i[i][1]+(positions.v[i][1]-path.v[i][1])]);
  127. // oValues.push([path.o[i][0]+(positions.v[i][0]-path.v[i][0]),path.o[i][1]+(positions.v[i][1]-path.v[i][1])]);
  128. /////OPTION 3
  129. //vValues.push(positions.v[i]);
  130. //iValues.push(path.i[i]);
  131. //oValues.push(path.o[i]);
  132. /////OPTION 4
  133. //vValues.push(path.v[i]);
  134. // oValues.push(positions.o[i]);
  135. // iValues.push(positions.i[i]);
  136. } */
  137. return {
  138. v: vValues,
  139. o: oValues,
  140. i: iValues,
  141. c: path.c,
  142. };
  143. };
  144. MouseModifier.prototype.processShapes = function () {
  145. var mouseX = this.elem.globalData.mouseX;
  146. var mouseY = this.elem.globalData.mouseY;
  147. var shapePaths;
  148. var i;
  149. var len = this.shapes.length;
  150. var j;
  151. var jLen;
  152. if (mouseX) {
  153. var localMouseCoords = this.elem.globalToLocal([mouseX, mouseY, 0]);
  154. var shapeData;
  155. var newPaths = [];
  156. for (i = 0; i < len; i += 1) {
  157. shapeData = this.shapes[i];
  158. if (!shapeData.shape._mdf && !this._mdf) {
  159. shapeData.shape.paths = shapeData.last;
  160. } else {
  161. shapeData.shape._mdf = true;
  162. shapePaths = shapeData.shape.paths;
  163. jLen = shapePaths.length;
  164. for (j = 0; j < jLen; j += 1) {
  165. if (!this.positions[i][j]) {
  166. this.positions[i][j] = {
  167. v: [],
  168. o: [],
  169. i: [],
  170. distV: [],
  171. distO: [],
  172. distI: [],
  173. };
  174. }
  175. newPaths.push(this.processPath(shapePaths[j], localMouseCoords, this.positions[i][j]));
  176. }
  177. shapeData.shape.paths = newPaths;
  178. shapeData.last = newPaths;
  179. }
  180. }
  181. }
  182. };
  183. MouseModifier.prototype.initModifierProperties = function (elem, data) {
  184. this.getValue = this.processKeys;
  185. this.data = data;
  186. this.positions = [];
  187. };
  188. ShapeModifiers.registerModifier('ms', MouseModifier);
  189. export default MouseModifier;