質問編集履歴
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -34,8 +34,24 @@
|
|
34
34
|
|
35
35
|
###追記
|
36
36
|
```
|
37
|
+
<!DOCTYPE html>
|
38
|
+
<html lang="ja">
|
39
|
+
<head>
|
40
|
+
<meta charset="utf-8">
|
41
|
+
<title>Interactive Art</title>
|
42
|
+
<style>
|
43
|
+
canvas {
|
44
|
+
background: black;
|
45
|
+
}
|
46
|
+
</style>
|
47
|
+
</head>
|
48
|
+
<body>
|
49
|
+
<canvas id="mycanvas" width="500" height="250">
|
50
|
+
Canvasに対応したブラウザを使ってください。
|
51
|
+
</canvas>
|
37
52
|
|
53
|
+
<script>
|
38
|
-
|
54
|
+
var img = new Image();
|
39
55
|
|
40
56
|
img.onload = (function(){
|
41
57
|
|
@@ -47,5 +63,8 @@
|
|
47
63
|
});
|
48
64
|
|
49
65
|
img.src = "logo.PNG";
|
66
|
+
</script>
|
67
|
+
</html>
|
68
|
+
|
50
69
|
```
|
51
70
|
上記のコードを追記した結果、リロードするタイミングで一瞬だけ画像が表示されますが、すぐに消えてしまう現象に陥っております。
|
4
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -34,154 +34,18 @@
|
|
34
34
|
|
35
35
|
###追記
|
36
36
|
```
|
37
|
-
<script>
|
38
|
-
(function() {
|
39
|
-
"use strict";
|
40
37
|
|
41
|
-
var canvas;
|
42
|
-
var ctx;
|
43
|
-
var Ball;
|
44
|
-
|
38
|
+
var img = new Image();
|
45
|
-
var Stage;
|
46
|
-
var stage;
|
47
39
|
|
48
|
-
|
40
|
+
img.onload = (function(){
|
49
|
-
var Draw;
|
50
|
-
var draw;
|
51
|
-
/////////////////////////////////////////////////////////////////////////
|
52
|
-
var App;
|
53
|
-
var app;
|
54
|
-
/////////////////////////////////////////////////////////////////////////
|
55
41
|
|
42
|
+
var canvas = document.getElementById('logo');
|
56
43
|
|
57
|
-
canvas = document.getElementById("mycanvas");
|
58
|
-
if (!canvas || !canvas.getContext) return false;
|
59
|
-
|
44
|
+
// var ctx = canvas.getContext('2d');
|
60
45
|
|
61
|
-
|
46
|
+
ctx.drawImage(img, 0, 0);
|
62
|
-
return min + Math.floor(Math.random() * (max - min + 1));
|
63
|
-
|
47
|
+
});
|
64
48
|
|
65
|
-
function adjustPosition(pos, r, max) {
|
66
|
-
// if (x - r < 0) x = r;
|
67
|
-
// if (y - r < 0) y = r;
|
68
|
-
// if (x + r > canvas.width) x = canvas.width - r;
|
69
|
-
// if (y + r > canvas.height) y = canvas.height - r;
|
70
|
-
if (pos - r < 0) {
|
71
|
-
return r;
|
72
|
-
} else if (pos + r > max) {
|
73
|
-
return max - r;
|
74
|
-
} else {
|
75
|
-
return pos;
|
76
|
-
}
|
77
|
-
}
|
78
|
-
|
79
|
-
//
|
80
|
-
(function() {
|
81
|
-
var canvas = document.getElementById('mycanvas');
|
82
|
-
var container = document.getElementById('wrap');
|
83
|
-
sizing();
|
84
|
-
|
85
|
-
function sizing() {
|
86
|
-
canvas.height = container.offsetHeight;
|
87
|
-
canvas.width = container.offsetWidth;
|
88
|
-
}
|
89
|
-
|
90
|
-
window.addEventListener('resize', function() {
|
91
|
-
(!window.requestAnimationFrame) ? setTimeout(sizing, 300): window.requestAnimationFrame(sizing);
|
92
|
-
});
|
93
|
-
})();
|
94
|
-
//
|
95
|
-
|
96
|
-
|
97
|
-
canvas.addEventListener("click", function(e) {
|
98
|
-
var x, y, r;
|
99
|
-
var rect;
|
100
|
-
// x = rand(100, 400);
|
101
|
-
// y = rand(100, 200);
|
102
|
-
rect = e.target.getBoundingClientRect();
|
103
|
-
x = e.clientX - rect.left;
|
104
|
-
y = e.clientY - rect.top;
|
105
|
-
r = rand(0, 100) < 20 ? rand(50, 80) : rand(10, 35);
|
106
|
-
|
107
|
-
|
108
|
-
x = adjustPosition(x, r, canvas.width);
|
109
|
-
y = adjustPosition(y, r, canvas.height);
|
110
|
-
balls.push(new Ball(x, y, r));
|
111
|
-
});
|
112
|
-
|
113
|
-
Ball = function(x, y, r) {
|
114
|
-
this.x = x;
|
115
|
-
this.y = y;
|
116
|
-
this.r = r;
|
117
|
-
this.vx = rand(-10, 10);
|
118
|
-
this.vy = rand(-10, 10);
|
119
|
-
this.color = "hsla(" + rand(150, 250) + ", " + rand(60, 100) + "%," + rand(40, 60) + "%," + Math.random() + ")";
|
120
|
-
};
|
121
|
-
|
122
|
-
Ball.prototype.draw = function() {
|
123
|
-
ctx.beginPath();
|
124
|
-
ctx.arc(this.x, this.y, this.r, 0, Math.PI*2);
|
125
|
-
ctx.fillStyle = this.color;
|
126
|
-
ctx.closePath();
|
127
|
-
ctx.fill();
|
128
|
-
};
|
129
|
-
|
130
|
-
Ball.prototype.move = function() {
|
131
|
-
if (this.x + this.r > canvas.width || this.x - this.r < 0) {
|
132
|
-
this.vx *= -1;
|
133
|
-
}
|
134
|
-
if (this.y + this.r > canvas.height || this.y - this.r < 0) {
|
135
|
-
this.vy *= -1;
|
136
|
-
}
|
137
|
-
this.x += this.vx;
|
138
|
-
this.y += this.vy;
|
139
|
-
};
|
140
|
-
|
141
|
-
// var ball = new Ball(rand(100, 200), rand(100, 200), rand(10, 50));
|
142
|
-
// ball.draw();
|
143
|
-
|
144
|
-
Stage = function() {
|
145
|
-
this.update = function() {
|
146
|
-
var i;
|
147
|
-
this.clear();
|
148
|
-
for (i = 0; i < balls.length; i++) {
|
149
|
-
balls[i].draw();
|
150
|
-
balls[i].move();
|
151
|
-
}
|
152
|
-
setTimeout(function() {
|
153
|
-
this.update();
|
154
|
-
}.bind(this), 30);
|
155
|
-
};
|
156
|
-
this.clear = function() {
|
157
|
-
ctx.fillStyle = "black";
|
158
|
-
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
159
|
-
}
|
160
|
-
};
|
161
|
-
|
162
|
-
|
49
|
+
img.src = "logo.PNG";
|
163
|
-
img.addEventListener("load", function() {
|
164
|
-
stage = new Stage();
|
165
|
-
stage.update();
|
166
|
-
draw = new Draw();
|
167
|
-
});
|
168
|
-
/////////////////////////////////////////////////////////////////////////
|
169
|
-
App = function() {
|
170
|
-
this.draw = function() {
|
171
|
-
ctx.fillRect(0, 0, width, height);
|
172
|
-
|
173
|
-
var img_logo = new Image();
|
174
|
-
img_logo.src = 'img/logo.png';
|
175
|
-
ctx.drawImage(img_logo, 0, 0);
|
176
|
-
|
177
|
-
app.draw();
|
178
|
-
};
|
179
|
-
/////////////////////////////////////////////////////////////////////////
|
180
|
-
})();
|
181
|
-
</script>
|
182
|
-
</body>
|
183
|
-
</html>
|
184
50
|
```
|
185
|
-
|
51
|
+
上記のコードを追記した結果、リロードするタイミングで一瞬だけ画像が表示されますが、すぐに消えてしまう現象に陥っております。
|
186
|
-
他のコードはできているものをそのまま写した箇所です。
|
187
|
-
現在は画面が真っ白な状態となっております。
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -183,4 +183,5 @@
|
|
183
183
|
</html>
|
184
184
|
```
|
185
185
|
「///」で囲ってある箇所は画像を挿入するために自分でコーディングした箇所です。
|
186
|
-
他のコードはできているものをそのまま写した箇所です。
|
186
|
+
他のコードはできているものをそのまま写した箇所です。
|
187
|
+
現在は画面が真っ白な状態となっております。
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -34,19 +34,6 @@
|
|
34
34
|
|
35
35
|
###追記
|
36
36
|
```
|
37
|
-
<!DOCTYPE html>
|
38
|
-
<html lang="ja">
|
39
|
-
<head>
|
40
|
-
<meta charset="utf-8">
|
41
|
-
<title>インタラクティブアート</title>
|
42
|
-
<link rel="stylesheet" href="styles.css">
|
43
|
-
</head>
|
44
|
-
<body>
|
45
|
-
<div id="wrap">
|
46
|
-
<canvas id="mycanvas" width="100%" height="400">
|
47
|
-
Canvasに対応したブラウザを使ってください。
|
48
|
-
</canvas>
|
49
|
-
</div>
|
50
37
|
<script>
|
51
38
|
(function() {
|
52
39
|
"use strict";
|
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -30,4 +30,170 @@
|
|
30
30
|
###
|
31
31
|
原因が分かりません。アドバイスいただけないでしょうか。
|
32
32
|
|
33
|
-
以上、ご確認お願いいたします。
|
33
|
+
以上、ご確認お願いいたします。
|
34
|
+
|
35
|
+
###追記
|
36
|
+
```
|
37
|
+
<!DOCTYPE html>
|
38
|
+
<html lang="ja">
|
39
|
+
<head>
|
40
|
+
<meta charset="utf-8">
|
41
|
+
<title>インタラクティブアート</title>
|
42
|
+
<link rel="stylesheet" href="styles.css">
|
43
|
+
</head>
|
44
|
+
<body>
|
45
|
+
<div id="wrap">
|
46
|
+
<canvas id="mycanvas" width="100%" height="400">
|
47
|
+
Canvasに対応したブラウザを使ってください。
|
48
|
+
</canvas>
|
49
|
+
</div>
|
50
|
+
<script>
|
51
|
+
(function() {
|
52
|
+
"use strict";
|
53
|
+
|
54
|
+
var canvas;
|
55
|
+
var ctx;
|
56
|
+
var Ball;
|
57
|
+
var balls = [];
|
58
|
+
var Stage;
|
59
|
+
var stage;
|
60
|
+
|
61
|
+
var img = new Image();
|
62
|
+
var Draw;
|
63
|
+
var draw;
|
64
|
+
/////////////////////////////////////////////////////////////////////////
|
65
|
+
var App;
|
66
|
+
var app;
|
67
|
+
/////////////////////////////////////////////////////////////////////////
|
68
|
+
|
69
|
+
|
70
|
+
canvas = document.getElementById("mycanvas");
|
71
|
+
if (!canvas || !canvas.getContext) return false;
|
72
|
+
ctx = canvas.getContext("2d");
|
73
|
+
|
74
|
+
function rand(min, max) {
|
75
|
+
return min + Math.floor(Math.random() * (max - min + 1));
|
76
|
+
}
|
77
|
+
|
78
|
+
function adjustPosition(pos, r, max) {
|
79
|
+
// if (x - r < 0) x = r;
|
80
|
+
// if (y - r < 0) y = r;
|
81
|
+
// if (x + r > canvas.width) x = canvas.width - r;
|
82
|
+
// if (y + r > canvas.height) y = canvas.height - r;
|
83
|
+
if (pos - r < 0) {
|
84
|
+
return r;
|
85
|
+
} else if (pos + r > max) {
|
86
|
+
return max - r;
|
87
|
+
} else {
|
88
|
+
return pos;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
//
|
93
|
+
(function() {
|
94
|
+
var canvas = document.getElementById('mycanvas');
|
95
|
+
var container = document.getElementById('wrap');
|
96
|
+
sizing();
|
97
|
+
|
98
|
+
function sizing() {
|
99
|
+
canvas.height = container.offsetHeight;
|
100
|
+
canvas.width = container.offsetWidth;
|
101
|
+
}
|
102
|
+
|
103
|
+
window.addEventListener('resize', function() {
|
104
|
+
(!window.requestAnimationFrame) ? setTimeout(sizing, 300): window.requestAnimationFrame(sizing);
|
105
|
+
});
|
106
|
+
})();
|
107
|
+
//
|
108
|
+
|
109
|
+
|
110
|
+
canvas.addEventListener("click", function(e) {
|
111
|
+
var x, y, r;
|
112
|
+
var rect;
|
113
|
+
// x = rand(100, 400);
|
114
|
+
// y = rand(100, 200);
|
115
|
+
rect = e.target.getBoundingClientRect();
|
116
|
+
x = e.clientX - rect.left;
|
117
|
+
y = e.clientY - rect.top;
|
118
|
+
r = rand(0, 100) < 20 ? rand(50, 80) : rand(10, 35);
|
119
|
+
|
120
|
+
|
121
|
+
x = adjustPosition(x, r, canvas.width);
|
122
|
+
y = adjustPosition(y, r, canvas.height);
|
123
|
+
balls.push(new Ball(x, y, r));
|
124
|
+
});
|
125
|
+
|
126
|
+
Ball = function(x, y, r) {
|
127
|
+
this.x = x;
|
128
|
+
this.y = y;
|
129
|
+
this.r = r;
|
130
|
+
this.vx = rand(-10, 10);
|
131
|
+
this.vy = rand(-10, 10);
|
132
|
+
this.color = "hsla(" + rand(150, 250) + ", " + rand(60, 100) + "%," + rand(40, 60) + "%," + Math.random() + ")";
|
133
|
+
};
|
134
|
+
|
135
|
+
Ball.prototype.draw = function() {
|
136
|
+
ctx.beginPath();
|
137
|
+
ctx.arc(this.x, this.y, this.r, 0, Math.PI*2);
|
138
|
+
ctx.fillStyle = this.color;
|
139
|
+
ctx.closePath();
|
140
|
+
ctx.fill();
|
141
|
+
};
|
142
|
+
|
143
|
+
Ball.prototype.move = function() {
|
144
|
+
if (this.x + this.r > canvas.width || this.x - this.r < 0) {
|
145
|
+
this.vx *= -1;
|
146
|
+
}
|
147
|
+
if (this.y + this.r > canvas.height || this.y - this.r < 0) {
|
148
|
+
this.vy *= -1;
|
149
|
+
}
|
150
|
+
this.x += this.vx;
|
151
|
+
this.y += this.vy;
|
152
|
+
};
|
153
|
+
|
154
|
+
// var ball = new Ball(rand(100, 200), rand(100, 200), rand(10, 50));
|
155
|
+
// ball.draw();
|
156
|
+
|
157
|
+
Stage = function() {
|
158
|
+
this.update = function() {
|
159
|
+
var i;
|
160
|
+
this.clear();
|
161
|
+
for (i = 0; i < balls.length; i++) {
|
162
|
+
balls[i].draw();
|
163
|
+
balls[i].move();
|
164
|
+
}
|
165
|
+
setTimeout(function() {
|
166
|
+
this.update();
|
167
|
+
}.bind(this), 30);
|
168
|
+
};
|
169
|
+
this.clear = function() {
|
170
|
+
ctx.fillStyle = "black";
|
171
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
172
|
+
}
|
173
|
+
};
|
174
|
+
|
175
|
+
img.src = "img/logo.PNG";
|
176
|
+
img.addEventListener("load", function() {
|
177
|
+
stage = new Stage();
|
178
|
+
stage.update();
|
179
|
+
draw = new Draw();
|
180
|
+
});
|
181
|
+
/////////////////////////////////////////////////////////////////////////
|
182
|
+
App = function() {
|
183
|
+
this.draw = function() {
|
184
|
+
ctx.fillRect(0, 0, width, height);
|
185
|
+
|
186
|
+
var img_logo = new Image();
|
187
|
+
img_logo.src = 'img/logo.png';
|
188
|
+
ctx.drawImage(img_logo, 0, 0);
|
189
|
+
|
190
|
+
app.draw();
|
191
|
+
};
|
192
|
+
/////////////////////////////////////////////////////////////////////////
|
193
|
+
})();
|
194
|
+
</script>
|
195
|
+
</body>
|
196
|
+
</html>
|
197
|
+
```
|
198
|
+
「///」で囲ってある箇所は画像を挿入するために自分でコーディングした箇所です。
|
199
|
+
他のコードはできているものをそのまま写した箇所です。
|