MCPcopy Index your code
hub / github.com/processing/p5.js / addToShape

Method addToShape

src/shape/custom_shapes.js:82–131  ·  view source on GitHub ↗
(shape)

Source from the content-addressed store, hash-verified

80 }
81
82 addToShape(shape) {
83 /*
84 TODO:
85 Refactor?
86 Test this method once more primitives are implemented.
87 Test segments separately (Segment adds an extra step to this method).
88 */
89 let lastContour = shape.at(-1);
90
91 if (lastContour.primitives.length === 0) {
92 lastContour.primitives.push(this);
93 } else {
94 // last primitive in shape
95 let lastPrimitive = shape.at(-1, -1);
96 let hasSameType = lastPrimitive instanceof this.constructor;
97 let spareCapacity = lastPrimitive.vertexCapacity -
98 lastPrimitive.vertexCount;
99
100 // this primitive
101 let pushableVertices;
102 let remainingVertices;
103
104 if (hasSameType && spareCapacity > 0) {
105
106 pushableVertices = this.vertices.splice(0, spareCapacity);
107 remainingVertices = this.vertices;
108 lastPrimitive.vertices.push(...pushableVertices);
109
110 if (remainingVertices.length > 0) {
111 lastContour.primitives.push(this);
112 }
113 }
114 else {
115 lastContour.primitives.push(this);
116 }
117 }
118
119 // if primitive itself was added
120 // (i.e. its individual vertices weren't all added to an existing primitive)
121 // give it a reference to the shape and store its location within the shape
122 let addedToShape = this.vertices.length > 0;
123 if (addedToShape) {
124 let lastContour = shape.at(-1);
125 this._primitivesIndex = lastContour.primitives.length - 1;
126 this._contoursIndex = shape.contours.length - 1;
127 this._shape = shape;
128 }
129
130 return shape.at(-1, -1);
131 }
132
133 get _nextPrimitive() {
134 return this._belongsToShape ?

Callers

nothing calls this directly

Calls 2

atMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected