-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26d512c
commit eb76b6b
Showing
12 changed files
with
539 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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,71 @@ | ||
import { useEffect } from "react"; | ||
import useScript from "./useScript"; | ||
/* | ||
* @param {string} gsource - the source of the swf | ||
* @param {string} Rufflecontainer - the container that the player will be stored in | ||
* @param {number} width - the width of the ruffle player | ||
* @param {number} height - the height of the ruffle player | ||
*/ | ||
|
||
// Declaring width and height for the player isnt reccomeded | ||
// because it will automatically take 100% of your ruffleContainer | ||
// But feel free to set the width if you would like to | ||
declare global { | ||
interface Window { | ||
RufflePlayer: any; | ||
} | ||
} | ||
|
||
const useRuffle = ( | ||
Rufflecontainer: any, | ||
gsource: string, | ||
width: string | number, | ||
height: string | number | ||
) => { | ||
if (!width) width = "100%"; | ||
if (!height) height = "100%"; | ||
if (!Rufflecontainer) throw new Error("Rufflecontainer is required"); | ||
useScript("/cdn/games/ruffle/ruffle.js"); | ||
useEffect(() => { | ||
// stops loading the ruffle player if it is already loaded | ||
if (window.RufflePlayer) return; | ||
window.RufflePlayer = window.RufflePlayer || {}; | ||
window.RufflePlayer.config = { | ||
// Options affecting files only | ||
autoplay: "auto", | ||
unmuteOverlay: "visible", | ||
backgroundColor: "#222a3a", | ||
letterbox: "fullscreen", | ||
warnOnUnsupportedContent: true, | ||
contextMenu: true, | ||
showSwfDownload: false, | ||
upgradeToHttps: window.location.protocol === "https:", | ||
maxExecutionDuration: { secs: 15, nanos: 0 }, | ||
logLevel: "error", | ||
base: null, | ||
menu: true, | ||
salign: "", | ||
scale: "showAll", | ||
quality: "high", | ||
}; | ||
window.addEventListener("load", (event) => { | ||
const ruffle = window.RufflePlayer.newest(); | ||
const player = ruffle.createPlayer(); | ||
Rufflecontainer.current.appendChild(player); | ||
player.style.width = width; | ||
player.style.height = height; | ||
player | ||
.load(gsource, { | ||
allowScriptAccess: false, // if false swf cant interact with page (recommended false) | ||
}) | ||
.then(() => { | ||
console.log("swf loaded"); | ||
}) | ||
.catch((error: unknown) => { | ||
console.log("swf load error:", error); | ||
}); | ||
}); | ||
}, []); | ||
}; | ||
|
||
export default useRuffle; |
This file contains 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,18 @@ | ||
import { useEffect } from "react"; | ||
|
||
const useScript = (url: string) => { | ||
useEffect(() => { | ||
const script = document.createElement("script"); | ||
|
||
script.src = url; | ||
script.async = true; | ||
|
||
document.body.appendChild(script); | ||
console.log(script); | ||
return () => { | ||
document.body.removeChild(script); | ||
}; | ||
}, [url]); | ||
}; | ||
|
||
export default useScript; |
Oops, something went wrong.