Path.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. var v, vlen = vectors.length;
  29. for ( v = 1; v < vlen; v++ ) {
  30. this.lineTo( vectors[ v ].x, vectors[ v ].y );
  31. };
  32. };
  33. // startPath() endPath()?
  34. THREE.Path.prototype.moveTo = function ( x, y ) {
  35. var args = Array.prototype.slice.call( arguments );
  36. this.actions.push( { action: THREE.PathActions.MOVE_TO, args: args } );
  37. };
  38. THREE.Path.prototype.lineTo = function ( x, y ) {
  39. var args = Array.prototype.slice.call( arguments );
  40. var lastargs = this.actions[ this.actions.length - 1 ].args;
  41. var x0 = lastargs[ lastargs.length - 2 ];
  42. var y0 = lastargs[ lastargs.length - 1 ];
  43. var curve = new THREE.LineCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( x, y ) );
  44. this.curves.push( curve );
  45. this.actions.push( { action: THREE.PathActions.LINE_TO, args: args } );
  46. };
  47. THREE.Path.prototype.quadraticCurveTo = function( aCPx, aCPy, aX, aY ) {
  48. var args = Array.prototype.slice.call( arguments );
  49. var lastargs = this.actions[ this.actions.length - 1 ].args;
  50. var x0 = lastargs[ lastargs.length - 2 ];
  51. var y0 = lastargs[ lastargs.length - 1 ];
  52. var curve = new THREE.QuadraticBezierCurve( new THREE.Vector2( x0, y0 ),
  53. new THREE.Vector2( aCPx, aCPy ),
  54. new THREE.Vector2( aX, aY ) );
  55. this.curves.push( curve );
  56. this.actions.push( { action: THREE.PathActions.QUADRATIC_CURVE_TO, args: args } );
  57. };
  58. THREE.Path.prototype.bezierCurveTo = function( aCP1x, aCP1y,
  59. aCP2x, aCP2y,
  60. aX, aY ) {
  61. var args = Array.prototype.slice.call( arguments );
  62. var lastargs = this.actions[ this.actions.length - 1 ].args;
  63. var x0 = lastargs[ lastargs.length - 2 ];
  64. var y0 = lastargs[ lastargs.length - 1 ];
  65. var curve = new THREE.CubicBezierCurve( new THREE.Vector2( x0, y0 ),
  66. new THREE.Vector2( aCP1x, aCP1y ),
  67. new THREE.Vector2( aCP2x, aCP2y ),
  68. new THREE.Vector2( aX, aY ) );
  69. this.curves.push( curve );
  70. this.actions.push( { action: THREE.PathActions.BEZIER_CURVE_TO, args: args } );
  71. };
  72. THREE.Path.prototype.splineThru = function( pts /*Array of Vector*/ ) {
  73. var args = Array.prototype.slice.call( arguments );
  74. var lastargs = this.actions[ this.actions.length - 1 ].args;
  75. var x0 = lastargs[ lastargs.length - 2 ];
  76. var y0 = lastargs[ lastargs.length - 1 ];
  77. var npts = [ new THREE.Vector2( x0, y0 ) ];
  78. npts = npts.concat( 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. var t;
  197. for ( j = 1; j <= tdivisions; j ++ ) {
  198. t = j / tdivisions;
  199. if ( !aClockwise ) {
  200. t = 1 - t;
  201. }
  202. angle = aStartAngle + t * deltaAngle;
  203. tx = lastx + aX + aRadius * Math.cos( angle );
  204. ty = lasty + aY + aRadius * Math.sin( angle );
  205. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  206. points.push( new THREE.Vector2( tx, ty ) );
  207. }
  208. //console.log(points);
  209. break;
  210. } // end switch
  211. }
  212. if ( closedPath ) {
  213. points.push( points[ 0 ] );
  214. }
  215. return points;
  216. };
  217. // This was used for testing purposes. Should be removed soon.
  218. THREE.Path.prototype.transform = function( path, segments ) {
  219. var bounds = this.getBoundingBox();
  220. var oldPts = this.getPoints( segments ); // getPoints getSpacedPoints
  221. //console.log( path.cacheArcLengths() );
  222. //path.getLengths(400);
  223. //segments = 40;
  224. return this.getWrapPoints( oldPts, path );
  225. };
  226. // Read http://www.tinaja.com/glib/nonlingr.pdf
  227. // nonlinear transforms
  228. THREE.Path.prototype.nltransform = function( a, b, c, d, e, f ) {
  229. // a - horizontal size
  230. // b - lean
  231. // c - x offset
  232. // d - vertical size
  233. // e - climb
  234. // f - y offset
  235. var oldPts = this.getPoints();
  236. var i, il, p, oldX, oldY;
  237. for ( i = 0, il = oldPts.length; i < il; i ++ ) {
  238. p = oldPts[i];
  239. oldX = p.x;
  240. oldY = p.y;
  241. p.x = a * oldX + b * oldY + c;
  242. p.y = d * oldY + e * oldX + f;
  243. }
  244. return oldPts;
  245. };
  246. // FUTURE Export JSON Format
  247. /* Draws this path onto a 2d canvas easily */
  248. THREE.Path.prototype.debug = function( canvas ) {
  249. var bounds = this.getBoundingBox();
  250. if ( !canvas ) {
  251. canvas = document.createElement( "canvas" );
  252. canvas.setAttribute( 'width', bounds.maxX + 100 );
  253. canvas.setAttribute( 'height', bounds.maxY + 100 );
  254. document.body.appendChild( canvas );
  255. }
  256. var ctx = canvas.getContext( "2d" );
  257. ctx.fillStyle = "white";
  258. ctx.fillRect( 0, 0, canvas.width, canvas.height );
  259. ctx.strokeStyle = "black";
  260. ctx.beginPath();
  261. var i, il, item, action, args;
  262. // Debug Path
  263. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  264. item = this.actions[ i ];
  265. args = item.args;
  266. action = item.action;
  267. // Short hand for now
  268. if ( action != THREE.PathActions.CSPLINE_THRU ) {
  269. ctx[ action ].apply( ctx, args );
  270. }
  271. /*
  272. switch ( action ) {
  273. case THREE.PathActions.MOVE_TO:
  274. ctx[ action ]( args[ 0 ], args[ 1 ] );
  275. break;
  276. case THREE.PathActions.LINE_TO:
  277. ctx[ action ]( args[ 0 ], args[ 1 ] );
  278. break;
  279. case THREE.PathActions.QUADRATIC_CURVE_TO:
  280. ctx[ action ]( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ] );
  281. break;
  282. case THREE.PathActions.CUBIC_CURVE_TO:
  283. ctx[ action ]( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], args[ 4 ], args[ 5 ] );
  284. break;
  285. }
  286. */
  287. }
  288. ctx.stroke();
  289. ctx.closePath();
  290. // Debug Points
  291. ctx.strokeStyle = "red";
  292. /* TO CLEAN UP */
  293. var p, points = this.getPoints();
  294. //var theta = -90 /180 * Math.PI;
  295. //var p, points = this.transform( 0.866, - 0.866,0, 0.500 , 0.50,-50 );
  296. //0.866, - 0.866,0, 0.500 , 0.50,-50
  297. // Math.cos(theta),Math.sin(theta),100,
  298. // Math.cos(theta),-Math.sin(theta),-50
  299. // translate, scale, rotation
  300. for ( i = 0, il = points.length; i < il; i ++ ) {
  301. p = points[ i ];
  302. ctx.beginPath();
  303. ctx.arc( p.x, p.y, 1.5, 0, Math.PI * 2, false );
  304. ctx.stroke();
  305. ctx.closePath();
  306. }
  307. };
  308. // Breaks path into shapes
  309. THREE.Path.prototype.toShapes = function() {
  310. var i, il, item, action, args;
  311. var subPaths = [], lastPath = new THREE.Path();
  312. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  313. item = this.actions[ i ];
  314. args = item.args;
  315. action = item.action;
  316. if ( action == THREE.PathActions.MOVE_TO ) {
  317. if ( lastPath.actions.length != 0 ) {
  318. subPaths.push( lastPath );
  319. lastPath = new THREE.Path();
  320. }
  321. }
  322. lastPath[ action ].apply( lastPath, args );
  323. }
  324. if ( lastPath.actions.length != 0 ) {
  325. subPaths.push( lastPath );
  326. }
  327. //console.log(subPaths);
  328. if ( subPaths.length == 0 ) return [];
  329. var holesFirst = !THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() );
  330. var tmpPath, tmpShape, shapes = [];
  331. //console.log("Holes first", holesFirst);
  332. if ( holesFirst ) {
  333. tmpShape = new THREE.Shape();
  334. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  335. tmpPath = subPaths[ i ];
  336. if ( THREE.Shape.Utils.isClockWise( tmpPath.getPoints() ) ) {
  337. tmpShape.actions = tmpPath.actions;
  338. tmpShape.curves = tmpPath.curves;
  339. shapes.push( tmpShape );
  340. tmpShape = new THREE.Shape();
  341. //console.log('cw', i);
  342. } else {
  343. tmpShape.holes.push( tmpPath );
  344. //console.log('ccw', i);
  345. }
  346. }
  347. } else {
  348. // Shapes first
  349. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  350. tmpPath = subPaths[ i ];
  351. if ( THREE.Shape.Utils.isClockWise( tmpPath.getPoints() ) ) {
  352. if ( tmpShape ) shapes.push( tmpShape );
  353. tmpShape = new THREE.Shape();
  354. tmpShape.actions = tmpPath.actions;
  355. tmpShape.curves = tmpPath.curves;
  356. } else {
  357. tmpShape.holes.push( tmpPath );
  358. }
  359. }
  360. shapes.push( tmpShape );
  361. }
  362. //console.log("shape", shapes);
  363. return shapes;
  364. };