11import withMountWhenInView from './withMountWhenInView.js' ;
2- import { clamp , clamp01 } from '../utils/index.js' ;
2+ import { damp , clamp , clamp01 } from '../utils/index.js' ;
33
44/**
55 * @typedef {import('../Base').default } Base
@@ -46,55 +46,91 @@ export default function withScrolledInView(BaseClass) {
4646 x : 0 ,
4747 y : 0 ,
4848 } ,
49+ dampedProgress : {
50+ x : 0 ,
51+ y : 0 ,
52+ } ,
4953 } ;
5054
5155 /**
52- * Class constructor.
53- * @param {HTMLElement } element
56+ * Factor used for the `dampedProgress` props.
57+ * @type {number }
58+ */
59+ dampFactor = 0.1 ;
60+
61+ /**
62+ * Precision for the `dampedProgress` props.
63+ * @type {number }
64+ */
65+ dampPrecision = 0.001 ;
66+
67+ /**
68+ * Mounted hook.
69+ * @returns {void }
70+ */
71+ mounted ( ) {
72+ this . __setProps ( ) ;
73+ }
74+
75+ /**
76+ * Resized hook.
77+ * @returns {void }
78+ */
79+ resized ( ) {
80+ this . __setProps ( ) ;
81+ }
82+
83+ /**
84+ * Scrolled hook.
85+ * @param {any } props
86+ * @returns {void }
87+ */
88+ scrolled ( props ) {
89+ if ( ( ! this . $services . has ( 'ticked' ) && props . changed . y ) || props . changed . x ) {
90+ this . $services . enable ( 'ticked' ) ;
91+ }
92+ }
93+
94+ /**
95+ * Raf hook.
96+ * @returns {void }
5497 */
55- constructor ( element ) {
56- super ( element ) ;
57-
58- this . $on ( 'mounted' , ( ) => this . __setProps ( ) ) ;
59- this . $on ( 'resized' , ( ) => this . __setProps ( ) ) ;
60-
61- this . $on (
62- 'scrolled' ,
63- /**
64- * @typedef {import('../services/scroll').ScrollServiceProps } ScrollServiceProps
65- * @param {Event & { detail: [ScrollServiceProps] } } event
66- */
67- ( { detail : [ props ] } ) => {
68- this . $services . toggle ( 'ticked' , props . changed . y || props . changed . x ) ;
69- }
98+ ticked ( ) {
99+ // X axis
100+ this . __props . current . x = clamp ( window . pageXOffset , this . __props . start . x , this . __props . end . x ) ;
101+ this . __props . progress . x = clamp01 (
102+ ( this . __props . current . x - this . __props . start . x ) /
103+ ( this . __props . end . x - this . __props . start . x )
104+ ) ;
105+ this . __props . dampedProgress . x = damp (
106+ this . __props . progress . x ,
107+ this . __props . dampedProgress . x ,
108+ this . dampFactor ,
109+ this . dampPrecision
70110 ) ;
71111
72- this . $on ( 'ticked' , ( ) => {
73- // X axis
74- this . __props . current . x = clamp (
75- window . pageXOffset ,
76- this . __props . start . x ,
77- this . __props . end . x
78- ) ;
79- this . __props . progress . x = clamp01 (
80- ( this . __props . current . x - this . __props . start . x ) /
81- ( this . __props . end . x - this . __props . start . x )
82- ) ;
83-
84- // Y axis
85- this . __props . current . y = clamp (
86- window . pageYOffset ,
87- this . __props . start . y ,
88- this . __props . end . y
89- ) ;
90- this . __props . progress . y = clamp01 (
91- ( this . __props . current . y - this . __props . start . y ) /
92- ( this . __props . end . y - this . __props . start . y )
93- ) ;
94-
95- // @ts -ignore
96- this . __callMethod ( 'scrolledInView' , this . __props ) ;
97- } ) ;
112+ // Y axis
113+ this . __props . current . y = clamp ( window . pageYOffset , this . __props . start . y , this . __props . end . y ) ;
114+ this . __props . progress . y = clamp01 (
115+ ( this . __props . current . y - this . __props . start . y ) /
116+ ( this . __props . end . y - this . __props . start . y )
117+ ) ;
118+ this . __props . dampedProgress . y = damp (
119+ this . __props . progress . y ,
120+ this . __props . dampedProgress . y ,
121+ this . dampFactor ,
122+ this . dampPrecision
123+ ) ;
124+
125+ if (
126+ this . __props . dampedProgress . x === this . __props . progress . x &&
127+ this . __props . dampedProgress . y === this . __props . progress . y
128+ ) {
129+ this . $services . disable ( 'ticked' ) ;
130+ }
131+
132+ // @ts -ignore
133+ this . __callMethod ( 'scrolledInView' , this . __props ) ;
98134 }
99135
100136 /**
@@ -118,24 +154,16 @@ export default function withScrolledInView(BaseClass) {
118154 const xCurrent = clamp ( window . pageXOffset , xStart , xEnd ) ;
119155 const xProgress = clamp01 ( ( xCurrent - xStart ) / ( xEnd - xStart ) ) ;
120156
121- this . __props = {
122- start : {
123- x : xStart ,
124- y : yStart ,
125- } ,
126- end : {
127- x : xEnd ,
128- y : yEnd ,
129- } ,
130- current : {
131- x : xCurrent ,
132- y : yCurrent ,
133- } ,
134- progress : {
135- x : xProgress ,
136- y : yProgress ,
137- } ,
138- } ;
157+ this . __props . start . x = xStart ;
158+ this . __props . start . y = yStart ;
159+ this . __props . end . x = xEnd ;
160+ this . __props . end . y = yEnd ;
161+ this . __props . current . x = xCurrent ;
162+ this . __props . current . y = yCurrent ;
163+ this . __props . progress . x = xProgress ;
164+ this . __props . progress . y = yProgress ;
165+ this . __props . dampedProgress . x = damp ( xProgress , this . __props . dampedProgress . x ) ;
166+ this . __props . dampedProgress . y = damp ( yProgress , this . __props . dampedProgress . y ) ;
139167 }
140168 } ;
141169}
0 commit comments