diff --git a/src/animation/Flash.ts b/src/animation/Flash.ts new file mode 100644 index 0000000..c821a95 --- /dev/null +++ b/src/animation/Flash.ts @@ -0,0 +1,42 @@ +import { Animation } from "./Animation.js"; +import * as THREE from "three"; + +export default class Flash extends Animation { + constructor( + object: THREE.Object3D, + fadeOut: boolean = false, + startTime: number, + endTime: number, + ) { + super( + (_elapsedTime) => { + let alpha; + if (!fadeOut) { + if (_elapsedTime < startTime) { + alpha = 0; + } else if (_elapsedTime === startTime) { + alpha = 1; + } else { + alpha = 1; + } + } else { + const midpoint = (startTime + endTime) / 2; + if (_elapsedTime < startTime) { + alpha = 0; + } else if (_elapsedTime < midpoint) { + const t0 = (_elapsedTime - startTime) / (midpoint - startTime); + alpha = t0; + } else if (_elapsedTime < endTime) { + const t1 = (_elapsedTime - midpoint) / (endTime - midpoint); + alpha = 1 - t1; + } else { + alpha = 0; + } + } + + object.setOpacity(alpha); + }, + { object, reveal: true }, + ); + } +} diff --git a/src/animation/index.ts b/src/animation/index.ts index bec8f08..a859b53 100644 --- a/src/animation/index.ts +++ b/src/animation/index.ts @@ -10,4 +10,5 @@ export { default as SetOpacity } from "./SetOpacity.js"; export { default as FadeOut } from "./FadeOut.js"; export { default as Wait } from "./Wait.js"; export { default as Emphasize } from "./Emphasize.js"; -export { default as Shake } from "./Shake.js"; \ No newline at end of file +export { default as Shake } from "./Shake.js"; +export { default as Flash } from "./Flash.js"; \ No newline at end of file diff --git a/src/geometry/Polygon.ts b/src/geometry/Polygon.ts index 1faaf2a..cc88d8a 100644 --- a/src/geometry/Polygon.ts +++ b/src/geometry/Polygon.ts @@ -14,6 +14,11 @@ export type PolygonAttributes = { export default class Polygon extends Shape { constructor(points: Array, config: Style = {}) { + // Ensure the polygon is closed by adding the first point to the end + if (points.length > 0 && !points[0].equals(points[points.length - 1])) { + points.push(points[0]); + } + super(points, { ...Polygon.defaultConfig(), ...config }); this.curveEndIndices = []; for (let i = 0; i < points.length - 1; i++) {