Path.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 = new THREE.CurvePath();
  14. THREE.Path.prototype.constructor = THREE.Path;
  15. THREE.PathActions = {
  16. MOVE_TO: 'moveTo',
  17. LINE_TO: 'lineTo',
  18. QUADRATIC_CURVE_TO: 'quadraticCurveTo', // Bezier quadratic curve
  19. BEZIER_CURVE_TO: 'bezierCurveTo', // Bezier cubic curve
  20. CSPLINE_THRU: 'splineThru', // Catmull-rom spline
  21. ARC: 'arc' // Circle
  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. // TODO ARC ( x, y, x - radius, y - radius, startAngle, endAngle )
  85. THREE.Path.prototype.arc = function ( aX, aY, aRadius,
  86. aStartAngle, aEndAngle, aClockwise ) {
  87. var args = Array.prototype.slice.call( arguments );
  88. var curve = new THREE.ArcCurve( aX, aY, aRadius,
  89. aStartAngle, aEndAngle, aClockwise );
  90. this.curves.push( curve );
  91. // console.log( 'arc', args );
  92. this.actions.push( { action: THREE.PathActions.ARC, args: args } );
  93. };
  94. THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
  95. if ( ! divisions ) divisions = 40;
  96. var points = [];
  97. for ( var i = 0; i < divisions; i ++ ) {
  98. points.push( this.getPoint( i / divisions ) );
  99. //if( !this.getPoint( i / divisions ) ) throw "DIE";
  100. }
  101. // if ( closedPath ) {
  102. //
  103. // points.push( points[ 0 ] );
  104. //
  105. // }
  106. return points;
  107. };
  108. /* Return an array of vectors based on contour of the path */
  109. THREE.Path.prototype.getPoints = function( divisions, closedPath ) {
  110. divisions = divisions || 12;
  111. var points = [];
  112. var i, il, item, action, args;
  113. var cpx, cpy, cpx2, cpy2, cpx1, cpy1, cpx0, cpy0,
  114. laste, j,
  115. t, tx, ty;
  116. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  117. item = this.actions[ i ];
  118. action = item.action;
  119. args = item.args;
  120. switch( action ) {
  121. case THREE.PathActions.MOVE_TO:
  122. // points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  123. break;
  124. case THREE.PathActions.LINE_TO:
  125. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  126. break;
  127. case THREE.PathActions.QUADRATIC_CURVE_TO:
  128. cpx = args[ 2 ];
  129. cpy = args[ 3 ];
  130. cpx1 = args[ 0 ];
  131. cpy1 = args[ 1 ];
  132. if ( points.length > 0 ) {
  133. laste = points[ points.length - 1 ];
  134. cpx0 = laste.x;
  135. cpy0 = laste.y;
  136. } else {
  137. laste = this.actions[ i - 1 ].args;
  138. cpx0 = laste[ laste.length - 2 ];
  139. cpy0 = laste[ laste.length - 1 ];
  140. }
  141. for ( j = 1; j <= divisions; j ++ ) {
  142. t = j / divisions;
  143. tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
  144. ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
  145. points.push( new THREE.Vector2( tx, ty ) );
  146. }
  147. break;
  148. case THREE.PathActions.BEZIER_CURVE_TO:
  149. cpx = args[ 4 ];
  150. cpy = args[ 5 ];
  151. cpx1 = args[ 0 ];
  152. cpy1 = args[ 1 ];
  153. cpx2 = args[ 2 ];
  154. cpy2 = args[ 3 ];
  155. if ( points.length > 0 ) {
  156. laste = points[ points.length - 1 ];
  157. cpx0 = laste.x;
  158. cpy0 = laste.y;
  159. } else {
  160. laste = this.actions[ i - 1 ].args;
  161. cpx0 = laste[ laste.length - 2 ];
  162. cpy0 = laste[ laste.length - 1 ];
  163. }
  164. for ( j = 1; j <= divisions; j ++ ) {
  165. t = j / divisions;
  166. tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx );
  167. ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy );
  168. points.push( new THREE.Vector2( tx, ty ) );
  169. }
  170. break;
  171. case THREE.PathActions.CSPLINE_THRU:
  172. laste = this.actions[ i - 1 ].args;
  173. var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] );
  174. var spts = [ last ];
  175. var n = divisions * args[ 0 ].length;
  176. spts = spts.concat( args[ 0 ] );
  177. var spline = new THREE.SplineCurve( spts );
  178. for ( j = 1; j <= n; j ++ ) {
  179. points.push( spline.getPointAt( j / n ) ) ;
  180. }
  181. break;
  182. case THREE.PathActions.ARC:
  183. laste = this.actions[ i - 1 ].args;
  184. var aX = args[ 0 ], aY = args[ 1 ],
  185. aRadius = args[ 2 ],
  186. aStartAngle = args[ 3 ], aEndAngle = args[ 4 ],
  187. aClockwise = !!args[ 5 ];
  188. var lastx = laste[ laste.length - 2 ],
  189. lasty = laste[ laste.length - 1 ];
  190. if ( laste.length == 0 ) {
  191. lastx = lasty = 0;
  192. }
  193. var deltaAngle = aEndAngle - aStartAngle;
  194. var angle;
  195. var tdivisions = divisions * 2;
  196. for ( j = 1; j <= tdivisions; j ++ ) {
  197. t = j / tdivisions;
  198. if ( ! aClockwise ) {
  199. t = 1 - t;
  200. }
  201. angle = aStartAngle + t * deltaAngle;
  202. tx = lastx + aX + aRadius * Math.cos( angle );
  203. ty = lasty + aY + aRadius * Math.sin( angle );
  204. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  205. points.push( new THREE.Vector2( tx, ty ) );
  206. }
  207. //console.log(points);
  208. break;
  209. } // end switch
  210. }
  211. if ( closedPath ) {
  212. points.push( points[ 0 ] );
  213. }
  214. return points;
  215. };
  216. // This was used for testing purposes. Should be removed soon.
  217. THREE.Path.prototype.transform = function( path, segments ) {
  218. var bounds = this.getBoundingBox();
  219. var oldPts = this.getPoints( segments ); // getPoints getSpacedPoints
  220. //console.log( path.cacheArcLengths() );
  221. //path.getLengths(400);
  222. //segments = 40;
  223. return this.getWrapPoints( oldPts, path );
  224. };
  225. // Read http://www.tinaja.com/glib/nonlingr.pdf
  226. // nonlinear transforms
  227. THREE.Path.prototype.nltransform = function( a, b, c, d, e, f ) {
  228. // a - horizontal size
  229. // b - lean
  230. // c - x offset
  231. // d - vertical size
  232. // e - climb
  233. // f - y offset
  234. var oldPts = this.getPoints();
  235. var i, il, p, oldX, oldY;
  236. for ( i = 0, il = oldPts.length; i < il; i ++ ) {
  237. p = oldPts[i];
  238. oldX = p.x;
  239. oldY = p.y;
  240. p.x = a * oldX + b * oldY + c;
  241. p.y = d * oldY + e * oldX + f;
  242. }
  243. return oldPts;
  244. };
  245. // FUTURE Export JSON Format
  246. /* Draws this path onto a 2d canvas easily */
  247. THREE.Path.prototype.debug = function( canvas ) {
  248. var bounds = this.getBoundingBox();
  249. if ( !canvas ) {
  250. canvas = document.createElement( "canvas" );
  251. canvas.setAttribute( 'width', bounds.maxX + 100 );
  252. canvas.setAttribute( 'height', bounds.maxY + 100 );
  253. document.body.appendChild( canvas );
  254. }
  255. var ctx = canvas.getContext( "2d" );
  256. ctx.fillStyle = "white";
  257. ctx.fillRect( 0, 0, canvas.width, canvas.height );
  258. ctx.strokeStyle = "black";
  259. ctx.beginPath();
  260. var i, il, item, action, args;
  261. // Debug Path
  262. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  263. item = this.actions[ i ];
  264. args = item.args;
  265. action = item.action;
  266. // Short hand for now
  267. if ( action != THREE.PathActions.CSPLINE_THRU ) {
  268. ctx[ action ].apply( ctx, args );
  269. }
  270. /*
  271. switch ( action ) {
  272. case THREE.PathActions.MOVE_TO:
  273. ctx[ action ]( args[ 0 ], args[ 1 ] );
  274. break;
  275. case THREE.PathActions.LINE_TO:
  276. ctx[ action ]( args[ 0 ], args[ 1 ] );
  277. break;
  278. case THREE.PathActions.QUADRATIC_CURVE_TO:
  279. ctx[ action ]( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ] );
  280. break;
  281. case THREE.PathActions.CUBIC_CURVE_TO:
  282. ctx[ action ]( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], args[ 4 ], args[ 5 ] );
  283. break;
  284. }
  285. */
  286. }
  287. ctx.stroke();
  288. ctx.closePath();
  289. // Debug Points
  290. ctx.strokeStyle = "red";
  291. /* TO CLEAN UP */
  292. var p, points = this.getPoints();
  293. for ( i = 0, il = points.length; i < il; i ++ ) {
  294. p = points[ i ];
  295. ctx.beginPath();
  296. ctx.arc( p.x, p.y, 1.5, 0, Math.PI * 2, false );
  297. ctx.stroke();
  298. ctx.closePath();
  299. }
  300. };
  301. // Breaks path into shapes
  302. THREE.Path.prototype.toShapes = function() {
  303. var i, il, item, action, args;
  304. var subPaths = [], lastPath = new THREE.Path();
  305. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  306. item = this.actions[ i ];
  307. args = item.args;
  308. action = item.action;
  309. if ( action == THREE.PathActions.MOVE_TO ) {
  310. if ( lastPath.actions.length != 0 ) {
  311. subPaths.push( lastPath );
  312. lastPath = new THREE.Path();
  313. }
  314. }
  315. lastPath[ action ].apply( lastPath, args );
  316. }
  317. if ( lastPath.actions.length != 0 ) {
  318. subPaths.push( lastPath );
  319. }
  320. // console.log(subPaths);
  321. if ( subPaths.length == 0 ) return [];
  322. var tmpPath, tmpShape, shapes = [];
  323. var holesFirst = !THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() );
  324. // console.log("Holes first", holesFirst);
  325. if ( subPaths.length == 1) {
  326. tmpPath = subPaths[0];
  327. tmpShape = new THREE.Shape();
  328. tmpShape.actions = tmpPath.actions;
  329. tmpShape.curves = tmpPath.curves;
  330. shapes.push( tmpShape );
  331. return shapes;
  332. };
  333. if ( holesFirst ) {
  334. tmpShape = new THREE.Shape();
  335. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  336. tmpPath = subPaths[ i ];
  337. if ( THREE.Shape.Utils.isClockWise( tmpPath.getPoints() ) ) {
  338. tmpShape.actions = tmpPath.actions;
  339. tmpShape.curves = tmpPath.curves;
  340. shapes.push( tmpShape );
  341. tmpShape = new THREE.Shape();
  342. //console.log('cw', i);
  343. } else {
  344. tmpShape.holes.push( tmpPath );
  345. //console.log('ccw', i);
  346. }
  347. }
  348. } else {
  349. // Shapes first
  350. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  351. tmpPath = subPaths[ i ];
  352. if ( THREE.Shape.Utils.isClockWise( tmpPath.getPoints() ) ) {
  353. if ( tmpShape ) shapes.push( tmpShape );
  354. tmpShape = new THREE.Shape();
  355. tmpShape.actions = tmpPath.actions;
  356. tmpShape.curves = tmpPath.curves;
  357. } else {
  358. tmpShape.holes.push( tmpPath );
  359. }
  360. }
  361. shapes.push( tmpShape );
  362. }
  363. //console.log("shape", shapes);
  364. return shapes;
  365. };