就労継続支援A型事業所「GREEN WOOD」logo画像 就労継続支援A型事業所「GREEN WOOD」tel画像
  • Top

    トップ

  • Gallery

    ギャラリー

galleryページへ戻る

タイトル:スターダスト(12月)

使用言語:p5.js(Javascriptフレームワーク)

プログラミングコード

   
  
/******************
* 変数設定
*******************/
//ダストの数
const num=100;
//ダストの配列設定
let dia;
let dias=[];

//雪
let snow;
let snows=[];

function setup(){

//描画するキャンバス設定
//responsible用
if(windowWidth>=820){
cv=createCanvas(970,520,WEBGL);
}else if(windowWidth < 820 && windowWidth>=450){
cv=createCanvas(windowWidth,520,WEBGL);
}else if(windowWidth < 450){
cv=createCanvas(windowWidth,200,WEBGL);
}

cv.parent('#myCanvas1');
//angleMode(DEGREES);

//インスタンス設定
for(let i=0;i < num;i++){
dia=new Dia(random(-width/2,width/2),-height/2-random(height),0,1,1,1);
dias.push(dia);
}

}


function draw(){
//背景
background(0,50,50,200);

//ダストの設定
for(let i=0;i < dias.length;i++){

dias[i].move();   //ダストの移動メソッド
dias[i].show();   //ダストの描画メソッド
dias[i].remove(); //ダストの削除メソッド

}

//雪
if(random(1)>0.5){
snow=new Snow();
snows.push(snow);
}

for(let i=0;i < snows.length;i++){
if(!snows[i].remove()){
snows[i].move();
snows[i].show();
}else{
snows.splice(i,1);
}
}

}

/*********************************************
ブラウザ幅を変更したときのキャンバスサイズ再設定
**********************************************/

function windowResized() {
if(windowWidth>=820){
resizeCanvas(970, 430);
}else if(windowWidth < 820){
resizeCanvas(windowWidth, 430);
}
}


/******************
* スターダストクラス
*******************/

class Dia{
//コンストラクタ
constructor(x,y,z,vx,vy,vz){
this.pos=createVector(x,y,z);
this.vel=createVector(vx,vy,vz).mult(0.1); 
this.l=random(100);

//ダストのサイズ
if(windowWidth > =820){
this.size=random(15);
}else if(windowWidth < 820 && windowWidth>=450){
this.size=random(12);
}else if(windowWidth < 450){
this.size=random(8);
}

}

//ダスト動作メソッド
move(){
this.pos.add(this.vel);
this.pos.x=this.pos.x+sin(frameCount/1000);
this.pos.y=this.pos.y+1;
this.pos.z=this.pos.z-0.05*cos(frameCount/1000);
}

//ダスト再設定メソッド
remove(){
if(this.pos.y > height){
this.pos.x=random(-width/2,width/2);
this.pos.y=-height/2-random(height/2);
}
}
//ダストの描画メソッド
show(){

push();
noFill();
stroke(random(255),random(255),random(255));

translate(this.pos.x,this.pos.y,this.pos.z);

for (let y = 0; y <= 500; y = y + 500) {
for (let x = 0; x <= 500; x = x + 500) {
rotateX(frameCount * 0.01);
rotateY(frameCount * 0.01);
box(this.size,this.size,this.size);
}
}

translate(-this.pos.x,-this.pos.y,-this.pos.z);
pop();

}

}


/******************
* 雪クラス手前
*******************/

class Snow{
//コンストラクタ
constructor(){
this.pos=createVector(random(-width/2,width/2),random(-height/2,-height/3));
this.vel=createVector(0,1).mult(1);  //ベクトル設定
this.r=random(0.5,1);  //雪のサイズ
this.alpha=random(150,255); //雪の透明度
}

//雪の動きメソッド
move(){
this.pos.add(this.vel);
}

//雪の除去判定メソッド
remove(){
if(this.pos.y>=height){
return true;
}else{
return false;
}
}

//雪の描画メソッド
show(){
push();
translate(this.pos.x,this.pos.y);
scale(this.r);
noStroke();
fill(255,255,255,this.alpha);
ellipse(this.pos.x,this.pos.y,5,5);
translate(-this.pos.x,-this.pos.y);
pop();
}

}

    
  
galleryページへ戻る

©グリーンウッド All Right Reserved 2024