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;
Z
zz85 已提交
333
			var t;
334

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

337
				t = j / tdivisions;
338

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

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

Z
zz85 已提交
343
				}
344

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

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

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

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

Z
zz85 已提交
354
			}
355

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

		  break;

		} // end switch
Z
zz85 已提交
361

362 363
	}

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

		points.push( points[ 0 ] );

	}

370
	return points;
Z
zz85 已提交
371

372
};
Z
zz85 已提交
373

Z
zz85 已提交
374

Z
zz85 已提交
375

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

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

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

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

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

389 390 391
};

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

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

403
	var oldPts = this.getPoints();
404

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

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

409
		p = oldPts[i];
410

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

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

417
	}
418

419
	return oldPts;
420

421 422
};

Z
zz85 已提交
423 424 425

// FUTURE Export JSON Format

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

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

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

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

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

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

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

	}
Z
zz85 已提交
442

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

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

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

452
	// Debug Path
Z
zz85 已提交
453

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

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

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

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

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

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

		}
Z
zz85 已提交
468

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

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

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

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

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

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

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

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

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

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

	}

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

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

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

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

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

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

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

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

517
	}
Z
zz85 已提交
518

519
};
520 521

// Breaks path into shapes
522

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

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

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

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

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

543
			}
544

545
		}
546 547 548

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

549
	}
550 551 552 553 554

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

		subPaths.push( lastPath );

555
	}
556

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

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

	var holesFirst = !THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() );
Z
zz85 已提交
562 563 564 565 566 567 568
	if ( subPaths.length == 1) {
		tmpPath = subPaths[0];
		tmpShape = new THREE.Shape();
		tmpShape.actions = tmpPath.actions;
		tmpShape.curves = tmpPath.curves;
		return tmpShape;
	};
569 570 571

	var tmpPath, tmpShape, shapes = [];

Z
zz85 已提交
572 573 574
	// console.log("Holes first", holesFirst);
	
	
575 576 577

	if ( holesFirst ) {

578
		tmpShape = new THREE.Shape();
579 580 581 582 583 584 585

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

			tmpPath = subPaths[ i ];

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

586 587
				tmpShape.actions = tmpPath.actions;
				tmpShape.curves = tmpPath.curves;
588 589

				shapes.push( tmpShape );
590
				tmpShape = new THREE.Shape();
591

592
				//console.log('cw', i);
593

594
			} else {
595 596 597

				tmpShape.holes.push( tmpPath );

598
				//console.log('ccw', i);
599

600
			}
601

602
		}
603

604
	} else {
605

606
		// Shapes first
607 608 609 610 611 612 613 614 615 616

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

			tmpPath = subPaths[ i ];

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


				if ( tmpShape ) shapes.push( tmpShape );

617 618 619
				tmpShape = new THREE.Shape();
				tmpShape.actions = tmpPath.actions;
				tmpShape.curves = tmpPath.curves;
620

621
			} else {
622 623 624

				tmpShape.holes.push( tmpPath );

625
			}
626

627
		}
628 629 630

		shapes.push( tmpShape );

631
	}
632

633
	//console.log("shape", shapes);
634

635
	return shapes;
636

Z
zz85 已提交
637
};