Path.js 13 KB

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