Path.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**
  2. * @author zz85 / http://www.lab4games.net/zz85/blog
  3. * Creates free form 2d path using series of points, lines or curves.
  4. *
  5. **/
  6. THREE.Path = function ( points ) {
  7. THREE.CurvePath.call(this);
  8. this.actions = [];
  9. if ( points ) {
  10. this.fromPoints( points );
  11. }
  12. };
  13. THREE.Path.prototype = Object.create( THREE.CurvePath.prototype );
  14. THREE.PathActions = {
  15. MOVE_TO: 'moveTo',
  16. LINE_TO: 'lineTo',
  17. QUADRATIC_CURVE_TO: 'quadraticCurveTo', // Bezier quadratic curve
  18. BEZIER_CURVE_TO: 'bezierCurveTo', // Bezier cubic curve
  19. CSPLINE_THRU: 'splineThru', // Catmull-rom spline
  20. ARC: 'arc', // Circle
  21. ELLIPSE: 'ellipse'
  22. };
  23. // TODO Clean up PATH API
  24. // Create path using straight lines to connect all points
  25. // - vectors: array of Vector2
  26. THREE.Path.prototype.fromPoints = function ( vectors ) {
  27. this.moveTo( vectors[ 0 ].x, vectors[ 0 ].y );
  28. for ( var v = 1, vlen = vectors.length; v < vlen; v ++ ) {
  29. this.lineTo( vectors[ v ].x, vectors[ v ].y );
  30. };
  31. };
  32. // startPath() endPath()?
  33. THREE.Path.prototype.moveTo = function ( x, y ) {
  34. var args = Array.prototype.slice.call( arguments );
  35. this.actions.push( { action: THREE.PathActions.MOVE_TO, args: args } );
  36. };
  37. THREE.Path.prototype.lineTo = function ( x, y ) {
  38. var args = Array.prototype.slice.call( arguments );
  39. var lastargs = this.actions[ this.actions.length - 1 ].args;
  40. var x0 = lastargs[ lastargs.length - 2 ];
  41. var y0 = lastargs[ lastargs.length - 1 ];
  42. var curve = new THREE.LineCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( x, y ) );
  43. this.curves.push( curve );
  44. this.actions.push( { action: THREE.PathActions.LINE_TO, args: args } );
  45. };
  46. THREE.Path.prototype.quadraticCurveTo = function( aCPx, aCPy, aX, aY ) {
  47. var args = Array.prototype.slice.call( arguments );
  48. var lastargs = this.actions[ this.actions.length - 1 ].args;
  49. var x0 = lastargs[ lastargs.length - 2 ];
  50. var y0 = lastargs[ lastargs.length - 1 ];
  51. var curve = new THREE.QuadraticBezierCurve( new THREE.Vector2( x0, y0 ),
  52. new THREE.Vector2( aCPx, aCPy ),
  53. new THREE.Vector2( aX, aY ) );
  54. this.curves.push( curve );
  55. this.actions.push( { action: THREE.PathActions.QUADRATIC_CURVE_TO, args: args } );
  56. };
  57. THREE.Path.prototype.bezierCurveTo = function( aCP1x, aCP1y,
  58. aCP2x, aCP2y,
  59. aX, aY ) {
  60. var args = Array.prototype.slice.call( arguments );
  61. var lastargs = this.actions[ this.actions.length - 1 ].args;
  62. var x0 = lastargs[ lastargs.length - 2 ];
  63. var y0 = lastargs[ lastargs.length - 1 ];
  64. var curve = new THREE.CubicBezierCurve( new THREE.Vector2( x0, y0 ),
  65. new THREE.Vector2( aCP1x, aCP1y ),
  66. new THREE.Vector2( aCP2x, aCP2y ),
  67. new THREE.Vector2( aX, aY ) );
  68. this.curves.push( curve );
  69. this.actions.push( { action: THREE.PathActions.BEZIER_CURVE_TO, args: args } );
  70. };
  71. THREE.Path.prototype.splineThru = function( pts /*Array of Vector*/ ) {
  72. var args = Array.prototype.slice.call( arguments );
  73. var lastargs = this.actions[ this.actions.length - 1 ].args;
  74. var x0 = lastargs[ lastargs.length - 2 ];
  75. var y0 = lastargs[ lastargs.length - 1 ];
  76. //---
  77. var npts = [ new THREE.Vector2( x0, y0 ) ];
  78. Array.prototype.push.apply( npts, pts );
  79. var curve = new THREE.SplineCurve( npts );
  80. this.curves.push( curve );
  81. this.actions.push( { action: THREE.PathActions.CSPLINE_THRU, args: args } );
  82. };
  83. // FUTURE: Change the API or follow canvas API?
  84. THREE.Path.prototype.ellipse = function ( aX, aY, xRadius, yRadius,
  85. aStartAngle, aEndAngle, aClockwise ) {
  86. var laste = this.actions[ this.actions.length - 1];
  87. this.absellipse(laste.x + aX, laste.y + aY, xRadius, yRadius,
  88. aStartAngle, aEndAngle, aClockwise );
  89. };
  90. THREE.Path.prototype.arc = function ( aX, aY, aRadius,
  91. aStartAngle, aEndAngle, aClockwise ) {
  92. var laste = this.actions[ this.actions.length - 1];
  93. this.absarc(laste.x + aX, laste.y + aY, aRadius,
  94. aStartAngle, aEndAngle, aClockwise );
  95. };
  96. THREE.Path.prototype.absellipse = function ( aX, aY, xRadius, yRadius,
  97. aStartAngle, aEndAngle, aClockwise ) {
  98. var args = Array.prototype.slice.call( arguments );
  99. var curve = new THREE.EllipseCurve( aX, aY, xRadius, yRadius,
  100. aStartAngle, aEndAngle, aClockwise );
  101. this.curves.push( curve );
  102. // All of the other actions look to the last two elements in the list to
  103. // find the ending point, so we need to append them.
  104. var lastPoint = curve.getPoint(aClockwise ? 1 : 0);
  105. args.push(lastPoint.x);
  106. args.push(lastPoint.y);
  107. this.actions.push( { action: THREE.PathActions.ELLIPSE, args: args } );
  108. };
  109. THREE.Path.prototype.absarc = function ( aX, aY, aRadius,
  110. aStartAngle, aEndAngle, aClockwise ) {
  111. this.absellipse(aX, aY, aRadius, aRadius,
  112. aStartAngle, aEndAngle, aClockwise);
  113. };
  114. THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
  115. if ( ! divisions ) divisions = 40;
  116. var points = [];
  117. for ( var i = 0; i < divisions; i ++ ) {
  118. points.push( this.getPoint( i / divisions ) );
  119. //if( !this.getPoint( i / divisions ) ) throw "DIE";
  120. }
  121. // if ( closedPath ) {
  122. //
  123. // points.push( points[ 0 ] );
  124. //
  125. // }
  126. return points;
  127. };
  128. /* Return an array of vectors based on contour of the path */
  129. THREE.Path.prototype.getPoints = function( divisions, closedPath ) {
  130. if (this.useSpacedPoints) {
  131. console.log('tata');
  132. return this.getSpacedPoints( divisions, closedPath );
  133. }
  134. divisions = divisions || 12;
  135. var points = [];
  136. var i, il, item, action, args;
  137. var cpx, cpy, cpx2, cpy2, cpx1, cpy1, cpx0, cpy0,
  138. laste, j,
  139. t, tx, ty;
  140. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  141. item = this.actions[ i ];
  142. action = item.action;
  143. args = item.args;
  144. switch( action ) {
  145. case THREE.PathActions.MOVE_TO:
  146. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  147. break;
  148. case THREE.PathActions.LINE_TO:
  149. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  150. break;
  151. case THREE.PathActions.QUADRATIC_CURVE_TO:
  152. cpx = args[ 2 ];
  153. cpy = args[ 3 ];
  154. cpx1 = args[ 0 ];
  155. cpy1 = args[ 1 ];
  156. if ( points.length > 0 ) {
  157. laste = points[ points.length - 1 ];
  158. cpx0 = laste.x;
  159. cpy0 = laste.y;
  160. } else {
  161. laste = this.actions[ i - 1 ].args;
  162. cpx0 = laste[ laste.length - 2 ];
  163. cpy0 = laste[ laste.length - 1 ];
  164. }
  165. for ( j = 1; j <= divisions; j ++ ) {
  166. t = j / divisions;
  167. tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
  168. ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
  169. points.push( new THREE.Vector2( tx, ty ) );
  170. }
  171. break;
  172. case THREE.PathActions.BEZIER_CURVE_TO:
  173. cpx = args[ 4 ];
  174. cpy = args[ 5 ];
  175. cpx1 = args[ 0 ];
  176. cpy1 = args[ 1 ];
  177. cpx2 = args[ 2 ];
  178. cpy2 = args[ 3 ];
  179. if ( points.length > 0 ) {
  180. laste = points[ points.length - 1 ];
  181. cpx0 = laste.x;
  182. cpy0 = laste.y;
  183. } else {
  184. laste = this.actions[ i - 1 ].args;
  185. cpx0 = laste[ laste.length - 2 ];
  186. cpy0 = laste[ laste.length - 1 ];
  187. }
  188. for ( j = 1; j <= divisions; j ++ ) {
  189. t = j / divisions;
  190. tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx );
  191. ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy );
  192. points.push( new THREE.Vector2( tx, ty ) );
  193. }
  194. break;
  195. case THREE.PathActions.CSPLINE_THRU:
  196. laste = this.actions[ i - 1 ].args;
  197. var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] );
  198. var spts = [ last ];
  199. var n = divisions * args[ 0 ].length;
  200. spts = spts.concat( args[ 0 ] );
  201. var spline = new THREE.SplineCurve( spts );
  202. for ( j = 1; j <= n; j ++ ) {
  203. points.push( spline.getPointAt( j / n ) ) ;
  204. }
  205. break;
  206. case THREE.PathActions.ARC:
  207. var aX = args[ 0 ], aY = args[ 1 ],
  208. aRadius = args[ 2 ],
  209. aStartAngle = args[ 3 ], aEndAngle = args[ 4 ],
  210. aClockwise = !!args[ 5 ];
  211. var deltaAngle = aEndAngle - aStartAngle;
  212. var angle;
  213. var tdivisions = divisions * 2;
  214. for ( j = 1; j <= tdivisions; j ++ ) {
  215. t = j / tdivisions;
  216. if ( ! aClockwise ) {
  217. t = 1 - t;
  218. }
  219. angle = aStartAngle + t * deltaAngle;
  220. tx = aX + aRadius * Math.cos( angle );
  221. ty = aY + aRadius * Math.sin( angle );
  222. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  223. points.push( new THREE.Vector2( tx, ty ) );
  224. }
  225. //console.log(points);
  226. break;
  227. case THREE.PathActions.ELLIPSE:
  228. var aX = args[ 0 ], aY = args[ 1 ],
  229. xRadius = args[ 2 ],
  230. yRadius = args[3]
  231. aStartAngle = args[ 4 ], aEndAngle = args[ 5 ],
  232. aClockwise = !!args[ 6 ];
  233. var deltaAngle = aEndAngle - aStartAngle;
  234. var angle;
  235. var tdivisions = divisions * 2;
  236. for ( j = 1; j <= tdivisions; j ++ ) {
  237. t = j / tdivisions;
  238. if ( ! aClockwise ) {
  239. t = 1 - t;
  240. }
  241. angle = aStartAngle + t * deltaAngle;
  242. tx = aX + xRadius * Math.cos( angle );
  243. ty = aY + yRadius * Math.sin( angle );
  244. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  245. points.push( new THREE.Vector2( tx, ty ) );
  246. }
  247. //console.log(points);
  248. break;
  249. } // end switch
  250. }
  251. // Normalize to remove the closing point by default.
  252. var lastPoint = points[ points.length - 1];
  253. var EPSILON = 0.0000000001;
  254. if ( Math.abs(lastPoint.x - points[ 0 ].x) < EPSILON &&
  255. Math.abs(lastPoint.y - points[ 0 ].y) < EPSILON)
  256. points.splice( points.length - 1, 1);
  257. if ( closedPath ) {
  258. points.push( points[ 0 ] );
  259. }
  260. return points;
  261. };
  262. // Breaks path into shapes
  263. THREE.Path.prototype.toShapes = function() {
  264. var i, il, item, action, args;
  265. var subPaths = [], lastPath = new THREE.Path();
  266. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  267. item = this.actions[ i ];
  268. args = item.args;
  269. action = item.action;
  270. if ( action == THREE.PathActions.MOVE_TO ) {
  271. if ( lastPath.actions.length != 0 ) {
  272. subPaths.push( lastPath );
  273. lastPath = new THREE.Path();
  274. }
  275. }
  276. lastPath[ action ].apply( lastPath, args );
  277. }
  278. if ( lastPath.actions.length != 0 ) {
  279. subPaths.push( lastPath );
  280. }
  281. // console.log(subPaths);
  282. if ( subPaths.length == 0 ) return [];
  283. var tmpPath, tmpShape, shapes = [];
  284. var holesFirst = !THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() );
  285. // console.log("Holes first", holesFirst);
  286. if ( subPaths.length == 1) {
  287. tmpPath = subPaths[0];
  288. tmpShape = new THREE.Shape();
  289. tmpShape.actions = tmpPath.actions;
  290. tmpShape.curves = tmpPath.curves;
  291. shapes.push( tmpShape );
  292. return shapes;
  293. };
  294. if ( holesFirst ) {
  295. tmpShape = new THREE.Shape();
  296. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  297. tmpPath = subPaths[ i ];
  298. if ( THREE.Shape.Utils.isClockWise( tmpPath.getPoints() ) ) {
  299. tmpShape.actions = tmpPath.actions;
  300. tmpShape.curves = tmpPath.curves;
  301. shapes.push( tmpShape );
  302. tmpShape = new THREE.Shape();
  303. //console.log('cw', i);
  304. } else {
  305. tmpShape.holes.push( tmpPath );
  306. //console.log('ccw', i);
  307. }
  308. }
  309. } else {
  310. // Shapes first
  311. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  312. tmpPath = subPaths[ i ];
  313. if ( THREE.Shape.Utils.isClockWise( tmpPath.getPoints() ) ) {
  314. if ( tmpShape ) shapes.push( tmpShape );
  315. tmpShape = new THREE.Shape();
  316. tmpShape.actions = tmpPath.actions;
  317. tmpShape.curves = tmpPath.curves;
  318. } else {
  319. tmpShape.holes.push( tmpPath );
  320. }
  321. }
  322. shapes.push( tmpShape );
  323. }
  324. //console.log("shape", shapes);
  325. return shapes;
  326. };