-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
polygon.vue
156 lines (143 loc) · 3.37 KB
/
polygon.vue
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
<template>
<div>
<svg width="300" height="300">
<Tweezing :to="stats" tween="tweenjs" :duration="updateInterval" :easing="easing">
<polygon
slot-scope="points"
:points="transformToPoints(points)"
></polygon>
</Tweezing>
<circle cx="150" cy="150" r="140"></circle>
</svg>
<label>Sides: {{ sides }}</label>
<input
type="range"
min="3"
max="500"
v-model.number="sides"
>
<label>Minimum Radius: {{ (minRadius * 100).toFixed(0) }}%</label>
<input
type="range"
min="0"
step="0.01"
max="0.9"
v-model.number="minRadius"
>
<label>Update Interval: {{ updateInterval }}ms</label>
<input
type="range"
min="10"
max="2000"
v-model.number="updateInterval"
>
<label class="text">
Easing Equation
<select v-model="equationType">
<option v-for="easing in easings" :value="easing.value">{{ easing.text }}</option>
</select>
</label>
<br>
<label class="radio">
<input v-model="easingType" type="radio" name="easing-type" value="In">
In
</label>
<label class="radio">
<input v-model="easingType" type="radio" name="easing-type" value="Out">
Out
</label>
<label class="radio">
<input v-model="easingType" type="radio" name="easing-type" value="InOut">
InOut
</label>
</div>
</template>
<script>
import TWEEN from '@tweenjs/tween.js'
function valueToPoint(value, index, total) {
const x = 0
const y = -value * 0.9
const angle = Math.PI * 2 / total * index
const cos = Math.cos(angle) * 150
const sin = Math.sin(angle) * 150
return {
x: x * cos - y * sin + 150,
y: x * sin + y * cos + 150,
}
}
function generatePoints(stats) {
const total = stats.length
return stats
.map(function(stat, index) {
const point = valueToPoint(stat, index, total)
return point.x + ',' + point.y
})
.join(' ')
}
export default {
data() {
const defaultSides = 10
return {
sides: defaultSides,
stats: Array.from(Array(defaultSides), () => 0),
minRadius: 0.5,
updateInterval: 500,
// custom easings
equationType: TWEEN.Easing.Linear,
easingType: 'Out',
}
},
computed: {
easing() {
return this.equationType[this.easingType]
},
easings() {
return Object.keys(TWEEN.Easing).map(easing => ({
value: TWEEN.Easing[easing],
text: easing,
}))
},
},
mounted() {
this.setupInterval()
},
watch: {
updateInterval: 'setupInterval',
},
methods: {
randomizeStats() {
this.stats = Array.from(
Array(this.sides),
() => (1 - this.minRadius) * Math.random() + this.minRadius
)
},
setupInterval() {
if (this.interval) clearInterval(this.interval)
this.interval = setInterval(this.randomizeStats.bind(this), this.updateInterval)
},
transformToPoints(pointsArray) {
const { length } = pointsArray
return pointsArray
.map((p, i) => valueToPoint(p, i, length))
.reduce((points, { x, y }) => points + ` ${x},${y}`, '')
},
},
}
</script>
<style scoped>
svg {
display: block;
}
polygon {
fill: var(--color-purple);
}
circle {
fill: transparent;
stroke: #35495e;
}
input[type='range'] {
display: block;
width: 100%;
margin-bottom: 15px;
}
</style>