-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
193 lines (152 loc) · 4.2 KB
/
main.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
// 動かす要素
tg = $('.js-tg');
updateCnt = 0;
// マウス座標
mouse = {
x:0,
y:0
};
// 初期設定
init();
function init() {
$(window).on('mousemove', _eMouseMove).on('mousedown', _eMouseDown).on('mouseup', _eMouseUp);
update();
}
// ----------------------------------------
// イベント マウス動いた
// ----------------------------------------
function _eMouseMove(e) {
mouse.x = e.clientX;
mouse.y = e.clientY;
}
// ----------------------------------------
// イベント マウス押した
// ----------------------------------------
function _eMouseDown(e) {
}
// ----------------------------------------
// イベント マウス離した
// ----------------------------------------
function _eMouseUp(e) {
}
// 毎フレーム実行
window.requestAnimationFrame(update);
function update() {
var sw = window.innerWidth;
var sh = window.innerHeight;
// conic-gradient()作成
var grad = 'repeating-conic-gradient(';
// 線の色
var color = '#000000';
var per1 = map(mouse.x, 1, 25, 0, sw);
var per2 = per1 + map(mouse.y, 1, 25, 0, sh);
grad += 'transparent 0,';
grad += 'transparent ' + per1 + '%,';
grad += color + ' ' + per1 + '%,';
grad += color + ' ' + per2 + '%';
grad += ')';
var sizeW = map(mouse.x, 50, sw, 0, sw);
var sizeH = map(mouse.y, 50, sh, 0, sh);
tg.css({
backgroundImage:grad,
backgroundSize: sizeW + 'px ' + sizeH + 'px'
});
updateCnt++;
window.requestAnimationFrame(update);
}
// ########################################
// ユーティリティ系 ↓
// ########################################
// ----------------------------------------
// minからmaxまでランダム
// ----------------------------------------
function random(min, max) {
return Math.random() * (max - min) + min;
}
// ----------------------------------------
// minからmaxまでランダム int
// ----------------------------------------
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// ----------------------------------------
// minからmaxまでランダム 半分の確率で-をつける
// ----------------------------------------
function random2(min, max) {
var val = Math.random() * (max - min) + min;
if(Math.random() > 0.5) {
val *= -1;
}
return val;
}
// ----------------------------------------
// -valからvalまでランダム
// ----------------------------------------
function range(val) {
return random(-val, val);
}
// ----------------------------------------
// 配列の中ランダム
// ----------------------------------------
function randomArr(arr) {
return arr[randomInt(0, arr.length - 1)]
}
// 1 / rangeの確率でtrueを取得
// -----------------------------------
// @range : 2以上の分母(int)
// return : true or false(boolean)
// -----------------------------------
function hit(range) {
return (randomInt(0, range - 1) == 0)
}
// ----------------------------------------
// 度からラジアンに変換
// @val : 度
// ----------------------------------------
function radian(val) {
return val * Math.PI / 180;
}
// ----------------------------------------
// ラジアンから度に変換
// @val : ラジアン
// ----------------------------------------
function degree(val) {
return val * 180 / Math.PI;
}
// ----------------------------------------
// 範囲変換
// @val : 変換したい値
// @toMin : 変換後の最小値
// @toMax : 変換後の最大値
// @fromMin : 変換前の最小値
// @fromMax : 変換前の最大値
// ----------------------------------------
function map(val, toMin, toMax, fromMin, fromMax) {
if(val <= fromMin) {
return toMin;
}
if(val >= fromMax) {
return toMax;
}
p = (toMax - toMin) / (fromMax - fromMin);
return ((val - fromMin) * p) + toMin;
}
// 配列内のパラメータを比較してソート
// -----------------------------------
// @arr : 配列
// @para : パラメーター名
// @desc : 降順かどうか(boolean)
// -----------------------------------
function sort(arr, para, desc) {
if(desc) {
arr.sort(function(a, b) {
return b[para] - a[para];
}
)
} else {
arr.sort(function(a, b) {
return a[para] - b[para];
}
)
}
}