Ich versuche, Hintergrund für jedes erstellte Partikel zu erstellen. Leinwandmuster funktioniert nicht richtig. Ich erhalte diesen Fehler >>
"Syntaxerror: Eine ungültige oder illegale Zeichenfolge angegeben wurde"
HTMLBildhintergrund für Leinwand Partikel
<canvas id="canvas"></canvas>
JS
(function(){
window.onload = function(){
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d'),
particles = {},
particleIndex = 0,
particleNum = 1;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.width);
function Particle(){
this.x = canvas.width/2;
this.y = 0;
this.vx = Math.random() * 10 - 5;
this.vy = Math.random() * 10 - 5;
this.gravity = 0.2;
particleIndex++;
particles[particleIndex] = this;
this.id = particleIndex;
this.test = 0;
this.maxLife = 100;
}
Particle.prototype.draw = function(){
this.x += this.vx;
this.y += this.vy;
this.vy += this.gravity;
this.test++;
if (this.test >= this.maxLife) {
delete particles[this.id];
};
var img = new Image();
img.src = 'img/aaa.png';
var pattern = ctx.createPattern(img,'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(this.x, this.y, 20, 20);
};
setInterval(function(){
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < particleNum; i++) {
new Particle();
};
for(var i in particles) {
particles[i].draw();
}
},30)
}})();
Ich habe versucht, mit ctx.drawImage dies auch zu tun (), aber das Bild wurde nur einmal angezeigt.
Irgendwelche Tipps? :) Fiddle
Könnten Sie ein jsfiddle oder anderes Beispiel liefern, damit wir wissen, welche Bibliotheken Sie versuchen zu benutzen? – Cutter
Ich verwende keine Bibliothek. Es ist reines JS. Der obige Code ist vollständig. Sie können "ctx.fillStyle = pattern;" mit "ctx.fillStyle = '# f00';" und testen. Es funktioniert. –