-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
111 lines (82 loc) · 2.86 KB
/
script.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
var size = 25;
var x_center = window.innerWidth/2;
var y_center = window.innerHeight/2;
var get_mouse_pos = true;
var get_touch_pos = false;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
draw();
function draw() {
// canvas.addEventListener('mousedown', e => {
// get_mouse_pos = true;
// getMousePosition(canvas, e)
// });
// canvas.addEventListener('mouseup', e => {
// get_mouse_pos = false;
// });
canvas.addEventListener('mousemove', function(e) {
if(get_mouse_pos) {
getMousePosition(canvas, e)
}
})
canvas.addEventListener('touchstart', function(e) {
getTouchPosition(canvas,e);
event.preventDefault();
}, false);
canvas.addEventListener('touchend', function(e) {
get_touch_pos = false;
}, false);
canvas.addEventListener('touchmove', function(e) {
getTouchPosition(canvas,e);
event.preventDefault();
}, false);
canvas.style.width = window.innerWidth + 'px' ;
canvas.style.height = window.innerHeight + 'px';
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let min = Math.min(canvas.width, canvas.height);
ctx.fillStyle = 'rgba(0,0,0,0.8)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
let columns = canvas.width/size;
let rows = canvas.height/size;
let scale_radius = 2.5;
for (let i = 0; i < columns; i++ ) {
for (let j = 0; j < rows; j++) {
let dist = Math.sqrt((scale_radius*(size*(i + 0.5) - x_center)/min)**4 + (scale_radius*(size*(j + 0.5) - y_center)/min)**4)/Math.SQRT2;
let x_shift = dist*(2*Math.random() - 1);
let y_shift = dist*(2*Math.random() - 1);
plus(size*(i + 0.5),
size*(j + 0.5),
x_shift, y_shift,
size,
3,
'hsla(280,100%,60%, 0.7)');
}
}
window.requestAnimationFrame(draw);
}
function plus(x, y, x_shift, y_shift, size, line_width, color) {
ctx.strokeStyle = color;
ctx.lineWidth = line_width;
ctx.beginPath();
ctx.moveTo(x + x_shift*size/2, y - size/2);
ctx.lineTo(x + x_shift*size/2, y + size/2);
ctx.closePath();
ctx.stroke();
ctx.strokeStyle = color;
ctx.lineWidth = line_width;
ctx.beginPath();
ctx.moveTo(x - size/2, y + y_shift*size/2);
ctx.lineTo(x + size/2, y + y_shift*size/2);
ctx.closePath();
ctx.stroke();
}
function getMousePosition(canvas, event) {
x_center = event.clientX;
y_center = event.clientY;
}
function getTouchPosition(canvas, event) {
var touch = event.touches[0];
x_center = touch.clientX;
y_center = touch.clientY;
}