-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
143 lines (128 loc) · 3.32 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test 01 - Processing.js</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.6/processing.min.js"></script>
<script type="text/javascript">
window.onload = function() {
var canvas = document.getElementsByTagName('canvas')[0];
// Processing のソースコードが書かれた script 要素
var codeElm = document.getElementById('processing-code');
// 要素の内容を取得する
var code = codeElm.textContent || codeElm.innerText || codeElm.text;
new Processing(canvas, code);
};
</script>
<script id="processing-code" type="application/processing">
static int WIDTH = 640;
static int HEIGHT = 480;
static int SPACE = 60;
static int SPEED = 10; // the higher value, the slower speed
int getRandomColor() {
return random(0, 255);
}
int getNextPosition(int from, int to) {
if (from != to) {
return from + ((to - from) / SPEED);
}
return to;
}
static int CHILD_BASE_X = 600;
static int CHILD_BASE_Y = 40;
static int CHILD_COUNT = 8;
class Parent {
int x = 50;
int y = 240;
int r = 25;
int[] colors;
Parent() {
colors = new int[3];
for (int i = 0; i<3; i++) {
colors[i] = getRandomColor();
}
}
}
class Child {
int x;
int y;
int r = 25;
int[] colors;
Child() {
x = random(0, WIDTH);
y = random(0, HEIGHT);
colors = new int[3];
for (int i = 0; i<3; i++) {
colors[i] = getRandomColor();
}
}
}
Parent parent = new Parent();
Child[] children = new Child[CHILD_COUNT];
void setup() {
size(WIDTH, HEIGHT);
noStroke();
for (int i = 0; i < CHILD_COUNT; i++) {
children[i] = new Child();
}
frameRate(60);
}
void draw() {
background(0);
// parent ellipse
fill(parent.colors[0], parent.colors[1], parent.colors[2]);
ellipse(parent.x, parent.y, parent.r*2, parent.r*2);
// child ellipses
for (int i = 0; i < CHILD_COUNT; i++) {
Child child = children[i];
fill(child.colors[0], child.colors[1], child.colors[2]);
ellipse(child.x, child.y, child.r*2, child.r*2);
}
if (mouseX >= parent.x - parent.r && mouseX <= parent.x + parent.r
&& mouseY >= parent.y - parent.r && mouseY <= parent.y + parent.r) {
int targetX, targetY;
// when onmouseover
for (int i = 0; i < CHILD_COUNT; i++) {
if (i == 0) {
targetX = parent.x + SPACE;
targetY = parent.y;
} else {
targetX = children[i-1].x + SPACE;
targetY = children[i-1].y;
}
Child child = children[i];
if (child.x != targetX) {
child.x = getNextPosition(child.x, targetX);
}
if (child.y != targetY) {
child.y = getNextPosition(child.y, targetY);
}
}
} else {
// when onmouseout
for (int i = 0; i < CHILD_COUNT; i++) {
if (i == 0) {
targetX = CHILD_BASE_X;
targetY = CHILD_BASE_Y;
} else {
targetX = children[i-1].x;
targetY = children[i-1].y;
}
Child child = children[i];
if (child.x != targetX) {
child.x = getNextPosition(child.x, targetX);
}
if (child.y != targetY) {
child.y = getNextPosition(child.y, targetY);
}
}
}
}
</script>
</head>
<body>
<div>
<canvas width="640" height="480" id="canvas"></canvas>
</div>
</body>
</html>