Skip to content

Commit dfd202a

Browse files
committed
the centering is wrong lmao but heres what I have
1 parent b53f8d1 commit dfd202a

File tree

5 files changed

+141
-294
lines changed

5 files changed

+141
-294
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"version": "0.1.0",
66
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
77
"dependencies": {
8-
"anime": "^0.1.2",
8+
"animejs": "^3.2.0",
99
"gatsby": "^2.23.12",
1010
"gatsby-image": "^2.4.9",
1111
"gatsby-plugin-manifest": "^2.4.14",

src/components/animated-svg.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
import React from 'react';
22
import styled from 'styled-components';
3+
import useWindowDimensions from '../hooks/useWindowDimensions';
34

45
const Morph = styled.svg`
5-
position: relative;
6-
height: 100%;
7-
fill: var(--background-color-2);
8-
flex: none;
6+
position: absolute;
7+
top: -9999px;
8+
bottom: -9999px;
9+
left: -9999px;
10+
right: -9999px;
11+
margin: auto;
912
`
10-
class AnimatedSVG extends React.Component {
11-
12-
13-
render(props) {
14-
return (
15-
<Morph className="morph" width="100%" height="100%" viewBox="0 0 1400 770">
16-
<path d={props.path} />
17-
</Morph>
18-
)
19-
/*return (
20-
<Morph>
21-
<Anime easing="easeOutElastic"
22-
duration={shape.animation.path.duration}
23-
direction="alternate"
24-
loop={true}
25-
autoplay={true}
26-
d={shape.pathAlt}
27-
scale={[shape.scaleX, shape.scaleY]}>
28-
<path d={shape.path} />
29-
</Anime>
13+
const MorphWrap = styled.div`
14+
position: relative;
15+
overflow: hidden;
16+
width:100%;
17+
height:100%;
18+
`
19+
const AnimatedSVG = (props) => {
20+
const { height, width } = useWindowDimensions();
21+
const scale = 1.1;
22+
const offset = (width / 100);
23+
return (
24+
<MorphWrap>
25+
<Morph className="morph" width="100%" height="100%" viewBox={`${0 + offset} 0 ${(width + offset)} ${height}`}>
26+
<path className="front-path" d={props.frontPath} />width - 1600)/2)
27+
<path className="back-path" d={props.backPath} />
3028
</Morph>
31-
);
32-
*/
33-
}
34-
} export default AnimatedSVG;
29+
</MorphWrap>
30+
);
31+
}
32+
export default AnimatedSVG;
33+

src/hooks/useWindowDimensions.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useState, useEffect } from 'react';
2+
3+
function getWindowDimensions() {
4+
const { innerWidth: width, innerHeight: height } = window;
5+
return {
6+
width,
7+
height
8+
};
9+
}
10+
11+
export default function useWindowDimensions() {
12+
const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions());
13+
14+
useEffect(() => {
15+
function handleResize() {
16+
setWindowDimensions(getWindowDimensions());
17+
}
18+
19+
window.addEventListener('resize', handleResize);
20+
return () => window.removeEventListener('resize', handleResize);
21+
}, []);
22+
23+
return windowDimensions;
24+
}

src/pages/index.js

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useEffect } from "react"
22
import styled from "styled-components"
3-
import anime from "anime"
3+
import anime from "animejs"
44

55
import Landing from "../components/landing"
66
import Values from "../components/values"
@@ -31,24 +31,34 @@ const IndexPage = () => {
3131
const DOM = {};
3232
const shapes = [
3333
{
34-
path: "M1097.71 325.749C998.758 479.321 773.301 300.383 620.828 417.246C533.189 484.417 582.901 683.182 487.244 742.999C345.661 831.533 157.766 799.075 114.455 665.972C50.3149 468.857 246.827 454.214 315.253 370.857C417.892 245.821 141.549 21.1992 512.274 -0.79818C697.581 -11.7936 1191.95 179.503 1097.71 325.749Z",
35-
pathAlt: 'M778.388 350.375C755.786 279.743 687.273 226.063 606.753 187.216C526.233 148.369 378.484 111.51 295.975 173.09C213.467 234.67 150.475 322.122 126.46 431.601C102.445 541.08 192.147 695.762 327.759 757.918C463.372 820.074 648.426 774.87 736.715 667.51C825.005 560.15 800.99 421.006 778.388 350.375Z',
36-
scaleX: 1.2,
37-
scaleY: 1,
34+
path: ["M329.085 554.908C281.328 450.071 280.786 -9.26052 228.705 -40.6883C176.623 -72.1161 -52.9267 148.72 -1.85414 326.836C54.3956 523.008 268.363 554.736 345.912 641.025C434.268 739.339 482.986 1111.32 685.982 951.743C791.023 869.169 697.639 670.034 582.808 666.807C493.816 664.307 376.842 659.746 329.085 554.908Z",
35+
"M571.999 462C723.361 343.701 695 148 938 104.5C1181 61 1560 609 1325 864.5C1151.78 1052.83 131.648 979.999 69.4995 817.5C-39.5003 532.499 420.638 580.299 571.999 462Z"],
36+
pathAlt: ["M329.085 554.908C281.328 450.071 280.786 -9.26052 228.705 -40.6883C176.623 -72.1161 -52.9267 148.72 -1.85414 326.836C54.3956 523.008 268.363 554.736 345.912 641.025C434.268 739.339 482.986 1111.32 685.982 951.743C791.023 869.169 697.639 670.034 582.808 666.807C493.816 664.307 376.842 659.746 329.085 554.908Z",
37+
"M571.999 462C723.361 343.701 695 148 938 104.5C1181 61 1560 609 1325 864.5C1151.78 1052.83 131.648 979.999 69.4995 817.5C-39.5003 532.499 420.638 580.299 571.999 462Z"],
38+
scaleX: 1.1,
39+
scaleY: 1.1,
3840
rotate: 0,
39-
tx: -30,
40-
ty: -300,
41-
fill: {
42-
color: '#282faf',
41+
tx: 0,
42+
ty: 0,
43+
fill: [{
44+
color: '#00ffff',
4345
duration: 500,
4446
easing: 'linear'
45-
},
47+
},{
48+
color: '#ffff00',
49+
duration: 500,
50+
easing: 'linear'
51+
}],
4652
animation: {
47-
path: {
53+
path: [{
4854
duration: 3000,
4955
easing: 'easeOutElastic',
5056
elasticity: 600
51-
},
57+
},{
58+
duration: 3000,
59+
easing: 'easeOutElastic',
60+
elasticity: 600
61+
}],
5262
svg: {
5363
duration: 2000,
5464
easing: 'easeOutElastic'
@@ -183,7 +193,8 @@ const IndexPage = () => {
183193
}
184194
];
185195
DOM.svg = document.querySelector('.morph');
186-
DOM.shapeEl = DOM.svg.querySelector('path');
196+
DOM.frontShapeEl = DOM.svg?.querySelector('.front-path');
197+
DOM.backShapeEl = DOM.svg?.querySelector('.back-path');
187198

188199
// initShapeEl
189200
useEffect(() => {
@@ -198,37 +209,67 @@ const IndexPage = () => {
198209
translateY: shapes[0].ty + 'px',
199210
rotate: shapes[0].rotate + 'deg'
200211
});
212+
201213
}, []);
202214
// initShapeLoop
203215
useEffect(() => {
204-
anime.remove(DOM.shapeEl);
216+
anime.remove(DOM.frontShapeEl);
217+
anime.remove(DOM.backShapeEl);
205218
anime({
206-
targets: DOM.shapeEl,
219+
targets: DOM.frontShapeEl,
207220
easing: 'linear',
208-
d: [{ value: shapes[initShapeLoopStep].pathAlt, duration: 3500 }, { value: shapes[initShapeLoopStep].path, duration: 3500 }],
221+
d: [{ value: shapes[initShapeLoopStep].pathAlt[0], duration: 3500 }, { value: shapes[initShapeLoopStep].path[0], duration: 3500 }],
209222
loop: true,
210223
fill: {
211-
value: shapes[initShapeLoopStep].fill.color,
212-
duration: shapes[initShapeLoopStep].fill.duration,
213-
easing: shapes[initShapeLoopStep].fill.easing
224+
value: shapes[initShapeLoopStep].fill[0].color,
225+
duration: shapes[initShapeLoopStep].fill[0].duration,
226+
easing: shapes[initShapeLoopStep].fill[0].easing
227+
},
228+
direction: 'alternate'
229+
});
230+
anime({
231+
targets: DOM.backShapeEl,
232+
easing: 'linear',
233+
d: [{ value: shapes[initShapeLoopStep].pathAlt[1], duration: 3500 }, { value: shapes[initShapeLoopStep].path[1], duration: 3500 }],
234+
loop: true,
235+
fill: {
236+
value: shapes[initShapeLoopStep].fill[1].color,
237+
duration: shapes[initShapeLoopStep].fill[1].duration,
238+
easing: shapes[initShapeLoopStep].fill[1].easing
214239
},
215240
direction: 'alternate'
216241
});
217242
}, [initShapeLoopStep]);
218243

219244
// On step change
220245
useEffect(() => {
221-
anime.remove(DOM.shapeEl);
246+
anime.remove(DOM.frontShapeEl);
247+
anime.remove(DOM.backShapeEl);
248+
anime({
249+
targets: DOM.frontShapeEl,
250+
duration: shapes[step].animation.path[0].duration,
251+
easing: shapes[step].animation.path[0].easing,
252+
elasticity: shapes[step].animation.path[0].elasticity || 0,
253+
d: shapes[step].path[0],
254+
fill: {
255+
value: shapes[step].fill[0].color,
256+
duration: shapes[step].fill[0].duration,
257+
easing: shapes[step].fill[0].easing
258+
},
259+
complete: function () {
260+
setShapeLoopStep(step);
261+
}
262+
});
222263
anime({
223-
targets: DOM.shapeEl,
224-
duration: shapes[step].animation.path.duration,
225-
easing: shapes[step].animation.path.easing,
226-
elasticity: shapes[step].animation.path.elasticity || 0,
227-
d: shapes[step].path,
264+
targets: DOM.backShapeEl,
265+
duration: shapes[step].animation.path[1].duration,
266+
easing: shapes[step].animation.path[1].easing,
267+
elasticity: shapes[step].animation.path[1].elasticity || 0,
268+
d: shapes[step].path[1],
228269
fill: {
229-
value: shapes[step].fill.color,
230-
duration: shapes[step].fill.duration,
231-
easing: shapes[step].fill.easing
270+
value: shapes[step].fill[1].color,
271+
duration: shapes[step].fill[1].duration,
272+
easing: shapes[step].fill[1].easing
232273
},
233274
complete: function () {
234275
setShapeLoopStep(step);
@@ -252,11 +293,13 @@ const IndexPage = () => {
252293
const getCallbacks = (idx) => {
253294
return {
254295
enterViewport: (watcher) => {
296+
console.log(idx);
255297
setStep(idx);
256298
},
257299
exitViewport: (watcher) => {
258300
const pos = !watcher.isAboveViewport ? idx - 1 : idx + 1;
259301
if (pos <= NUM_SECTIONS && step !== pos) {
302+
console.log(pos);
260303
setStep(pos);
261304
}
262305
}
@@ -266,7 +309,7 @@ const IndexPage = () => {
266309
return (
267310
<Background>
268311
<MorphWrap className="morph-wrap">
269-
<AnimatedSVG path={this.shapes[0].pathAlt} />
312+
<AnimatedSVG frontPath={shapes[0].pathAlt[0]} backPath={shapes[0].pathAlt[1]}/>
270313
</MorphWrap>
271314
<Landing callbacks={getCallbacks(0)} />
272315
<Values callbacks={getCallbacks(1)} />

0 commit comments

Comments
 (0)