質問編集履歴
3
追加情報
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,4 +11,134 @@
|
|
11
11
|
|
12
12
|
どなたかお教えしていただくことは可能でしょうか?
|
13
13
|
|
14
|
-
なお、開発はrailsで行なっており、現在はhtml.erbのファイルにsketch.jsを読み込ませて、上記サイトのhtmlファイルに記述されているscriptをコピペしている状態です。
|
14
|
+
なお、開発はrailsで行なっており、現在はhtml.erbのファイルにsketch.jsを読み込ませて、上記サイトのhtmlファイルに記述されているscriptをコピペしている状態です。
|
15
|
+
|
16
|
+
追記
|
17
|
+
現在、railsで開発をしていて、index.html.erbは下記のようになっております。
|
18
|
+
```script
|
19
|
+
<canvas id="canvas"></canvas>
|
20
|
+
<section id="body">
|
21
|
+
// 中身
|
22
|
+
</section>
|
23
|
+
|
24
|
+
<script src="../js/sketch.js"></script>
|
25
|
+
<script>
|
26
|
+
|
27
|
+
// ---------------------------------------- Particle ----------------------------------------
|
28
|
+
function Particle(x, y, radius) {
|
29
|
+
this.init(x, y, radius);
|
30
|
+
}
|
31
|
+
Particle.prototype = {
|
32
|
+
init: function (x, y, radius) {
|
33
|
+
this.alive = true;
|
34
|
+
this.radius = radius || 10;
|
35
|
+
this.wander = 0.15;
|
36
|
+
this.theta = random(TWO_PI);
|
37
|
+
this.drag = 0.92;
|
38
|
+
this.color = '#fff';
|
39
|
+
this.x = x || 0.0;
|
40
|
+
this.y = y || 0.0;
|
41
|
+
this.vx = 0.0;
|
42
|
+
this.vy = 0.0;
|
43
|
+
},
|
44
|
+
move: function () {
|
45
|
+
this.x += this.vx;
|
46
|
+
this.y += this.vy;
|
47
|
+
this.vx *= this.drag;
|
48
|
+
this.vy *= this.drag;
|
49
|
+
this.theta += random(-0.5, 0.5) * this.wander;
|
50
|
+
this.vx += sin(this.theta) * 0.1;
|
51
|
+
this.vy += cos(this.theta) * 0.1;
|
52
|
+
this.radius *= 0.96;
|
53
|
+
this.alive = this.radius > 0.5;
|
54
|
+
},
|
55
|
+
draw: function (ctx) {
|
56
|
+
ctx.beginPath();
|
57
|
+
ctx.arc(this.x, this.y, this.radius, 0, TWO_PI);
|
58
|
+
ctx.fillStyle = this.color;
|
59
|
+
ctx.fill();
|
60
|
+
}
|
61
|
+
};
|
62
|
+
// ---------------------------------------- Example ----------------------------------------
|
63
|
+
var MAX_PARTICLES = 280;
|
64
|
+
var COLOURS = [
|
65
|
+
'#69D2E7',
|
66
|
+
'#A7DBD8',
|
67
|
+
'#E0E4CC',
|
68
|
+
'#F38630',
|
69
|
+
'#FA6900',
|
70
|
+
'#FF4E50',
|
71
|
+
'#F9D423'
|
72
|
+
];
|
73
|
+
var particles = [];
|
74
|
+
var pool = [];
|
75
|
+
var demo = Sketch.create({container: document.getElementById('canvas'), retina: 'auto'});
|
76
|
+
demo.setup = function () {
|
77
|
+
// Set off some initial particles.
|
78
|
+
var i,
|
79
|
+
x,
|
80
|
+
y;
|
81
|
+
for (i = 0; i < 20; i++) {
|
82
|
+
x = (demo.width * 0.5) + random(-100, 100);
|
83
|
+
y = (demo.height * 0.5) + random(-100, 100);
|
84
|
+
demo.spawn(x, y);
|
85
|
+
}
|
86
|
+
};
|
87
|
+
demo.spawn = function (x, y) {
|
88
|
+
|
89
|
+
var particle,
|
90
|
+
theta,
|
91
|
+
force;
|
92
|
+
if (particles.length >= MAX_PARTICLES)
|
93
|
+
pool.push(particles.shift());
|
94
|
+
particle = pool.length
|
95
|
+
? pool.pop()
|
96
|
+
: new Particle();
|
97
|
+
particle.init(x, y, random(5, 40));
|
98
|
+
particle.wander = random(0.5, 2.0);
|
99
|
+
particle.color = random(COLOURS);
|
100
|
+
particle.drag = random(0.9, 0.99);
|
101
|
+
theta = random(TWO_PI);
|
102
|
+
force = random(2, 8);
|
103
|
+
particle.vx = sin(theta) * force;
|
104
|
+
particle.vy = cos(theta) * force;
|
105
|
+
particles.push(particle);
|
106
|
+
};
|
107
|
+
demo.update = function () {
|
108
|
+
var i,
|
109
|
+
particle;
|
110
|
+
for (i = particles.length - 1; i >= 0; i--) {
|
111
|
+
particle = particles[i];
|
112
|
+
if (particle.alive)
|
113
|
+
particle.move();
|
114
|
+
else
|
115
|
+
pool.push(particles.splice(i, 1)[0]);
|
116
|
+
}
|
117
|
+
};
|
118
|
+
demo.draw = function () {
|
119
|
+
demo.globalCompositeOperation = 'lighter';
|
120
|
+
for (var i = particles.length - 1; i >= 0; i--) {
|
121
|
+
particles[i].draw(demo);
|
122
|
+
}
|
123
|
+
};
|
124
|
+
demo.mousemove = function () {
|
125
|
+
var particle,
|
126
|
+
theta,
|
127
|
+
force,
|
128
|
+
touch,
|
129
|
+
max,
|
130
|
+
i,
|
131
|
+
j,
|
132
|
+
n;
|
133
|
+
for (i = 0, n = demo.touches.length; i < n; i++) {
|
134
|
+
touch = demo.touches[i],
|
135
|
+
max = random(1, 4);
|
136
|
+
for (j = 0; j < max; j++) {
|
137
|
+
demo.spawn(touch.x, touch.y);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
};
|
141
|
+
</script>
|
142
|
+
```
|
143
|
+
|
144
|
+
ですがスケッチが背景に指定されません。どなたか回答お願いします。
|
2
誤字修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
Sketch.jsのパーティクルの実装をbackroundに指定したい
|
body
CHANGED
File without changes
|
1
誤字修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
現在以下のサイトを見て
|
1
|
+
現在以下のサイトを見てsketch.jsのparticleの実装を行なっています。
|
2
2
|
http://soulwire.github.io/sketch.js/examples/particles.html
|
3
3
|
|
4
4
|
基本的にはコードをコピペしているだけです。
|
@@ -11,4 +11,4 @@
|
|
11
11
|
|
12
12
|
どなたかお教えしていただくことは可能でしょうか?
|
13
13
|
|
14
|
-
なお、開発はrailsで行なっており、現在はhtml.erbのファイルに
|
14
|
+
なお、開発はrailsで行なっており、現在はhtml.erbのファイルにsketch.jsを読み込ませて、上記サイトのhtmlファイルに記述されているscriptをコピペしている状態です。
|