Skip to content

Commit 71265e3

Browse files
committed
remove trails + only draw when visible
1 parent 3ec7c8f commit 71265e3

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

demo/content.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -961,35 +961,8 @@ addCanvas(1.8, (ctx, width, height, animate) => {
961961
},
962962
);
963963

964-
const pointHistory: Point[][] = [];
965-
let renderCount = 0;
966964
animate(() => {
967-
renderCount++;
968-
const points = animation.renderPoints();
969-
970-
if (renderCount % 2 === 0) {
971-
pointHistory.push(points);
972-
}
973-
if (pointHistory.length > trailLength) {
974-
pointHistory.shift();
975-
}
976-
977-
for (let i = 0; i < pointHistory.length; i++) {
978-
tempStyles(
979-
ctx,
980-
() => {
981-
ctx.fillStyle = colors.secondary;
982-
ctx.globalAlpha = i / pointHistory.length;
983-
},
984-
() => {
985-
forPoints(pointHistory[i], ({curr}) => {
986-
drawPoint(ctx, curr, i / pointHistory.length);
987-
});
988-
},
989-
);
990-
}
991-
992-
drawClosed(ctx, points, true);
965+
drawClosed(ctx, animation.renderPoints(), true);
993966
});
994967

995968
return `TODO`;

demo/internal/layout.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ export const addTitle = (heading: number, text: string) => {
8989
textWrapperElement.appendChild(textElement);
9090
};
9191

92+
const handleIntersection = (entries: any) => {
93+
entries.map((entry: any) => {
94+
entry.target.setAttribute("data-visible", entry.isIntersecting);
95+
});
96+
};
97+
9298
// Adds a row of cells to the bottom of the layout.
9399
export const addCanvas = (aspectRatio: number, ...painters: CellPainter[]) => {
94100
const sectionElement = createSection();
@@ -115,6 +121,10 @@ export const addCanvas = (aspectRatio: number, ...painters: CellPainter[]) => {
115121

116122
const cell = {aspectRatio, canvas, ctx, painter, animationID: -1};
117123
cellRow.push(cell);
124+
125+
new IntersectionObserver(handleIntersection, {
126+
threshold: 0.1,
127+
}).observe(canvas);
118128
}
119129
cells.push(cellRow);
120130

@@ -171,7 +181,8 @@ const redraw = () => {
171181
// Stop animating if cell is redrawn.
172182
if (cell.animationID !== animationID) return;
173183

174-
if (pausedAt === 0) {
184+
const visible = cell.canvas.getAttribute("data-visible") === "true";
185+
if (pausedAt === 0 && visible) {
175186
const frameTime = Date.now() - startTime - pauseOffset;
176187
cell.ctx.clearRect(0, 0, cellWidth, cellHeight);
177188
drawDebug();

0 commit comments

Comments
 (0)