-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.js
249 lines (200 loc) · 5.41 KB
/
counter.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var vector = new Two.Vector()
var entities = []
var mouse
var copy = [
"HTML",
"CSS",
"JAVASCRIPT",
"THREEJS",
"REACT",
"NEXTJS",
"FIGMA",
"WEBFLOW",
"FRAMER",
"REDUX",
"C++",
"BLENDER",
"R3F",
"WEBGL",
]
let myEle = document.querySelector("#matterC")
console.log(myEle)
var two = new Two({
type: Two.Types.canvas,
fullscreen: true,
autostart: true,
}).appendTo(myEle)
myEle.style.backgroundColor = "black"
var solver = Matter.Engine.create()
solver.world.gravity.y = 1
var bounds = {
length: 5000,
thickness: 50,
properties: {
isStatic: true,
},
}
// bounds.top = createBoundary(bounds.length, bounds.thickness);
bounds.left = createBoundary(bounds.thickness, bounds.length)
bounds.right = createBoundary(bounds.thickness, bounds.length)
bounds.bottom = createBoundary(bounds.length, bounds.thickness)
Matter.World.add(solver.world, [
/*bounds.top.entity,*/ bounds.left.entity,
bounds.right.entity,
bounds.bottom.entity,
])
var defaultStyles = {
size: two.width * 0.08,
weight: 400,
fill: "#9E9E9E",
leading: two.width * 0.08 * 0.8,
family: "Angus, Arial, sans-serif",
alignment: "center",
baseline: "baseline",
margin: {
top: 0,
left: 0,
right: 0,
bottom: 0,
},
}
addSlogan()
resize()
mouse = addMouseInteraction()
two.bind("resize", resize).bind("update", update)
function addMouseInteraction() {
// add mouse control
var mouse = Matter.Mouse.create(myEle)
var mouseConstraint = Matter.MouseConstraint.create(solver, {
mouse: mouse,
constraint: {
stiffness: 0.2,
},
})
Matter.World.add(solver.world, mouseConstraint)
return mouseConstraint
}
function resize() {
var length = bounds.length
var thickness = bounds.thickness
// vector.x = two.width / 2;
// vector.y = - thickness / 2;
// Matter.Body.setPosition(bounds.top.entity, vector);
vector.x = -thickness / 2
vector.y = two.height / 2
Matter.Body.setPosition(bounds.left.entity, vector)
vector.x = two.width + thickness / 2
vector.y = two.height / 2
Matter.Body.setPosition(bounds.right.entity, vector)
vector.x = two.width / 2
vector.y = two.height + thickness / 2
Matter.Body.setPosition(bounds.bottom.entity, vector)
var size
if (two.width < 800) {
size = two.width * 0.12
} else if (two.width > 1080 && two.width < 1600) {
size = two.width * 0.07
} else if (two.width > 1600) {
size = two.width * 0.06
} else {
size = two.width * 0.08
}
var leading = size * 0.8
for (var i = 0; i < two.scene.children.length; i++) {
var child = two.scene.children[i]
if (!child.isWord) {
continue
}
var text = child.text
var rectangle = child.rectangle
var entity = child.entity
text.size = size
text.leading = leading
var rect = text.getBoundingClientRect(true)
rectangle.width = rect.width
rectangle.height = rect.height
Matter.Body.scale(entity, 1 / entity.scale.x, 1 / entity.scale.y)
Matter.Body.scale(entity, rect.width, rect.height)
entity.scale.set(rect.width, rect.height)
text.size = size / 3
}
}
function addSlogan() {
var x = defaultStyles.margin.left
var y = -two.height // Header offset
for (var i = 0; i < copy.length; i++) {
var word = copy[i]
var group = new Two.Group()
var text = new Two.Text("", 0, 0, defaultStyles)
group.isWord = true
// Override default styles
if (word.value) {
text.value = word.value
for (var prop in word.styles) {
text[prop] = word.styles[prop]
}
} else {
text.value = word
}
var rect = text.getBoundingClientRect()
var ox = x + rect.width / 2
var oy = y + rect.height / 2
var ca = x + rect.width
var cb = two.width
// New line
if (ca >= cb) {
x = defaultStyles.margin.left
y +=
defaultStyles.leading +
defaultStyles.margin.top +
defaultStyles.margin.bottom
ox = x + rect.width / 2
oy = y + rect.height / 2
}
group.translation.x = ox
group.translation.y = oy
text.translation.y = 14
var rectangle = new Two.Rectangle(0, 0, rect.width, rect.height)
// rectangle.fill = 'rgb(255, 50, 50)';
rectangle.fill = "#1E1E1E"
rectangle.noStroke()
// rectangle.opacity = 0.75;
rectangle.visible = true
var entity = Matter.Bodies.rectangle(ox, oy, 1, 1)
Matter.Body.scale(entity, rect.width, rect.height)
entity.scale = new Two.Vector(rect.width, rect.height)
entity.object = group
entities.push(entity)
x += rect.width + defaultStyles.margin.left + defaultStyles.margin.right
group.text = text
group.rectangle = rectangle
group.entity = entity
group.add(rectangle, text)
two.add(group)
}
Matter.World.add(solver.world, entities)
}
function update(frameCount, timeDelta) {
var allBodies = Matter.Composite.allBodies(solver.world)
Matter.MouseConstraint.update(mouse, allBodies)
Matter.MouseConstraint._triggerEvents(mouse)
Matter.Engine.update(solver)
for (var i = 0; i < entities.length; i++) {
var entity = entities[i]
entity.object.position.copy(entity.position)
entity.object.rotation = entity.angle
}
}
function createBoundary(width, height) {
var rectangle = two.makeRectangle(0, 0, width, height)
rectangle.visible = false
rectangle.entity = Matter.Bodies.rectangle(
0,
0,
width,
height,
bounds.properties
)
rectangle.entity.position = rectangle.position
return rectangle
}