Path.js 13.0 KB
Newer Older
Z
zz85 已提交
1 2
/**
 * @author zz85 / http://www.lab4games.net/zz85/blog
Z
zz85 已提交
3 4
 * Creates free form 2d path using series of points, lines or curves.
 *
Z
zz85 已提交
5 6
 **/

7 8
THREE.Path = function ( points ) {

9
	THREE.CurvePath.call(this);
10 11 12 13 14 15 16

	this.actions = [];

	if ( points ) {

		this.fromPoints( points );

Z
zz85 已提交
17
	}
18

Z
zz85 已提交
19 20
};

21 22 23 24
THREE.Path.prototype = new THREE.CurvePath();
THREE.Path.prototype.constructor = THREE.Path;


25 26
THREE.PathActions = {

Z
zz85 已提交
27 28
	MOVE_TO: 'moveTo',
	LINE_TO: 'lineTo',
Z
zz85 已提交
29 30 31 32
	QUADRATIC_CURVE_TO: 'quadraticCurveTo', // Bezier quadratic curve
	BEZIER_CURVE_TO: 'bezierCurveTo', 		// Bezier cubic curve
	CSPLINE_THRU: 'splineThru',				// Catmull-rom spline
	ARC: 'arc'								// Circle
33

Z
zz85 已提交
34 35
};

36
// TODO Clean up PATH API
37

38 39
// Create path using straight lines to connect all points
// - vectors: array of Vector2
40

41
THREE.Path.prototype.fromPoints = function ( vectors ) {
42 43 44

	this.moveTo( vectors[ 0 ].x, vectors[ 0 ].y );

Z
zz85 已提交
45
	for ( var v = 1, vlen = vectors.length; v < vlen; v ++ ) {
46 47 48

		this.lineTo( vectors[ v ].x, vectors[ v ].y );

Z
zz85 已提交
49
	};
50

Z
zz85 已提交
51 52
};

Z
zz85 已提交
53 54
// startPath() endPath()?

55
THREE.Path.prototype.moveTo = function ( x, y ) {
56 57 58 59

	var args = Array.prototype.slice.call( arguments );
	this.actions.push( { action: THREE.PathActions.MOVE_TO, args: args } );

Z
zz85 已提交
60 61
};

62
THREE.Path.prototype.lineTo = function ( x, y ) {
63 64

	var args = Array.prototype.slice.call( arguments );
65

Z
curves  
zz85 已提交
66
	var lastargs = this.actions[ this.actions.length - 1 ].args;
67

Z
curves  
zz85 已提交
68 69
	var x0 = lastargs[ lastargs.length - 2 ];
	var y0 = lastargs[ lastargs.length - 1 ];
70

71
	var curve = new THREE.LineCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( x, y ) );
Z
zz85 已提交
72
	this.curves.push( curve );
73

74
	this.actions.push( { action: THREE.PathActions.LINE_TO, args: args } );
75

Z
zz85 已提交
76 77
};

78 79 80
THREE.Path.prototype.quadraticCurveTo = function( aCPx, aCPy, aX, aY ) {

	var args = Array.prototype.slice.call( arguments );
81

Z
curves  
zz85 已提交
82
	var lastargs = this.actions[ this.actions.length - 1 ].args;
83

Z
curves  
zz85 已提交
84 85
	var x0 = lastargs[ lastargs.length - 2 ];
	var y0 = lastargs[ lastargs.length - 1 ];
86

87 88 89
	var curve = new THREE.QuadraticBezierCurve( new THREE.Vector2( x0, y0 ),
												new THREE.Vector2( aCPx, aCPy ),
												new THREE.Vector2( aX, aY ) );
Z
zz85 已提交
90
	this.curves.push( curve );
91

92
	this.actions.push( { action: THREE.PathActions.QUADRATIC_CURVE_TO, args: args } );
93

Z
zz85 已提交
94 95
};

96 97
THREE.Path.prototype.bezierCurveTo = function( aCP1x, aCP1y,
                                               aCP2x, aCP2y,
98
                                               aX, aY ) {
99 100

	var args = Array.prototype.slice.call( arguments );
101

Z
curves  
zz85 已提交
102
	var lastargs = this.actions[ this.actions.length - 1 ].args;
103

Z
curves  
zz85 已提交
104 105 106
	var x0 = lastargs[ lastargs.length - 2 ];
	var y0 = lastargs[ lastargs.length - 1 ];

107 108 109 110
	var curve = new THREE.CubicBezierCurve( new THREE.Vector2( x0, y0 ),
											new THREE.Vector2( aCP1x, aCP1y ),
											new THREE.Vector2( aCP2x, aCP2y ),
											new THREE.Vector2( aX, aY ) );
Z
zz85 已提交
111
	this.curves.push( curve );
112

113
	this.actions.push( { action: THREE.PathActions.BEZIER_CURVE_TO, args: args } );
114

Z
zz85 已提交
115 116
};

Z
zz85 已提交
117
THREE.Path.prototype.splineThru = function( pts /*Array of Vector*/ ) {
118

Z
zz85 已提交
119
	var args = Array.prototype.slice.call( arguments );
Z
curves  
zz85 已提交
120
	var lastargs = this.actions[ this.actions.length - 1 ].args;
121

Z
curves  
zz85 已提交
122 123
	var x0 = lastargs[ lastargs.length - 2 ];
	var y0 = lastargs[ lastargs.length - 1 ];
Z
zz85 已提交
124
//---
125
	var npts = [ new THREE.Vector2( x0, y0 ) ];
126
	Array.prototype.push.apply( npts, pts );
127

Z
zz85 已提交
128 129
	var curve = new THREE.SplineCurve( npts );
	this.curves.push( curve );
130

131
	this.actions.push( { action: THREE.PathActions.CSPLINE_THRU, args: args } );
132

133
};
Z
zz85 已提交
134

Z
zz85 已提交
135
// FUTURE: Change the API or follow canvas API?
136 137
// TODO ARC ( x, y, x - radius, y - radius, startAngle, endAngle )

138 139
THREE.Path.prototype.arc = function ( aX, aY, aRadius,
									  aStartAngle, aEndAngle, aClockwise ) {
140

Z
zz85 已提交
141
	var args = Array.prototype.slice.call( arguments );
A
alteredq 已提交
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	var laste = this.actions[ this.actions.length - 1];

	var curve = new THREE.ArcCurve( laste.x + aX, laste.y + aY, aRadius,
									aStartAngle, aEndAngle, aClockwise );
	this.curves.push( curve );

	// All of the other actions look to the last two elements in the list to
	// find the ending point, so we need to append them.
	var lastPoint = curve.getPoint(aClockwise ? 1 : 0);
	args.push(lastPoint.x);
	args.push(lastPoint.y);

	this.actions.push( { action: THREE.PathActions.ARC, args: args } );

 };

THREE.Path.prototype.absarc = function ( aX, aY, aRadius,
									  aStartAngle, aEndAngle, aClockwise ) {

	var args = Array.prototype.slice.call( arguments );

Z
zz85 已提交
164
	var curve = new THREE.ArcCurve( aX, aY, aRadius,
165
									aStartAngle, aEndAngle, aClockwise );
Z
zz85 已提交
166
	this.curves.push( curve );
A
alteredq 已提交
167 168 169

	// console.log( 'arc', args );

170 171 172 173 174 175
        // All of the other actions look to the last two elements in the list to
        // find the ending point, so we need to append them.
        var lastPoint = curve.getPoint(aClockwise ? 1 : 0);
        args.push(lastPoint.x);
        args.push(lastPoint.y);

Z
zz85 已提交
176 177 178 179
	this.actions.push( { action: THREE.PathActions.ARC, args: args } );

 };

Z
zz85 已提交
180

181
THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
182

Z
zz85 已提交
183
	if ( ! divisions ) divisions = 40;
184

185 186
	var points = [];

187
	for ( var i = 0; i < divisions; i ++ ) {
188

189
		points.push( this.getPoint( i / divisions ) );
190

191
		//if( !this.getPoint( i / divisions ) ) throw "DIE";
192

Z
zz85 已提交
193
	}
Z
zz85 已提交
194

195
	// if ( closedPath ) {
196
	//
197
	// 	points.push( points[ 0 ] );
198
	//
199
	// }
200 201

	return points;
202 203

};
Z
zz85 已提交
204

Z
zz85 已提交
205
/* Return an array of vectors based on contour of the path */
206

A
alteredq 已提交
207
THREE.Path.prototype.getPoints = function( divisions, closedPath ) {
208

Z
zz85 已提交
209 210
	divisions = divisions || 12;

211 212 213 214 215 216 217
	var points = [];

	var i, il, item, action, args;
	var cpx, cpy, cpx2, cpy2, cpx1, cpy1, cpx0, cpy0,
		laste, j,
		t, tx, ty;

218
	for ( i = 0, il = this.actions.length; i < il; i ++ ) {
219 220 221 222 223 224 225 226 227 228

		item = this.actions[ i ];

		action = item.action;
		args = item.args;

		switch( action ) {

		case THREE.PathActions.MOVE_TO:

229
			points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
Z
zz85 已提交
230 231 232

			break;

233 234 235 236
		case THREE.PathActions.LINE_TO:

			points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );

Z
zz85 已提交
237 238
			break;

239 240 241 242 243 244 245 246 247 248 249
		case THREE.PathActions.QUADRATIC_CURVE_TO:

			cpx  = args[ 2 ];
			cpy  = args[ 3 ];

			cpx1 = args[ 0 ];
			cpy1 = args[ 1 ];

			if ( points.length > 0 ) {

				laste = points[ points.length - 1 ];
Z
zz85 已提交
250 251 252

				cpx0 = laste.x;
				cpy0 = laste.y;
253

Z
zz85 已提交
254 255
			} else {

256 257 258 259
				laste = this.actions[ i - 1 ].args;

				cpx0 = laste[ laste.length - 2 ];
				cpy0 = laste[ laste.length - 1 ];
Z
zz85 已提交
260 261 262

			}

263 264 265 266
			for ( j = 1; j <= divisions; j ++ ) {

				t = j / divisions;

267 268
				tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
				ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
269 270 271 272 273

				points.push( new THREE.Vector2( tx, ty ) );

		  	}

Z
zz85 已提交
274 275
			break;

276
		case THREE.PathActions.BEZIER_CURVE_TO:
Z
zz85 已提交
277

278 279
			cpx  = args[ 4 ];
			cpy  = args[ 5 ];
Z
zz85 已提交
280

281 282
			cpx1 = args[ 0 ];
			cpy1 = args[ 1 ];
Z
zz85 已提交
283

284 285
			cpx2 = args[ 2 ];
			cpy2 = args[ 3 ];
Z
zz85 已提交
286

287
			if ( points.length > 0 ) {
Z
zz85 已提交
288

289
				laste = points[ points.length - 1 ];
Z
zz85 已提交
290

291 292
				cpx0 = laste.x;
				cpy0 = laste.y;
Z
zz85 已提交
293

294
			} else {
Z
zz85 已提交
295

296
				laste = this.actions[ i - 1 ].args;
Z
zz85 已提交
297

298 299
				cpx0 = laste[ laste.length - 2 ];
				cpy0 = laste[ laste.length - 1 ];
Z
zz85 已提交
300

301
			}
Z
zz85 已提交
302 303


304
			for ( j = 1; j <= divisions; j ++ ) {
Z
zz85 已提交
305

306
				t = j / divisions;
Z
zz85 已提交
307

308 309
				tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx );
				ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy );
Z
zz85 已提交
310

311
				points.push( new THREE.Vector2( tx, ty ) );
Z
zz85 已提交
312

313
			}
Z
zz85 已提交
314

315
			break;
Z
zz85 已提交
316

Z
zz85 已提交
317
		case THREE.PathActions.CSPLINE_THRU:
318

319
			laste = this.actions[ i - 1 ].args;
320

321
			var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] );
322
			var spts = [ last ];
323

Z
zz85 已提交
324
			var n = divisions * args[ 0 ].length;
325

326
			spts = spts.concat( args[ 0 ] );
327

328
			var spline = new THREE.SplineCurve( spts );
329

Z
zz85 已提交
330
			for ( j = 1; j <= n; j ++ ) {
331

Z
zz85 已提交
332
				points.push( spline.getPointAt( j / n ) ) ;
333

334
			}
335

Z
zz85 已提交
336
			break;
Z
zz85 已提交
337

Z
zz85 已提交
338
		case THREE.PathActions.ARC:
Z
zz85 已提交
339

Z
zz85 已提交
340
			laste = this.actions[ i - 1 ].args;
Z
zz85 已提交
341

342 343 344 345
			var aX = args[ 0 ], aY = args[ 1 ],
				aRadius = args[ 2 ],
				aStartAngle = args[ 3 ], aEndAngle = args[ 4 ],
				aClockwise = !!args[ 5 ];
Z
zz85 已提交
346

347

Z
zz85 已提交
348 349
			var deltaAngle = aEndAngle - aStartAngle;
			var angle;
Z
zz85 已提交
350
			var tdivisions = divisions * 2;
351

Z
zz85 已提交
352
			for ( j = 1; j <= tdivisions; j ++ ) {
353

354
				t = j / tdivisions;
355

Z
zz85 已提交
356
				if ( ! aClockwise ) {
357

Z
zz85 已提交
358
					t = 1 - t;
359

Z
zz85 已提交
360
				}
361

Z
zz85 已提交
362
				angle = aStartAngle + t * deltaAngle;
363

364 365
				tx = aX + aRadius * Math.cos( angle );
				ty = aY + aRadius * Math.sin( angle );
366

Z
zz85 已提交
367
				//console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
368

Z
zz85 已提交
369
				points.push( new THREE.Vector2( tx, ty ) );
370

Z
zz85 已提交
371
			}
372

Z
zz85 已提交
373
			//console.log(points);
Z
zz85 已提交
374 375 376 377

		  break;

		} // end switch
Z
zz85 已提交
378

379 380
	}

381 382 383 384 385 386 387 388


	// Normalize to remove the closing point by default.
	var lastPoint = points[ points.length - 1];
	var EPSILON = 0.0000000001;
	if ( Math.abs(lastPoint.x - points[ 0 ].x) < EPSILON &&
             Math.abs(lastPoint.y - points[ 0 ].y) < EPSILON)
		points.splice( points.length - 1, 1);
A
alteredq 已提交
389 390 391 392 393 394
	if ( closedPath ) {

		points.push( points[ 0 ] );

	}

395
	return points;
Z
zz85 已提交
396

397
};
Z
zz85 已提交
398

Z
zz85 已提交
399

Z
zz85 已提交
400

401
// This was used for testing purposes. Should be removed soon.
Z
zz85 已提交
402

403
THREE.Path.prototype.transform = function( path, segments ) {
404

405 406
	var bounds = this.getBoundingBox();
	var oldPts = this.getPoints( segments ); // getPoints getSpacedPoints
407

408 409 410
	//console.log( path.cacheArcLengths() );
	//path.getLengths(400);
	//segments = 40;
411

412
	return this.getWrapPoints( oldPts, path );
413

414 415 416
};

// Read http://www.tinaja.com/glib/nonlingr.pdf
Z
zz85 已提交
417
// nonlinear transforms
418

419 420 421 422 423 424 425 426 427
THREE.Path.prototype.nltransform = function( a, b, c, d, e, f ) {

	// a - horizontal size
	// b - lean
	// c - x offset
	// d - vertical size
	// e - climb
	// f - y offset

428
	var oldPts = this.getPoints();
429

430
	var i, il, p, oldX, oldY;
431 432 433

	for ( i = 0, il = oldPts.length; i < il; i ++ ) {

434
		p = oldPts[i];
435

436 437
		oldX = p.x;
		oldY = p.y;
438

439 440
		p.x = a * oldX + b * oldY + c;
		p.y = d * oldY + e * oldX + f;
441

442
	}
443

444
	return oldPts;
445

446 447
};

Z
zz85 已提交
448 449 450

// FUTURE Export JSON Format

451
/* Draws this path onto a 2d canvas easily */
Z
zz85 已提交
452

453
THREE.Path.prototype.debug = function( canvas ) {
Z
zz85 已提交
454

455
	var bounds = this.getBoundingBox();
456 457

	if ( !canvas ) {
Z
zz85 已提交
458

459
		canvas = document.createElement( "canvas" );
Z
zz85 已提交
460

461 462
		canvas.setAttribute( 'width',  bounds.maxX + 100 );
		canvas.setAttribute( 'height', bounds.maxY + 100 );
Z
zz85 已提交
463

464 465 466
		document.body.appendChild( canvas );

	}
Z
zz85 已提交
467

468 469
	var ctx = canvas.getContext( "2d" );
	ctx.fillStyle = "white";
470
	ctx.fillRect( 0, 0, canvas.width, canvas.height );
Z
zz85 已提交
471

472 473
	ctx.strokeStyle = "black";
	ctx.beginPath();
Z
zz85 已提交
474

475
	var i, il, item, action, args;
Z
zz85 已提交
476

477
	// Debug Path
Z
zz85 已提交
478

479
	for ( i = 0, il = this.actions.length; i < il; i ++ ) {
Z
zz85 已提交
480

481
		item = this.actions[ i ];
Z
zz85 已提交
482

483 484
		args = item.args;
		action = item.action;
Z
zz85 已提交
485

486
		// Short hand for now
Z
zz85 已提交
487

488 489 490 491 492
		if ( action != THREE.PathActions.CSPLINE_THRU ) {

			ctx[ action ].apply( ctx, args );

		}
Z
zz85 已提交
493

494 495
		/*
		switch ( action ) {
Z
zz85 已提交
496

497
			case THREE.PathActions.MOVE_TO:
Z
zz85 已提交
498

499 500
				ctx[ action ]( args[ 0 ], args[ 1 ] );
				break;
Z
zz85 已提交
501

502
			case THREE.PathActions.LINE_TO:
Z
zz85 已提交
503

504 505
				ctx[ action ]( args[ 0 ], args[ 1 ] );
				break;
Z
zz85 已提交
506

507
			case THREE.PathActions.QUADRATIC_CURVE_TO:
Z
zz85 已提交
508

509 510
				ctx[ action ]( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ] );
				break;
Z
zz85 已提交
511

512
			case THREE.PathActions.CUBIC_CURVE_TO:
Z
zz85 已提交
513

514 515
				ctx[ action ]( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], args[ 4 ], args[ 5 ] );
				break;
Z
zz85 已提交
516

517 518
		}
		*/
Z
zz85 已提交
519 520 521

	}

522 523 524
	ctx.stroke();
	ctx.closePath();

Z
zz85 已提交
525
	// Debug Points
Z
zz85 已提交
526

527
	ctx.strokeStyle = "red";
Z
zz85 已提交
528

Z
zz85 已提交
529
	/* TO CLEAN UP */
530

531
	var p, points = this.getPoints();
532

533
	for ( i = 0, il = points.length; i < il; i ++ ) {
Z
zz85 已提交
534

535
		p = points[ i ];
Z
zz85 已提交
536

537 538 539 540
		ctx.beginPath();
		ctx.arc( p.x, p.y, 1.5, 0, Math.PI * 2, false );
		ctx.stroke();
		ctx.closePath();
Z
zz85 已提交
541

542
	}
Z
zz85 已提交
543

544
};
545 546

// Breaks path into shapes
547

548
THREE.Path.prototype.toShapes = function() {
549

550 551 552 553 554 555 556 557 558 559
	var i, il, item, action, args;

	var subPaths = [], lastPath = new THREE.Path();

	for ( i = 0, il = this.actions.length; i < il; i ++ ) {

		item = this.actions[ i ];

		args = item.args;
		action = item.action;
560 561 562 563 564 565

		if ( action == THREE.PathActions.MOVE_TO ) {

			if ( lastPath.actions.length != 0 ) {

				subPaths.push( lastPath );
566
				lastPath = new THREE.Path();
567

568
			}
569

570
		}
571 572 573

		lastPath[ action ].apply( lastPath, args );

574
	}
575 576 577 578 579

	if ( lastPath.actions.length != 0 ) {

		subPaths.push( lastPath );

580
	}
581

Z
zz85 已提交
582
	// console.log(subPaths);
583

584
	if ( subPaths.length == 0 ) return [];
585

Z
zz85 已提交
586 587
	var tmpPath, tmpShape, shapes = [];

588
	var holesFirst = !THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() );
Z
zz85 已提交
589 590
	// console.log("Holes first", holesFirst);

Z
zz85 已提交
591 592 593 594 595
	if ( subPaths.length == 1) {
		tmpPath = subPaths[0];
		tmpShape = new THREE.Shape();
		tmpShape.actions = tmpPath.actions;
		tmpShape.curves = tmpPath.curves;
Z
zz85 已提交
596 597
		shapes.push( tmpShape );
		return shapes;
Z
zz85 已提交
598
	};
599 600 601

	if ( holesFirst ) {

602
		tmpShape = new THREE.Shape();
603 604 605 606 607 608 609

		for ( i = 0, il = subPaths.length; i < il; i ++ ) {

			tmpPath = subPaths[ i ];

			if ( THREE.Shape.Utils.isClockWise( tmpPath.getPoints() ) ) {

610 611
				tmpShape.actions = tmpPath.actions;
				tmpShape.curves = tmpPath.curves;
612 613

				shapes.push( tmpShape );
614
				tmpShape = new THREE.Shape();
615

616
				//console.log('cw', i);
617

618
			} else {
619 620 621

				tmpShape.holes.push( tmpPath );

622
				//console.log('ccw', i);
623

624
			}
625

626
		}
627

628
	} else {
629

630
		// Shapes first
631 632 633 634 635 636 637 638 639 640

		for ( i = 0, il = subPaths.length; i < il; i ++ ) {

			tmpPath = subPaths[ i ];

			if ( THREE.Shape.Utils.isClockWise( tmpPath.getPoints() ) ) {


				if ( tmpShape ) shapes.push( tmpShape );

641 642 643
				tmpShape = new THREE.Shape();
				tmpShape.actions = tmpPath.actions;
				tmpShape.curves = tmpPath.curves;
644

645
			} else {
646 647 648

				tmpShape.holes.push( tmpPath );

649
			}
650

651
		}
652 653 654

		shapes.push( tmpShape );

655
	}
656

657
	//console.log("shape", shapes);
658

659
	return shapes;
660

Z
zz85 已提交
661
};