-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgears.js
More file actions
101 lines (79 loc) · 2.51 KB
/
Copy pathgears.js
File metadata and controls
101 lines (79 loc) · 2.51 KB
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
var MAX_SIZE = 120;
var NUM_GEARS = 10;
var canvas;
var parent;
var gears = [];
var centerX;
var centerY;
var width;
var height;
function doGears(newParent) {
canvas = document.createElement("canvas"); //document.getElementById("test");
parent = newParent;
canvas.width = 512;
canvas.height = 512;
width = canvas.width;
height = canvas.height;
centerX = canvas.width / 2;
centerY = canvas.height / 2;
gears.push(new Gear(Math.random() * canvas.width, Math.random() * canvas.height, MAX_SIZE));
for (var i = 1; i < NUM_GEARS; i++) {
var angle = Math.random() * 2 * Math.PI;
var size = (Math.random() * (MAX_SIZE - 30)) + 30;
var distance = size + gears[i - 1].r;
var xpos = gears[i - 1].x + (distance * Math.cos(angle));
var ypos = gears[i - 1].y + (distance * Math.sin(angle));
if (xpos < 0) {
xpos += width;
}
if (ypos < 0) {
ypos += width;
}
xpos %= width;
ypos %= height;
gears.push(new Gear(xpos, ypos, size));
}
draw();
}
function draw() {
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgba(16,16,16,1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < gears.length; i++) {
gears[i].draw(ctx);
}
parent.style.background = "url(" + canvas.toDataURL("image/png") + ") repeat";
}
function Gear(x_, y_, r_) {
this.x = x_;
this.y = y_;
this.r = r_;
this.draw = function(ctx) {
ctx.fillStyle = 'rgba(128,128,128,1)';
this.drawGear(this.x, this.y, this.r, ctx);
if (this.x - this.r < 0) {
this.drawGear(this.x + width, this.y, this.r, ctx);
}
if (this.y - this.r < 0) {
this.drawGear(this.x, this.y + height, this.r, ctx);
}
if ((this.x - this.r < 0) && (this.y - this.r < 0)) {
this.drawGear(this.x + width, this.y + height, this.r, ctx);
}
if (this.x + this.r > width) {
this.drawGear(this.x - width, this.y, this.r, ctx);
}
if (this.y + this.r > height) {
this.drawGear(this.x, this.y - height, this.r, ctx);
}
if ((this.x + this.r > width) && (this.y + this.r > height)) {
this.drawGear(this.x - width, this.y - height, this.r, ctx);;
}
};
this.drawGear = function(x, y, r, ctx) {
ctx.beginPath();
ctx.arc(x, y, r, 0, 2 * Math.PI);
ctx.fill();
ctx.closePath();
};
}