質問編集履歴

3

追加情報

2018/02/02 03:40

投稿

MASA08
MASA08

スコア24

test CHANGED
File without changes
test CHANGED
@@ -25,3 +25,263 @@
25
25
 
26
26
 
27
27
  なお、開発はrailsで行なっており、現在はhtml.erbのファイルにsketch.jsを読み込ませて、上記サイトのhtmlファイルに記述されているscriptをコピペしている状態です。
28
+
29
+
30
+
31
+ 追記
32
+
33
+ 現在、railsで開発をしていて、index.html.erbは下記のようになっております。
34
+
35
+ ```script
36
+
37
+ <canvas id="canvas"></canvas>
38
+
39
+ <section id="body">
40
+
41
+ // 中身
42
+
43
+ </section>
44
+
45
+
46
+
47
+ <script src="../js/sketch.js"></script>
48
+
49
+ <script>
50
+
51
+
52
+
53
+ // ---------------------------------------- Particle ----------------------------------------
54
+
55
+ function Particle(x, y, radius) {
56
+
57
+ this.init(x, y, radius);
58
+
59
+ }
60
+
61
+ Particle.prototype = {
62
+
63
+ init: function (x, y, radius) {
64
+
65
+ this.alive = true;
66
+
67
+ this.radius = radius || 10;
68
+
69
+ this.wander = 0.15;
70
+
71
+ this.theta = random(TWO_PI);
72
+
73
+ this.drag = 0.92;
74
+
75
+ this.color = '#fff';
76
+
77
+ this.x = x || 0.0;
78
+
79
+ this.y = y || 0.0;
80
+
81
+ this.vx = 0.0;
82
+
83
+ this.vy = 0.0;
84
+
85
+ },
86
+
87
+ move: function () {
88
+
89
+ this.x += this.vx;
90
+
91
+ this.y += this.vy;
92
+
93
+ this.vx *= this.drag;
94
+
95
+ this.vy *= this.drag;
96
+
97
+ this.theta += random(-0.5, 0.5) * this.wander;
98
+
99
+ this.vx += sin(this.theta) * 0.1;
100
+
101
+ this.vy += cos(this.theta) * 0.1;
102
+
103
+ this.radius *= 0.96;
104
+
105
+ this.alive = this.radius > 0.5;
106
+
107
+ },
108
+
109
+ draw: function (ctx) {
110
+
111
+ ctx.beginPath();
112
+
113
+ ctx.arc(this.x, this.y, this.radius, 0, TWO_PI);
114
+
115
+ ctx.fillStyle = this.color;
116
+
117
+ ctx.fill();
118
+
119
+ }
120
+
121
+ };
122
+
123
+ // ---------------------------------------- Example ----------------------------------------
124
+
125
+ var MAX_PARTICLES = 280;
126
+
127
+ var COLOURS = [
128
+
129
+ '#69D2E7',
130
+
131
+ '#A7DBD8',
132
+
133
+ '#E0E4CC',
134
+
135
+ '#F38630',
136
+
137
+ '#FA6900',
138
+
139
+ '#FF4E50',
140
+
141
+ '#F9D423'
142
+
143
+ ];
144
+
145
+ var particles = [];
146
+
147
+ var pool = [];
148
+
149
+ var demo = Sketch.create({container: document.getElementById('canvas'), retina: 'auto'});
150
+
151
+ demo.setup = function () {
152
+
153
+ // Set off some initial particles.
154
+
155
+ var i,
156
+
157
+ x,
158
+
159
+ y;
160
+
161
+ for (i = 0; i < 20; i++) {
162
+
163
+ x = (demo.width * 0.5) + random(-100, 100);
164
+
165
+ y = (demo.height * 0.5) + random(-100, 100);
166
+
167
+ demo.spawn(x, y);
168
+
169
+ }
170
+
171
+ };
172
+
173
+ demo.spawn = function (x, y) {
174
+
175
+
176
+
177
+ var particle,
178
+
179
+ theta,
180
+
181
+ force;
182
+
183
+ if (particles.length >= MAX_PARTICLES)
184
+
185
+ pool.push(particles.shift());
186
+
187
+ particle = pool.length
188
+
189
+ ? pool.pop()
190
+
191
+ : new Particle();
192
+
193
+ particle.init(x, y, random(5, 40));
194
+
195
+ particle.wander = random(0.5, 2.0);
196
+
197
+ particle.color = random(COLOURS);
198
+
199
+ particle.drag = random(0.9, 0.99);
200
+
201
+ theta = random(TWO_PI);
202
+
203
+ force = random(2, 8);
204
+
205
+ particle.vx = sin(theta) * force;
206
+
207
+ particle.vy = cos(theta) * force;
208
+
209
+ particles.push(particle);
210
+
211
+ };
212
+
213
+ demo.update = function () {
214
+
215
+ var i,
216
+
217
+ particle;
218
+
219
+ for (i = particles.length - 1; i >= 0; i--) {
220
+
221
+ particle = particles[i];
222
+
223
+ if (particle.alive)
224
+
225
+ particle.move();
226
+
227
+ else
228
+
229
+ pool.push(particles.splice(i, 1)[0]);
230
+
231
+ }
232
+
233
+ };
234
+
235
+ demo.draw = function () {
236
+
237
+ demo.globalCompositeOperation = 'lighter';
238
+
239
+ for (var i = particles.length - 1; i >= 0; i--) {
240
+
241
+ particles[i].draw(demo);
242
+
243
+ }
244
+
245
+ };
246
+
247
+ demo.mousemove = function () {
248
+
249
+ var particle,
250
+
251
+ theta,
252
+
253
+ force,
254
+
255
+ touch,
256
+
257
+ max,
258
+
259
+ i,
260
+
261
+ j,
262
+
263
+ n;
264
+
265
+ for (i = 0, n = demo.touches.length; i < n; i++) {
266
+
267
+ touch = demo.touches[i],
268
+
269
+ max = random(1, 4);
270
+
271
+ for (j = 0; j < max; j++) {
272
+
273
+ demo.spawn(touch.x, touch.y);
274
+
275
+ }
276
+
277
+ }
278
+
279
+ };
280
+
281
+ </script>
282
+
283
+ ```
284
+
285
+
286
+
287
+ ですがスケッチが背景に指定されません。どなたか回答お願いします。

2

誤字修正

2018/02/02 03:40

投稿

MASA08
MASA08

スコア24

test CHANGED
@@ -1 +1 @@
1
- Slick.jsのパーティクルの実装をbackroundに指定したい
1
+ Sketch.jsのパーティクルの実装をbackroundに指定したい
test CHANGED
File without changes

1

誤字修正

2018/02/01 15:04

投稿

MASA08
MASA08

スコア24

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 現在以下のサイトを見てslick.jsのparticleの実装を行なっています。
1
+ 現在以下のサイトを見てsketch.jsのparticleの実装を行なっています。
2
2
 
3
3
  http://soulwire.github.io/sketch.js/examples/particles.html
4
4
 
@@ -24,4 +24,4 @@
24
24
 
25
25
 
26
26
 
27
- なお、開発はrailsで行なっており、現在はhtml.erbのファイルにslick.jsを読み込ませて、上記サイトのhtmlファイルに記述されているscriptをコピペしている状態です。
27
+ なお、開発はrailsで行なっており、現在はhtml.erbのファイルにsketch.jsを読み込ませて、上記サイトのhtmlファイルに記述されているscriptをコピペしている状態です。