Last active
December 12, 2019 17:42
-
-
Save jesperlandberg/89de2c0071bee3cfc443bd59cffc2d59 to your computer and use it in GitHub Desktop.
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
| import e from '@unseenco/e' // Event emitter, I use my friend Jake's, since its great, but could also use TinyEmitter for example | |
| const Events = new e() | |
| export default Events |
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
| // Make sure u import ur global raf at least once somewhere, I usually do that in my main.js file | |
| import Raf from './Raf' | |
| class App { | |
| // My main class | |
| } | |
| new App() |
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
| import gsap from 'gsap' | |
| import Events from './Events' | |
| class Raf { | |
| constructor() { | |
| gsap.ticker.add(this.tick) | |
| } | |
| tick = () => { | |
| Events.emit('tick') | |
| /* | |
| in my personal core I calc and emit scroll and mouse values, | |
| so I can access these in any file that might need them. | |
| Events.emit('tick', { | |
| target: this.target, | |
| current: this.currentRounded, | |
| mouse: this.mouse | |
| }) | |
| */ | |
| } | |
| } | |
| export default new Raf() |
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
| // Example component where I need the raf | |
| import Events from './Events' | |
| export default class { | |
| constructor() { | |
| this.init() | |
| } | |
| init() { | |
| this.addEvents() | |
| } | |
| run = ({ current, mouse }) /* my scroll and mouse get passed like params */ => { | |
| // Do some component specific raf stuff | |
| } | |
| addEvents() { | |
| // Listen to raf | |
| Events.on('tick', this.run) | |
| } | |
| removeEvents() { | |
| // Stop listening to raf | |
| Events.off('tick', this.run) | |
| } | |
| destroy() { | |
| // Remove events and cleanup class | |
| this.removeEvents() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment