-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (60 loc) · 1.68 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
function Circle() {
// svg element variables
var svg = d3.select('svg')
// calc screen screen width
var windowWidth = document.documentElement.clientWidth
var windowHeight = document.documentElement.clientHeight
// movement speed in milliseconds
var speed = 750
// position init
var posX = windowWidth / 2
var posY = windowHeight / 2
// circle init
var r = 50
var circle = svg.append('circle')
.attr('cx', posX)
.attr('cy', posY)
.attr('r', r)
.style('fill', 'green')
.style('stroke', 'black')
// Spiral variables
var a = 5
var t = 0
function position() {
console.log("X: " + posX + " " + "Y: " + posY)
}
function randomColour() {
let randomColour = Math.floor(Math.random() * 16777215).toString(16)
return "#" + randomColour
}
function moveCircle(posX, posY) {
circle.transition().duration(speed)
.attr('cx', posX)
.attr('cy', posY)
.style('fill', randomColour())
}
function moveCircleInSpiral() {
// Calculate next point on spiral
posX = a * t * Math.cos(t)
posY = a * t * Math.sin(t)
// Move circle
moveCircle(posX + 500, posY + 500)
// Update 'time' variable
if (t !== 100) {
t += 1
} else {
t = 0
}
}
var functionToRun = function moveCircleRandomly() {
let min = 0 + r
let mX = windowWidth - r
let mY = windowHeight - r
// random number between min and max including max
posX = Math.ceil(Math.random() * (mX - min) + min)
posY = Math.ceil(Math.random() * (mY - min) + min)
moveCircle(posX, posY)
console.log(posX, posY)
}
setInterval(functionToRun, speed)
}