-
Notifications
You must be signed in to change notification settings - Fork 1
/
hypercard.js
142 lines (117 loc) · 3.19 KB
/
hypercard.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class HyperCard extends HTMLElement {
static tagName = "hyper-card";
static register(tagName, registry) {
if(!registry && ("customElements" in globalThis)) {
registry = globalThis.customElements;
}
registry?.define(tagName || this.tagName, this);
}
static classes = {
active: "active"
};
static css = `
@media (prefers-reduced-motion: no-preference) {
:host {
--_hypercard-scale: var(--hypercard-scale, 1.07);
/* Useful if you want a different parent to create the stacking context */
position: var(--hypercard-position, relative);
transition-duration: 300ms;
transition-property: transform, box-shadow;
transition-timing-function: ease-out;
transform: rotate3d(0);
overflow: clip;
}
:host(.${HyperCard.classes.active}) {
transition-duration: 150ms;
box-shadow: 0 5px 20px 5px #00000044;
}
:host .glow {
position: absolute;
z-index: var(--hypercard-glow-zindex, 0);
left: 0;
top: 0;
right: 0;
bottom: 0;
background-image: radial-gradient(circle at 50% -20%, #ffffff22, #0000000f);
pointer-events: none;
}
:host(:not(.${HyperCard.classes.active})) .glow {
display: none;
}
}
`;
useAnimation() {
return "matchMedia" in window && !window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
connectedCallback() {
// https://caniuse.com/mdn-api_cssstylesheet_replacesync
if(this.shadowRoot || !("replaceSync" in CSSStyleSheet.prototype)) {
return;
}
let shadowroot = this.attachShadow({ mode: "open" });
let sheet = new CSSStyleSheet();
sheet.replaceSync(HyperCard.css);
shadowroot.adoptedStyleSheets = [sheet];
let slot = document.createElement("slot");
shadowroot.appendChild(slot);
this.glow = document.createElement("div");
this.glow.classList.add("glow");
shadowroot.appendChild(this.glow);
this.bindEvents();
}
// Full credit to this: https://codepen.io/markmiro/pen/wbqMPa
rotateToMouse(e, bounds) {
const mouseX = e.clientX;
const mouseY = e.clientY;
const leftX = mouseX - bounds.x;
const topY = mouseY - bounds.y;
const center = {
x: leftX - bounds.width / 2,
y: topY - bounds.height / 2
}
const distance = Math.sqrt(center.x**2 + center.y**2);
this.style.transform = `
scale3d(var(--_hypercard-scale), var(--_hypercard-scale), var(--_hypercard-scale))
rotate3d(
${center.y / 100},
${-center.x / 100},
0,
${Math.log(distance)* 2}deg
)
`;
this.glow.style.backgroundImage = `
radial-gradient(
circle at
${center.x * 2 + bounds.width/2}px
${center.y * 2 + bounds.height/2}px,
#ffffff55,
#0000000f
)
`;
}
bindEvents() {
let bounds;
let handler = (e) => {
this.rotateToMouse(e, bounds);
};
this.addEventListener("mouseenter", () => {
if(!this.useAnimation()) {
return;
}
bounds = this.getBoundingClientRect();
this.classList.add(HyperCard.classes.active);
document.addEventListener("mousemove", handler);
});
this.addEventListener("mouseleave", () => {
if(!this.useAnimation()) {
return;
}
this.classList.remove(HyperCard.classes.active);
document.removeEventListener("mousemove", handler);
this.style.transform = "";
this.style.background = "";
});
}
}
HyperCard.register();
export { HyperCard }