-
Notifications
You must be signed in to change notification settings - Fork 15
Add external renderer layer for generated dancers #675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+278
−19
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
be6f758
add external renderer layer for generated dancers
mikeharv 9ecaeee
linting
mikeharv 7083a2f
linting
mikeharv 6b57ed2
linting
mikeharv 6701d41
small tweaks
mikeharv 0b807a3
move base url out
mikeharv c6617d5
add comments
mikeharv ab6bb32
refactor generated dancer as sprite
mikeharv dda681b
rename external layer to generated dancer
mikeharv 1ac02b5
code review
mikeharv 8c26979
code review
mikeharv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Thin p5 adapter: owns a p5.Graphics mid-layer and gives its 2D context | ||
// to the external renderer. CommonJS to match the rest of dance-party. | ||
|
||
// Map of move IDs to move names. These values are used to fetch source animation JSON. | ||
const movesById = { | ||
0: 'rest', | ||
1: 'clap_high', | ||
2: 'clown', | ||
3: 'dab', | ||
4: 'double_jam', | ||
5: 'drop', | ||
6: 'floss', | ||
7: 'fresh', | ||
8: 'kick', | ||
9: 'roll', | ||
10: 'this_or_that', | ||
11: 'thriller', | ||
12: 'xarmsside', | ||
13: 'xarmsup', | ||
14: 'xjump', | ||
15: 'xclapside', | ||
16: 'xheadhips', | ||
17: 'xhighkick', | ||
18: 'xbend', | ||
19: 'xfever', | ||
20: 'xhop', | ||
21: 'xknee', | ||
22: 'xkneel', | ||
23: 'xole', | ||
24: 'xslide', | ||
}; | ||
|
||
class GeneratedDancer { | ||
constructor(p5, worldW, worldH, renderer) { | ||
if (!p5 || !renderer) { | ||
throw new Error('GeneratedDancer requires p5 and a renderer'); | ||
} | ||
this.p5 = p5; | ||
this.renderer = renderer; | ||
|
||
this.graphics = this.p5.createGraphics(worldW, worldH); | ||
this.graphics.pixelDensity(1); | ||
|
||
// Hand the renderer our mid-layer 2D context. | ||
this.renderer.init(this.graphics.drawingContext); | ||
} | ||
|
||
async setSource(src) { | ||
if (typeof src === 'number') { | ||
src = movesById[src] || movesById[0]; | ||
} | ||
return this.renderer.setSource(src); | ||
} | ||
|
||
getDurationFrames() { | ||
return ( | ||
(this.renderer.getDurationFrames && this.renderer.getDurationFrames()) || | ||
null | ||
); | ||
} | ||
|
||
render(frameIndex) { | ||
if (!this.graphics || !this.renderer) { | ||
return; | ||
} | ||
// The renderer paints directly into graphics.drawingContext | ||
this.renderer.renderFrame(frameIndex); | ||
} | ||
|
||
resize(worldW, worldH) { | ||
// detach old <canvas> to avoid DOM leaks | ||
this.dispose(); | ||
this.graphics = this.p5.createGraphics(worldW, worldH); | ||
this.graphics.pixelDensity(1); | ||
this.renderer.init(this.graphics.drawingContext); | ||
} | ||
|
||
dispose() { | ||
this.renderer.dispose(); | ||
|
||
const el = | ||
this.graphics && | ||
(this.graphics.elt || | ||
this.graphics.canvas || | ||
(this.graphics._renderer && this.graphics._renderer.canvas)); | ||
if (el && el.parentNode) { | ||
el.parentNode.removeChild(el); | ||
} | ||
if (this.p5 && Array.isArray(this.p5._elements)) { | ||
this.p5._elements = this.p5._elements.filter(e => e !== this.graphics); | ||
} | ||
mikeharv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
this.graphics = null; | ||
this.renderer = null; | ||
this.p5 = null; | ||
} | ||
} | ||
|
||
module.exports = GeneratedDancer; |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.