Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Processing.js アニメーションの基本

Processing.js アニメーションの基本

Atsushi Tadokoro

May 29, 2014
Tweet

More Decks by Atsushi Tadokoro

Other Decks in Technology

Transcript

  1. Ξχϝʔγϣϯ ‣ ҎԼͷίʔυΛهೖ float posX, posY; //ԁͷத৺ҐஔΛ֨ೲ͢Δม਺ float speedX, speedY;

    //ԁͷ଎౓Λ֨ೲ͢Δม਺ void setup() { size(640, 480); //640x480pixelͷը໘Λੜ੒ frameRate(60); //ϑϨʔϜϨʔτ stroke(63, 191, 255); //ઢͷ৭ fill(0, 127, 255, 127); //ృΓͷ৭ posX = 40; //ԁͷॳظҐஔX posY = 40; //ԁͷॳظҐஔY speedX = 3; //ԁͷॳظҐஔX speedY = 2; //ԁͷॳظҐஔY } ! void draw() { background(15); //എܠΛඳը ellipse(posX, posY, 20, 20); //ࢦఆͨ͠ҐஔʹԁΛඳը posX = posX + speedX; //ԁͷX࠲ඪΛߋ৽ posY = posY + speedY; //ԁͷY࠲ඪΛߋ৽ }
  2. Ξχϝʔγϣϯ ‣ ৚݅෼ذΛ͔ͭͬͯɺนͰό΢ϯυͤͯ͞ΈΔ float posX, posY; //ԁͷத৺ҐஔΛ֨ೲ͢Δม਺ float speedX, speedY;

    //ԁͷ଎౓Λ֨ೲ͢Δม਺ ! void setup() { size(640, 480); //640x480pixelͷը໘Λੜ੒ frameRate(60); //ϑϨʔϜϨʔτ stroke(63, 191, 255); //ઢͷ৭ fill(0, 127, 255, 127); //ృΓͷ৭ posX = 40; //ԁͷॳظҐஔX posY = 40; //ԁͷॳظҐஔY speedX = 3; //ԁͷॳظҐஔX speedY = 2; //ԁͷॳظҐஔY }
  3. Ξχϝʔγϣϯ ‣ ৚݅෼ذΛ͔ͭͬͯɺนͰό΢ϯυͤͯ͞ΈΔ void draw() { background(15); //എܠΛඳը ellipse(posX, posY,

    20, 20); //ࢦఆͨ͠ҐஔʹԁΛඳը posX = posX + speedX; //ԁͷX࠲ඪΛߋ৽ posY = posY + speedY; //ԁͷY࠲ඪΛߋ৽ if (posX < 0 || posX > width) { //΋͠ը໘ͷࠨ୺·ͨ͸ӈ୺ʹ౸ୡͨ͠Β speedX = speedX * -1; //Xํ޲ͷεϐʔυΛ൓స } if (posY < 0 || posY > height) { //΋͠ը໘ͷԼ୺·ͨ͸্୺ʹ౸ୡͨ͠Β speedY = speedY * -1; //Yํ޲ͷεϐʔυΛ൓స } }
  4. Ξχϝʔγϣϯ ‣ ഑ྻΛ࢖༻ͯ͠ɺͨ͘͞ΜͷਤܗΛಈ͔͢ int NUM = 100; float posX[] =

    new float[NUM]; //ԁͷத৺ҐஔΛ֨ೲ͢Δม਺ float posY[] = new float[NUM]; float speedX[] = new float[NUM]; //ԁͷ଎౓Λ֨ೲ͢Δม਺ float speedY[] = new float[NUM]; ! void setup() { size(640, 480); //640x480pixelͷը໘Λੜ੒ frameRate(60); //ϑϨʔϜϨʔτ stroke(63, 191, 255); //ઢͷ৭ fill(0, 127, 255, 127); //ృΓͷ৭ //NUMճ͘Γ͔͑͠ for (int i = 0; i < NUM; i++) { posX[i] = random(width); //ԁͷॳظҐஔX posY[i] = random(height); //ԁͷॳظҐஔY speedX[i] = random(-5,5); //ԁͷॳظҐஔX speedY[i] = random(-5,5); //ԁͷॳظҐஔY } }
  5. Ξχϝʔγϣϯ ‣ ഑ྻΛ࢖༻ͯ͠ɺͨ͘͞ΜͷਤܗΛಈ͔͢ void draw() { background(15); //എܠΛඳը //NUMճ͘Γ͔͑͠ for

    (int i = 0; i < NUM; i++) { ellipse(posX[i], posY[i], 20, 20); //ࢦఆͨ͠ҐஔʹԁΛඳը posX[i] = posX[i] + speedX[i]; //ԁͷX࠲ඪΛߋ৽ posY[i] = posY[i] + speedY[i]; //ԁͷY࠲ඪΛߋ৽ //΋͠ը໘ͷࠨ୺ɺ·ͨ͸ӈ୺ʹ౸ୡͨ͠Β if (posX[i] < 0 || posX[i] > width) { speedX[i] = speedX[i] * -1; //Xํ޲ͷεϐʔυΛ൓స } //΋͠ը໘ͷԼ୺ɺ·ͨ͸্୺ʹ౸ୡͨ͠Β if (posY[i] < 0 || posY[i] > height) { speedY[i] = speedY[i] * -1; //Yํ޲ͷεϐʔυΛ൓స } } }