| 6 | window.addEventListener('mousemove', ev => mouse = getMousePos(ev)); |
| 7 | |
| 8 | export class Cursor { |
| 9 | constructor(el) { |
| 10 | this.DOM = {el: el}; |
| 11 | this.DOM.svg = this.DOM.el.querySelector('.cursor__svg'); |
| 12 | this.DOM.circle = this.DOM.svg.querySelector('.cursor__svg-circle'); |
| 13 | this.DOM.circle.style.transformOrigin = '50% 50%'; |
| 14 | this.DOM.text = this.DOM.el.querySelector('.cursor__text'); |
| 15 | |
| 16 | this.DOM.el.style.opacity = 0; |
| 17 | |
| 18 | this.bounds = this.DOM.svg.getBoundingClientRect(); |
| 19 | |
| 20 | this.renderedStyles = { |
| 21 | tx: {previous: 0, current: 0, amt: 0.2}, |
| 22 | ty: {previous: 0, current: 0, amt: 0.2}, |
| 23 | txText: {previous: 0, current: 0, amt: 0.1}, |
| 24 | tyText: {previous: 0, current: 0, amt: 0.1}, |
| 25 | scale: {previous: 1, current: 1, amt: 0.15} |
| 26 | }; |
| 27 | |
| 28 | this.onMouseMoveEv = () => { |
| 29 | this.renderedStyles.tx.previous = this.renderedStyles.tx.current |
| 30 | = this.renderedStyles.txText.previous |
| 31 | = this.renderedStyles.txText.current |
| 32 | = mouse.x - this.bounds.width/2; |
| 33 | this.renderedStyles.ty.previous = this.renderedStyles.ty.current |
| 34 | = this.renderedStyles.tyText.previous |
| 35 | = this.renderedStyles.tyText.current |
| 36 | = mouse.y - this.bounds.height/2; |
| 37 | |
| 38 | gsap.to(this.DOM.el, {duration: 0.9, ease: 'Power3.easeOut', opacity: 1}); |
| 39 | requestAnimationFrame(() => this.render()); |
| 40 | window.removeEventListener('mousemove', this.onMouseMoveEv); |
| 41 | }; |
| 42 | window.addEventListener('mousemove', this.onMouseMoveEv); |
| 43 | } |
| 44 | enter() { |
| 45 | this.renderedStyles['scale'].current = 1.5; |
| 46 | } |
| 47 | leave() { |
| 48 | this.renderedStyles['scale'].current = 1; |
| 49 | } |
| 50 | render() { |
| 51 | this.renderedStyles['tx'].current = this.renderedStyles['txText'].current = mouse.x - this.bounds.width/2; |
| 52 | this.renderedStyles['ty'].current = this.renderedStyles['tyText'].current = mouse.y - this.bounds.height/2; |
| 53 | |
| 54 | for (const key in this.renderedStyles ) { |
| 55 | this.renderedStyles[key].previous = lerp(this.renderedStyles[key].previous, this.renderedStyles[key].current, this.renderedStyles[key].amt); |
| 56 | } |
| 57 | |
| 58 | //this.DOM.el.style.transform = `translateX(${(this.renderedStyles['tx'].previous)}px) translateY(${this.renderedStyles['ty'].previous}px)`; |
| 59 | this.DOM.svg.style.transform = `translateX(${(this.renderedStyles['tx'].previous)}px) translateY(${this.renderedStyles['ty'].previous}px)`; |
| 60 | this.DOM.text.style.transform = `translateX(${(this.renderedStyles['txText'].previous)}px) translateY(${this.renderedStyles['tyText'].previous}px)`; |
| 61 | this.DOM.circle.style.transform = `scale(${this.renderedStyles['scale'].previous})`; |
| 62 | |
| 63 | requestAnimationFrame(() => this.render()); |
| 64 | } |
| 65 | } |
nothing calls this directly
no outgoing calls
no test coverage detected