-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathcone.js
133 lines (113 loc) · 3.88 KB
/
cone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Cone composite shape
*/
( function( root, factory ) {
// module definition
if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory( require('./boilerplate'), require('./vector'),
require('./path-command'), require('./anchor'), require('./ellipse') );
} else {
// browser global
var Zdog = root.Zdog;
Zdog.Cone = factory( Zdog, Zdog.Vector, Zdog.PathCommand,
Zdog.Anchor, Zdog.Ellipse );
}
}( this, function factory( utils, Vector, PathCommand, Anchor, Ellipse ) {
var Cone = Ellipse.subclass({
length: 1,
fill: true,
});
var TAU = utils.TAU;
Cone.prototype.create = function( /* options */) {
// call super
Ellipse.prototype.create.apply( this, arguments );
// composite shape, create child shapes
this.apex = new Anchor({
addTo: this,
translate: { z: this.length },
});
// vectors used for calculation
this.renderApex = new Vector();
this.renderCentroid = new Vector();
this.tangentA = new Vector();
this.tangentB = new Vector();
this.surfacePathCommands = [
new PathCommand( 'move', [ {} ] ), // points set in renderConeSurface
new PathCommand( 'line', [ {} ] ),
new PathCommand( 'line', [ {} ] ),
];
};
Cone.prototype.updateSortValue = function() {
// center of cone is one third of its length
this.renderCentroid.set( this.renderOrigin )
.lerp( this.apex.renderOrigin, 1/3 );
this.sortValue = this.renderCentroid.z;
};
Cone.prototype.render = function( ctx, renderer ) {
this.renderConeSurface( ctx, renderer );
Ellipse.prototype.render.apply( this, arguments );
};
Cone.prototype.renderConeSurface = function( ctx, renderer ) {
if ( !this.visible ) {
return;
}
this.renderApex.set( this.apex.renderOrigin )
.subtract( this.renderOrigin );
var scale = this.renderNormal.magnitude();
var apexDistance = this.renderApex.magnitude2d();
var normalDistance = this.renderNormal.magnitude2d();
// eccentricity
var eccenAngle = Math.acos( normalDistance/scale );
var eccen = Math.sin( eccenAngle );
var radius = this.diameter / 2 * scale;
// does apex extend beyond eclipse of face
var isApexVisible = radius * eccen < apexDistance;
if ( !isApexVisible ) {
return;
}
// update tangents
var apexAngle = Math.atan2( this.renderNormal.y, this.renderNormal.x ) +
TAU/2;
var projectLength = apexDistance/eccen;
var projectAngle = Math.acos( radius/projectLength );
// set tangent points
var tangentA = this.tangentA;
var tangentB = this.tangentB;
tangentA.x = Math.cos( projectAngle ) * radius * eccen;
tangentA.y = Math.sin( projectAngle ) * radius;
tangentB.set( this.tangentA );
tangentB.y *= -1;
tangentA.rotateZ( apexAngle );
tangentB.rotateZ( apexAngle );
tangentA.add( this.renderOrigin );
tangentB.add( this.renderOrigin );
this.setSurfaceRenderPoint( 0, tangentA );
this.setSurfaceRenderPoint( 1, this.apex.renderOrigin );
this.setSurfaceRenderPoint( 2, tangentB );
// render
var elem = this.getSurfaceRenderElement( ctx, renderer );
renderer.renderPath( ctx, elem, this.surfacePathCommands );
renderer.stroke( ctx, elem, this.stroke, this.color, this.getLineWidth() );
renderer.fill( ctx, elem, this.fill, this.color );
renderer.end( ctx, elem );
};
var svgURI = 'http://www.w3.org/2000/svg';
Cone.prototype.getSurfaceRenderElement = function( ctx, renderer ) {
if ( !renderer.isSvg ) {
return;
}
if ( !this.surfaceSvgElement ) {
// create svgElement
this.surfaceSvgElement = document.createElementNS( svgURI, 'path' );
this.surfaceSvgElement.setAttribute( 'stroke-linecap', 'round' );
this.surfaceSvgElement.setAttribute( 'stroke-linejoin', 'round' );
}
return this.surfaceSvgElement;
};
Cone.prototype.setSurfaceRenderPoint = function( index, point ) {
var renderPoint = this.surfacePathCommands[ index ].renderPoints[0];
renderPoint.set( point );
};
return Cone;
} ) );