-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxbrJS.js
More file actions
104 lines (95 loc) · 2.38 KB
/
Copy pathxbrJS.js
File metadata and controls
104 lines (95 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const { // try the one with another file
gsap: { registerPlugin, set, to, timeline },
MorphSVGPlugin,
Draggable
} = window;
registerPlugin(MorphSVGPlugin);
if (typeof window !== 'undefined') {
const { gsap: { registerPlugin, set, to, timeline }, MorphSVGPlugin, Draggable, } = window;
// Rest of your code using GSAP functions
} else {
// Handle the case where GSAP is not available (optional)
console.warn("GSAP library not loaded or not in a browser environment");
}
// Used to calculate distance of "tug"
let startX;
let startY;
const AUDIO = {
CLICK: new Audio("https://assets.codepen.io/605876/click.mp3")
};
const STATE = {
ON: false
};
const CORD_DURATION = 0.1;
const CORDS = document.querySelectorAll(".toggle-scene__cord");
const HIT = document.querySelector(".toggle-scene__hit-spot");
const DUMMY = document.querySelector(".toggle-scene__dummy-cord");
const DUMMY_CORD = document.querySelector(".toggle-scene__dummy-cord line");
const PROXY = document.createElement("div");
// set init position
const ENDX = DUMMY_CORD.getAttribute("x2");
const ENDY = DUMMY_CORD.getAttribute("y2");
const RESET = () => {
set(PROXY, {
x: ENDX,
y: ENDY
});
};
RESET();
const CORD_TL = timeline({
paused: true,
onStart: () => {
STATE.ON = !STATE.ON;
set(document.documentElement, { "--on": STATE.ON ? 1 : 0 });
set([DUMMY, HIT], { display: "none" });
set(CORDS[0], { display: "block" });
AUDIO.CLICK.play();
},
onComplete: () => {
set([DUMMY, HIT], { display: "block" });
set(CORDS[0], { display: "none" });
RESET();
}
});
for (let i = 1; i < CORDS.length; i++) {
CORD_TL.add(
to(CORDS[0], {
morphSVG: CORDS[i],
duration: CORD_DURATION,
repeat: 1,
yoyo: true
})
);
}
Draggable.create(PROXY, {
trigger: HIT,
type: "x,y",
onPress: (e) => {
startX = e.x;
startY = e.y;
},
onDrag: function () {
set(DUMMY_CORD, {
attr: {
x2: this.x,
y2: this.y
}
});
},
onRelease: function (e) {
const DISTX = Math.abs(e.x - startX);
const DISTY = Math.abs(e.y - startY);
const TRAVELLED = Math.sqrt(DISTX * DISTX + DISTY * DISTY);
to(DUMMY_CORD, {
attr: { x2: ENDX, y2: ENDY },
duration: CORD_DURATION,
onComplete: () => {
if (TRAVELLED > 50) {
CORD_TL.restart();
} else {
RESET();
}
}
});
}
});