Skip to content

Commit

Permalink
Object config for sample and segment
Browse files Browse the repository at this point in the history
  • Loading branch information
cereallarceny committed Jul 18, 2019
1 parent 486a4bd commit 014ab36
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Sample.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class Sample {
constructor(x, y, progress, segment) {
constructor({ x, y, progress, segment }) {
this.x = x;
this.y = y;
this.progress = progress;
Expand Down
2 changes: 1 addition & 1 deletion src/Segment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getMiddleSample } from './_utils';

export default class Segment {
constructor(samples) {
constructor({ samples }) {
this.samples = samples;
this.progress = getMiddleSample(samples).progress;
}
Expand Down
38 changes: 19 additions & 19 deletions src/_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const getData = ({
y = +y.toFixed(precision);
}

allSamples.push(new Sample(x, y, progress));
allSamples.push(new Sample({ x, y, progress }));
}

// Out of all the samples gathered, sort them into groups of length = samples
Expand All @@ -45,7 +45,7 @@ export const getData = ({

segmentSamples.push(allSamples[nextStart]);

allSegments.push(new Segment(segmentSamples));
allSegments.push(new Segment({ samples: segmentSamples }));
}

return allSegments;
Expand All @@ -61,16 +61,16 @@ export const strokeToFill = (data, width, precision) => {
const outlineStrokes = (data, width, precision) => {
// We need to get the points perpendicular to a startPoint, given angle, radius, and precision
const getPerpSamples = (angle, radius, precision, startPoint) => {
const p0 = new Sample(
Math.sin(angle) * radius + startPoint.x,
-Math.cos(angle) * radius + startPoint.y,
startPoint.progress
),
p1 = new Sample(
-Math.sin(angle) * radius + startPoint.x,
Math.cos(angle) * radius + startPoint.y,
startPoint.progress
);
const p0 = new Sample({
x: Math.sin(angle) * radius + startPoint.x,
y: -Math.cos(angle) * radius + startPoint.y,
progress: startPoint.progress
}),
p1 = new Sample({
x: -Math.sin(angle) * radius + startPoint.x,
y: Math.cos(angle) * radius + startPoint.y,
progress: startPoint.progress
});

if (precision) {
p0.x = +p0.x.toFixed(precision);
Expand Down Expand Up @@ -112,10 +112,12 @@ const outlineStrokes = (data, width, precision) => {
// segmentSamples is out of order...
// Given a segmentSamples length of 8, the points need to be rearranged from: 0, 2, 4, 6, 7, 5, 3, 1
outlinedData.push(
new Segment([
...segmentSamples.filter((s, i) => i % 2 === 0),
...segmentSamples.filter((s, i) => i % 2 === 1).reverse()
])
new Segment({
samples: [
...segmentSamples.filter((s, i) => i % 2 === 0),
...segmentSamples.filter((s, i) => i % 2 === 1).reverse()
]
})
);
}

Expand Down Expand Up @@ -170,8 +172,6 @@ const averageSegmentJoins = (outlinedData, precision) => {
export const flattenSegments = data =>
data
.map((segment, i) =>
segment.samples.map(
sample => new Sample(sample.x, sample.y, sample.progress, i)
)
segment.samples.map(sample => new Sample({ ...sample, segment: i }))
)
.flat();

0 comments on commit 014ab36

Please sign in to comment.