Path.js 11.9 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

Z
zz85 已提交
143
	var curve = new THREE.ArcCurve( aX, aY, aRadius,
144
									aStartAngle, aEndAngle, aClockwise );
Z
zz85 已提交
145
	this.curves.push( curve );
A
alteredq 已提交
146 147 148

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

Z
zz85 已提交
149 150 151 152
	this.actions.push( { action: THREE.PathActions.ARC, args: args } );

 };

Z
zz85 已提交
153

154
THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
155

Z
zz85 已提交
156
	if ( ! divisions ) divisions = 40;
157

158 159
	var points = [];

160
	for ( var i = 0; i < divisions; i ++ ) {
161

162
		points.push( this.getPoint( i / divisions ) );
163

164
		//if( !this.getPoint( i / divisions ) ) throw "DIE";
165

Z
zz85 已提交
166
	}
Z
zz85 已提交
167

168
	// if ( closedPath ) {
169
	//
170
	// 	points.push( points[ 0 ] );
171
	//
172
	// }
173 174

	return points;
175 176

};
Z
zz85 已提交
177

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

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

Z
zz85 已提交
182 183
	divisions = divisions || 12;

184 185 186 187 188 189 190
	var points = [];

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

191
	for ( i = 0, il = this.actions.length; i < il; i ++ ) {
192 193 194 195 196 197 198 199 200 201

		item = this.actions[ i ];

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

		switch( action ) {

		case THREE.PathActions.MOVE_TO:

A
alteredq 已提交
202
			// points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
Z
zz85 已提交
203 204 205

			break;

206 207 208 209
		case THREE.PathActions.LINE_TO:

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

Z
zz85 已提交
210 211
			break;

212 213 214 215 216 217 218 219 220 221 222
		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 已提交
223 224 225

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

Z
zz85 已提交
227 228
			} else {

229 230 231 232
				laste = this.actions[ i - 1 ].args;

				cpx0 = laste[ laste.length - 2 ];
				cpy0 = laste[ laste.length - 1 ];
Z
zz85 已提交
233 234 235

			}

236 237 238 239
			for ( j = 1; j <= divisions; j ++ ) {

				t = j / divisions;

240 241
				tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
				ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
242 243 244 245 246

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

		  	}

Z
zz85 已提交
247 248
			break;

249
		case THREE.PathActions.BEZIER_CURVE_TO:
Z
zz85 已提交
250

251 252
			cpx  = args[ 4 ];
			cpy  = args[ 5 ];
Z
zz85 已提交
253

254 255
			cpx1 = args[ 0 ];
			cpy1 = args[ 1 ];
Z
zz85 已提交
256

257 258
			cpx2 = args[ 2 ];
			cpy2 = args[ 3 ];
Z
zz85 已提交
259

260
			if ( points.length > 0 ) {
Z
zz85 已提交
261

262
				laste = points[ points.length - 1 ];
Z
zz85 已提交
263

264 265
				cpx0 = laste.x;
				cpy0 = laste.y;
Z
zz85 已提交
266

267
			} else {
Z
zz85 已提交
268

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

271 272
				cpx0 = laste[ laste.length - 2 ];
				cpy0 = laste[ laste.length - 1 ];
Z
zz85 已提交
273

274
			}
Z
zz85 已提交
275 276


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

279
				t = j / divisions;
Z
zz85 已提交
280

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

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

286
			}
Z
zz85 已提交
287

288
			break;
Z
zz85 已提交
289

Z
zz85 已提交
290
		case THREE.PathActions.CSPLINE_THRU:
291

292
			laste = this.actions[ i - 1 ].args;
293

294
			var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] );
295
			var spts = [ last ];
296

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

299
			spts = spts.concat( args[ 0 ] );
300

301
			var spline = new THREE.SplineCurve( spts );
302

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

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

307
			}
308

Z
zz85 已提交
309
			break;
Z
zz85 已提交
310

Z
zz85 已提交
311
		case THREE.PathActions.ARC:
Z
zz85 已提交
312

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

315 316 317 318
			var aX = args[ 0 ], aY = args[ 1 ],
				aRadius = args[ 2 ],
				aStartAngle = args[ 3 ], aEndAngle = args[ 4 ],
				aClockwise = !!args[ 5 ];
Z
zz85 已提交
319

Z
zz85 已提交
320
			var lastx = laste[ laste.length - 2 ],
321
				lasty = laste[ laste.length - 1 ];
Z
zz85 已提交
322

323
			if ( laste.length == 0 ) {
324

Z
zz85 已提交
325
				lastx = lasty = 0;
326

Z
zz85 已提交
327
			}
328 329


Z
zz85 已提交
330 331
			var deltaAngle = aEndAngle - aStartAngle;
			var angle;
Z
zz85 已提交
332
			var tdivisions = divisions * 2;
333

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

336
				t = j / tdivisions;
337

Z
zz85 已提交
338
				if ( ! aClockwise ) {
339

Z
zz85 已提交
340
					t = 1 - t;
341

Z
zz85 已提交
342
				}
343

Z
zz85 已提交
344
				angle = aStartAngle + t * deltaAngle;
345

346 347
				tx = lastx + aX + aRadius * Math.cos( angle );
				ty = lasty + aY + aRadius * Math.sin( angle );
348

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

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

Z
zz85 已提交
353
			}
354

Z
zz85 已提交
355
			//console.log(points);
Z
zz85 已提交
356 357 358 359

		  break;

		} // end switch
Z
zz85 已提交
360

361 362
	}

A
alteredq 已提交
363 364 365 366 367 368
	if ( closedPath ) {

		points.push( points[ 0 ] );

	}

369
	return points;
Z
zz85 已提交
370

371
};
Z
zz85 已提交
372

Z
zz85 已提交
373

Z
zz85 已提交
374

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

377
THREE.Path.prototype.transform = function( path, segments ) {
378

379 380
	var bounds = this.getBoundingBox();
	var oldPts = this.getPoints( segments ); // getPoints getSpacedPoints
381

382 383 384
	//console.log( path.cacheArcLengths() );
	//path.getLengths(400);
	//segments = 40;
385

386
	return this.getWrapPoints( oldPts, path );
387

388 389 390
};

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

393 394 395 396 397 398 399 400 401
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

402
	var oldPts = this.getPoints();
403

404
	var i, il, p, oldX, oldY;
405 406 407

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

408
		p = oldPts[i];
409

410 411
		oldX = p.x;
		oldY = p.y;
412

413 414
		p.x = a * oldX + b * oldY + c;
		p.y = d * oldY + e * oldX + f;
415

416
	}
417

418
	return oldPts;
419

420 421
};

Z
zz85 已提交
422 423 424

// FUTURE Export JSON Format

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

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

429
	var bounds = this.getBoundingBox();
430 431

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

433
		canvas = document.createElement( "canvas" );
Z
zz85 已提交
434

435 436
		canvas.setAttribute( 'width',  bounds.maxX + 100 );
		canvas.setAttribute( 'height', bounds.maxY + 100 );
Z
zz85 已提交
437

438 439 440
		document.body.appendChild( canvas );

	}
Z
zz85 已提交
441

442 443
	var ctx = canvas.getContext( "2d" );
	ctx.fillStyle = "white";
444
	ctx.fillRect( 0, 0, canvas.width, canvas.height );
Z
zz85 已提交
445

446 447
	ctx.strokeStyle = "black";
	ctx.beginPath();
Z
zz85 已提交
448

449
	var i, il, item, action, args;
Z
zz85 已提交
450

451
	// Debug Path
Z
zz85 已提交
452

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

455
		item = this.actions[ i ];
Z
zz85 已提交
456

457 458
		args = item.args;
		action = item.action;
Z
zz85 已提交
459

460
		// Short hand for now
Z
zz85 已提交
461

462 463 464 465 466
		if ( action != THREE.PathActions.CSPLINE_THRU ) {

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

		}
Z
zz85 已提交
467

468 469
		/*
		switch ( action ) {
Z
zz85 已提交
470

471
			case THREE.PathActions.MOVE_TO:
Z
zz85 已提交
472

473 474
				ctx[ action ]( args[ 0 ], args[ 1 ] );
				break;
Z
zz85 已提交
475

476
			case THREE.PathActions.LINE_TO:
Z
zz85 已提交
477

478 479
				ctx[ action ]( args[ 0 ], args[ 1 ] );
				break;
Z
zz85 已提交
480

481
			case THREE.PathActions.QUADRATIC_CURVE_TO:
Z
zz85 已提交
482

483 484
				ctx[ action ]( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ] );
				break;
Z
zz85 已提交
485

486
			case THREE.PathActions.CUBIC_CURVE_TO:
Z
zz85 已提交
487

488 489
				ctx[ action ]( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], args[ 4 ], args[ 5 ] );
				break;
Z
zz85 已提交
490

491 492
		}
		*/
Z
zz85 已提交
493 494 495

	}

496 497 498
	ctx.stroke();
	ctx.closePath();

Z
zz85 已提交
499
	// Debug Points
Z
zz85 已提交
500

501
	ctx.strokeStyle = "red";
Z
zz85 已提交
502

Z
zz85 已提交
503
	/* TO CLEAN UP */
504

505
	var p, points = this.getPoints();
506

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

509
		p = points[ i ];
Z
zz85 已提交
510

511 512 513 514
		ctx.beginPath();
		ctx.arc( p.x, p.y, 1.5, 0, Math.PI * 2, false );
		ctx.stroke();
		ctx.closePath();
Z
zz85 已提交
515

516
	}
Z
zz85 已提交
517

518
};
519 520

// Breaks path into shapes
521

522
THREE.Path.prototype.toShapes = function() {
523

524 525 526 527 528 529 530 531 532 533
	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;
534 535 536 537 538 539

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

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

				subPaths.push( lastPath );
540
				lastPath = new THREE.Path();
541

542
			}
543

544
		}
545 546 547

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

548
	}
549 550 551 552 553

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

		subPaths.push( lastPath );

554
	}
555

Z
zz85 已提交
556
	// console.log(subPaths);
557

558
	if ( subPaths.length == 0 ) return [];
559

Z
zz85 已提交
560 561
	var tmpPath, tmpShape, shapes = [];

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

Z
zz85 已提交
565 566 567 568 569
	if ( subPaths.length == 1) {
		tmpPath = subPaths[0];
		tmpShape = new THREE.Shape();
		tmpShape.actions = tmpPath.actions;
		tmpShape.curves = tmpPath.curves;
Z
zz85 已提交
570 571
		shapes.push( tmpShape );
		return shapes;
Z
zz85 已提交
572
	};
573 574 575

	if ( holesFirst ) {

576
		tmpShape = new THREE.Shape();
577 578 579 580 581 582 583

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

			tmpPath = subPaths[ i ];

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

584 585
				tmpShape.actions = tmpPath.actions;
				tmpShape.curves = tmpPath.curves;
586 587

				shapes.push( tmpShape );
588
				tmpShape = new THREE.Shape();
589

590
				//console.log('cw', i);
591

592
			} else {
593 594 595

				tmpShape.holes.push( tmpPath );

596
				//console.log('ccw', i);
597

598
			}
599

600
		}
601

602
	} else {
603

604
		// Shapes first
605 606 607 608 609 610 611 612 613 614

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

			tmpPath = subPaths[ i ];

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


				if ( tmpShape ) shapes.push( tmpShape );

615 616 617
				tmpShape = new THREE.Shape();
				tmpShape.actions = tmpPath.actions;
				tmpShape.curves = tmpPath.curves;
618

619
			} else {
620 621 622

				tmpShape.holes.push( tmpPath );

623
			}
624

625
		}
626 627 628

		shapes.push( tmpShape );

629
	}
630

631
	//console.log("shape", shapes);
632

633
	return shapes;
634

Z
zz85 已提交
635
};