Repository navigation
Fix shared effect ownership and gradient disposal - #894
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved disposal bugs can disrupt gradients shared across instances and reattach released texture listeners, while the unguarded WebGL test fails in browsers without WebGL support.
3 open issues
What changed in this PR
This PR addresses #847 by tracking shared effect ownership across Two.js shapes and making gradient disposal safe to repeat while preserving reuse.
Changes:
- Track effect assignments independently of scene membership and dispose effects only after their last assignment is released.
- Clear gradient renderer caches and external listeners while preserving internal change tracking.
- Add disposal regression coverage and update API documentation and TypeScript comments.
| File | Description |
|---|---|
wiki/docs/two/README.md |
Document shared-effect release behavior. |
wiki/docs/text/README.md |
Document text effect ownership. |
wiki/docs/shapes/points/README.md |
Document points effect ownership. |
wiki/docs/path/README.md |
Document path effect ownership. |
wiki/docs/effects/sprite/README.md |
Clarify sprite texture release. |
wiki/docs/effects/image/README.md |
Clarify image texture release. |
wiki/docs/effects/image-sequence/README.md |
Document shared texture disposal. |
wiki/docs/effects/gradient/README.md |
Document repeatable disposal and reuse. |
tests/suite/dispose.js |
Cover shared effects, release, and gradient reuse. |
src/utils/effects.js |
Centralize assignment tracking and effect cleanup. |
src/two.js |
Scope effect release to individual objects. |
src/two.d.ts |
Update release ownership comments. |
src/text.js |
Track and release text effect assignments. |
src/text.d.ts |
Update text disposal comments. |
src/shapes/points.js |
Track and release points effect assignments. |
src/shapes/points.d.ts |
Update points disposal comments. |
src/path.js |
Track and release path effect assignments. |
src/path.d.ts |
Update path disposal comments. |
src/effects/sprite.js |
Track sprite texture ownership. |
src/effects/sprite.d.ts |
Update sprite disposal comments. |
src/effects/image.js |
Track image texture ownership. |
src/effects/image.d.ts |
Update image disposal comments. |
src/effects/image-sequence.js |
Track texture ownership through collection edits. |
src/effects/image-sequence.d.ts |
Update sequence disposal comments. |
src/effects/gradient.js |
Perform parent cleanup and reset rendering state for reuse. |
src/effects/gradient.d.ts |
Update gradient disposal contract comments. |
Review effort: Balanced
jonofyi
approved these changes
Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Disposing a shape can currently destroy a gradient or texture still used by another shape, and identical fill/stroke assignments dispose the same effect twice. Gradient disposal also leaves renderer references and external listeners behind.
Track effect assignments independently of scene membership. Shape disposal detaches that shape's listeners and disposes each effect once, only when its last assignment is released. Apply the same tracking to image/sprite textures and image-sequence collections.
Two.release()preserves other shapes' effect listeners without disposing effect resources. Recursive release defers factory-created scene effects until shapes have released their assignments, and skips effects that still have users. SVG gradients and textures can move out of disposed renderers into surviving definitions. Image-sequence collection callbacks are detached before their ownership records are released, so later collection edits cannot restore released listeners.Gradient disposal now calls parent cleanup, clears cached renderer and stop elements, and preserves internal change tracking so subsequent rendering recreates the gradient correctly. Update API documentation and TypeScript comments with the ownership contract.
Ownership details:
effect.dispose()is explicit cleanup and should be called only when all users are finished.Validation:
Closes #847.