Skip to content

Commit e12ffd3

Browse files
author
Bryan Parker
committed
adds changeText to text and adds throttling function
1 parent c84cc53 commit e12ffd3

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

src/shapes/primitives/text.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ class Text implements Shape {
287287
return this._tex;
288288
}
289289

290+
changeText(text: string): this {
291+
this._text = text;
292+
293+
this._textMeasurement = new TextMeasurement(config.canvasInstance!, this._tex);
294+
this._textWidth = this._textMeasurement.textWidth(this._text, this._size, this._font);
295+
this._textHeight = this._textMeasurement.textHeight(this._text, this._size, this._font);
296+
297+
return this;
298+
}
299+
290300
private boundingBox(): { minX: number, maxX: number, minY: number, maxY: number } {
291301
const left = this._align === 'left' ? this._x : this._align === 'center' ? this._x - this._textWidth / 2 : this._x - this._textWidth;
292302
const top = this._baseline === 'top' ? this._y : this._baseline === 'middle' ? this._y - this._textHeight / 2 : this._y - this._textHeight;

src/utils.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,27 @@ function extractType<T extends Object>(obj: any): T {
4949
}
5050

5151

52-
export default { deepCopy, extractType };
52+
function throttle<T extends (...args: any[]) => void>(func: T, wait: number): (...args: Parameters<T>) => void {
53+
let timeout: ReturnType<typeof setTimeout> | null = null;
54+
let lastCallTime: number | null = null;
55+
56+
return function(...args: Parameters<T>): void {
57+
const now = Date.now();
58+
59+
if (lastCallTime && now < lastCallTime + wait) {
60+
if (timeout) {
61+
clearTimeout(timeout);
62+
}
63+
timeout = setTimeout(() => {
64+
lastCallTime = Date.now();
65+
func(...args);
66+
}, wait - (now - lastCallTime));
67+
} else {
68+
lastCallTime = Date.now();
69+
func(...args);
70+
}
71+
};
72+
}
73+
74+
75+
export default { deepCopy, extractType, throttle, };

test/script_test.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,27 +451,45 @@ class TestScene extends Scene {
451451
const p = axes.point([0.5, fn(0.5)]);
452452
const q = axes.point([0.69, fn(0.69)]);
453453

454-
const pText = new Text({ text: 'P' }).nextTo(p, [-1, 1]);
455-
const qText = new Text({ text: 'Q' }).nextTo(q, LEFT(1.5));
454+
const pText = new Tex({ text: 'P' }).nextTo(p, [-1, 1]);
455+
const qText = new Tex({ text: 'Q' }).nextTo(q, RIGHT(1.5));
456456
const qDot = new Dot({ center: q, color: Colors.blue() });
457457
const secant = new Line({ from: p, to: q, length: 6, lineColor: Colors.blue() });
458+
const tangent = new TangentLine({ plot, x: 0.5, length: 4, color: Colors.pink() });
459+
const tangentText = new Tex(`m`).changeColor(Colors.pink()).nextTo(tangent.to());
460+
const secantText = new Tex(`m_{\sec}`).nextTo(secant.to()).changeColor(Colors.blue());
461+
// const secantText = new Text(`m_{\\sec}`).nextTo(secant.to()).changeColor(Colors.blue());
458462

459463
this.add(
460464
axes, plot,
461465

462466
secant,
463-
new TangentLine({ plot, x: 0.5, length: 4, color: Colors.pink() }),
467+
tangent,
464468
new Dot({ center: p, color: Colors.pink() }),
465469

466470
qDot,
467-
pText, qText
471+
pText, qText,
472+
tangentText,
473+
secantText,
468474
);
469475

476+
const updateText = utils.throttle(text => secantText.changeText(text), 200);
477+
470478
this.add(new Updater((pctComplete: number, starting: boolean) => {
471-
const x = math.lerp(0.5, 0.69, 1 - pctComplete);
479+
const rx = math.lerp(0.5, 0.75, 1 - pctComplete);
480+
const m = (25 * rx * rx - 25 * 0.5 * 0.5) / (rx - 0.5);
481+
482+
const x = math.lerp(0.51, 0.69, 1 - pctComplete);
472483
qDot.moveTo(plot.pointAtX(x));
473484
secant.changeEndpoints(p, qDot, true);
474-
}, { duration: 3000, easing: Easing.linear, repeat: true, yoyo: true, }));
485+
qText.nextTo(qDot.center(), RIGHT(), 0.3);
486+
secantText.nextTo(secant.to()) //.changeText(`m_{\\sec} = ${m.toFixed(2)}`);
487+
488+
// console.log(m, pctComplete)
489+
updateText(`m_{\\sec} = ${m.toFixed(2)}`);
490+
491+
}, { duration: 5000, easing: Easing.linear, repeat: true, yoyo: true, }));
492+
// }, { duration: 5000, easing: x => Easing.easeStep(x, 10), repeat: true, yoyo: true, }));
475493
}
476494
}
477495

0 commit comments

Comments
 (0)