javascriptでcanvasを生成して花吹雪を降らすコードを書きました。
動きはするし、花びらの挙動も思った通りに操作できたのですが、何故かサイトをリロードしたら花びらが出て来なくなり、2回リロードしてやっと作動する感じになっています。
ローカルでしか試していないのですが、urlから飛んでサイトが表示されたらちゃんと吹雪いてます。しかしやはりページ更新したら動かなくなり、もう一度リロードしたらまた動き出します。
何故そのような挙動になりのか分からず困っています。
花吹雪の全コードはこの通りです。
$(function(){ window.onload = function(){ var timer = setInterval(timerFunc,30); //canvas作成(桜吹雪) var canvas1 = document.createElement('canvas'); document.body.appendChild(canvas1); mainCanvasContext = canvas1.getContext("2d"); canvas1.width = 800; canvas1.height = 350; canvas1.style.position = "absolute"; canvas1.style.top = "0"; canvas1.style.left = "0"; canvas1.style.zIndex = "500"; stageX = canvas1.width; stageY = canvas1.height; init(); canvas1.onmousemove = function(e){//マウスの動き mouseX=e.clientX; mouseY=e.clientY; windVelX = (1 - mouseX / stageX)*35; windVelY = (0.6 - mouseY / stageY)*15; }; }; //------------------------------ //桜吹雪 //------------------------------ // forked from kanariia1's "forked: 桜吹雪" http://jsdo.it/kanariia1/vOcE // forked from njf's "桜吹雪" http://jsdo.it/njf/alFS function Particle(){ }; Particle.prototype.x = 0; Particle.prototype.y = 0; Particle.prototype.velX = 0; Particle.prototype.velY = 0; Particle.prototype.rad = 0; Particle.prototype.w = 0; Particle.prototype.time = 0; Particle.prototype.zrot = 0; Particle.prototype.zrotW = 0; Particle.prototype.scale = 1; var stageX = 445; var stageY = 445; var mainCanvasContext; var particles = new Array(); var mouseX = 0; var mouseY = 0; var windVelX = 0; var windVelY = 0; var image = new Image(); image.src = "";//花びら function initParitcle(initP){ initP.w = 0.4*(0.5-Math.random()); initP.rad = Math.PI*Math.random(); initP.velY = Math.random()*3+5;//縦の動き initP.velX = (-4-Math.random())*5;//横の動き initP.zrot = Math.PI*Math.random(); initP.zrotW = (0.5-Math.random())/100; initP.scale = (1.1-Math.random())*0.7;//花びらの大きさ initP.time = 0; } function init(){ for(var i = 0 ; i < 7 ; i++){//花びらの数 var p = new Particle(); initParitcle(p); p.y = (1.1-3.2*Math.random())*stageY; p.x = Math.random()*stageX; particles.push(p); } }; function sortParticle(p1,p2){ return(p1.scale-p2.scale); } function timerFunc( ){ mainCanvasContext.setTransform(1,0,0,1,0,0); //mainCanvasContext.fillStyle = 'rgba(86, 14, 17, 0.4)'; mainCanvasContext.fillRect(0, 0, stageX, stageY); mainCanvasContext.clearRect(0,0,stageX,stageY); particles.sort(sortParticle); for(var i = 0 ; i<particles.length; i++){ var p = particles[i]; var rad = p.rad; mainCanvasContext.setTransform(Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad), p.x+image.width*0.5, p.y+image.height*0.5 ); var s = Math.sin(p.zrot); var c = Math.cos(p.zrot); mainCanvasContext.scale(p.scale*s,p.scale*c); mainCanvasContext.drawImage( image, 0, 0, image.width, image.height , -image.width*0.5,-image.height*0.5, image.width,image.height ); p.time ++; p.x+=(p.velX+ windVelX)*(p.scale/3) ; p.y+=(p.velY+windVelY)*(Math.abs(p.scale)/3); p.rad+=p.w; p.zrot+=p.zrotW; if(p.rad > Math.PI){ p.rad = -Math.PI; }else if(p.rad < -Math.PI){ p.rad = Math.PI; } var ts = image.width*Math.abs(p.scale); if(p.y>stageY+ts||p.x<-ts||p.x>ts+stageX){ initParitcle(p); ts = image.width*Math.abs(p.scale) if(p.y>stageY+ts){ p.y = -ts; p.x = Math.random()*stageX; }else{ if((p.velX+ windVelX)*(p.scale/3)>0){ p.x = -ts; }else{ p.x = stageX+ts; } } } } } });
どうか、お力添えをm_ _m
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー