-
-
Notifications
You must be signed in to change notification settings - Fork 4
getHypeDocument
I often read about people that need the hypeDocument
context while adding code into an DIV. In Hype this context actually doesn't know about the hypeDocument so this little helper solves that problem. The traditional approach found in the Tumult Hype docs requires hardcoding the document id. This approach makes retrieving the hypeDocument independent.
This snippet allows you to get the context anytime. Just add it to your Head-HTML. If you can't put this in your Head-HTML or need it contained in Hype put it in an init function (without the script tags) that runs on scene load. And third option would be an enabler … meaning stick into a persistent symbol.
<script>
/**
* getHypeDocument 1.1
* @param {HTMLDivElement} optional start node
* @return {hypeDocument} or null
*/
function getHypeDocument(e){
var ds = document.scripts, hd=HYPE.documents
var e = e || ds[ds.length-1];
if (e = e.closest('[id$="_hype_container"]'))
for (var key in hd)
if (hd[key].documentId() == e.id) return hd[key];
return null;
}
</script>
And then you can execute any of the following in your DIV that live inside of your Hype-file (just examples)…
Example in a script block
<script>
// get the hypeDocument
var hypeDocument = getHypeDocument();
// let's check it and now we can use it
console.log(hypeDocument)
</script>
Examples on a link (On links it currently needs this as a parameter)
Echo hypeDocument to console:
<a onclick="console.log(getHypeDocument(this))">log hypeDocument</a>
A more useful case: Start a timeline
<a onclick="getHypeDocument(this).startTimelineNamed('test')">start test</a>
1.0 Initial release under MIT-license
1.1 Removed recursion and simplified code
- Choose another topic from the sidebar
- Visit the topic index
- Return to the welcome page
- Learn about contributing
- Consider making an one-time donation
- Visit the landing page for this project
- Accessibility in Hype
- Quick iterations as the secret to progress
- Using querySelector
- Test an elements class name using classList and contains
- Including external files and Hype Extensions
- Fetching hypeDocument from within a rectangle
- Extend the functionality of Hype using Export Scripts
- Using external editors
- Embedding Hype documents into third-party services
- Accessing external APIs from Hype Previews
- Manually include resources in document head
- Manipulating scene height
- Extension template