/** * TricoliaSprite * demonstrates the co-ordinated motion of transparent gif images * Cesare Brizio * cebrizi@tin.it * http://xoomer.alice.it/cebrizio/ * */ PImage Tricolia01; PImage Tricolia02; PImage Tricolia03; PImage Tricolia04; PImage Tricolia05; PImage Tricolia06; PImage Tricolia07; PImage Tricolia08; float incrhor; float incrver; float incrhor2; float incrver2; float incrhor3; float incrver3; float incrhor4; float incrver4; float incrhor5; float incrver5; float incrhor6; float incrver6; float incrhor7; float incrver7; float ifactorx; float ifactory; float xposT01; float yposT01; float xposT02; float yposT02; float xposT03; float yposT03; float xposT04; float yposT04; float xposT05; float yposT05; float xposT06; float yposT06; float xposT07; float yposT07; float xposT08; float yposT08; float NotPressedxposT01; float NotPressedyposT01; float NotPressedxposT02; float NotPressedyposT02; float NotPressedxposT03; float NotPressedyposT03; float NotPressedxposT04; float NotPressedyposT04; float NotPressedxposT05; float NotPressedyposT05; float NotPressedxposT06; float NotPressedyposT06; float NotPressedxposT07; float NotPressedyposT07; float NotPressedxposT08; float NotPressedyposT08; float PressedxposT01; float PressedyposT01; float PressedxposT02; float PressedyposT02; float PressedxposT03; float PressedyposT03; float PressedxposT04; float PressedyposT04; float PressedxposT05; float PressedyposT05; float PressedxposT06; float PressedyposT06; float PressedxposT07; float PressedyposT07; float PressedxposT08; float PressedyposT08; float SPIRALxposT01; float SPIRALyposT01; float SPIRALxposT02; float SPIRALyposT02; float SPIRALxposT03; float SPIRALyposT03; float SPIRALxposT04; float SPIRALyposT04; float SPIRALxposT05; float SPIRALyposT05; float SPIRALxposT06; float SPIRALyposT06; float SPIRALxposT07; float SPIRALyposT07; float SPIRALxposT08; float SPIRALyposT08; float LINExposT01; float LINEyposT01; float LINExposT02; float LINEyposT02; float LINExposT03; float LINEyposT03; float LINExposT04; float LINEyposT04; float LINExposT05; float LINEyposT05; float LINExposT06; float LINEyposT06; float LINExposT07; float LINEyposT07; float LINExposT08; float LINEyposT08; float diffx01; float diffy01; float diffx02; float diffy02; float diffx03; float diffy03; float diffx04; float diffy04; float diffx05; float diffy05; float diffx06; float diffy06; float diffx07; float diffy07; float diffx08; float diffy08; float drag1 = 18.0; float drag2 = 20.0; float drag3 = 22.0; float drag4 = 24.0; float drag5 = 26.0; float drag6 = 28.0; float drag7 = 30.0; float drag8 = 32.0; float ifactorX2 = 1.0; float ifactorY2 = 1.0; float ifactorX3 = 1.0; float ifactorY3 = 1.0; float ifactorX4 = 1.0; float ifactorY4 = 1.0; float ifactorX5 = 1.0; float ifactorY5 = 1.0; float ifactorX6 = 1.0; float ifactorY6 = 1.0; float ifactorX7 = 1.0; float ifactorY7 = 1.0; float ComeFromNotPressed = 1.0; float ComeFromPressed = 0.0; void setup() { size(400,400); background(255); Tricolia01 = loadImage("T_01.gif"); Tricolia02 = loadImage("T_02.gif"); Tricolia03 = loadImage("T_03.gif"); Tricolia04 = loadImage("T_04.gif"); Tricolia05 = loadImage("T_05.gif"); Tricolia06 = loadImage("T_06.gif"); Tricolia07 = loadImage("T_07.gif"); Tricolia08 = loadImage("T_08.gif"); frameRate(60); /* Set an initial spiral arrangement for the eight GIFs */ xposT08 = 175; yposT08 = 130; xposT07 = 198; yposT07 = 146; xposT06 = 208; yposT06 = 187; xposT05 = 191; yposT05 = 223; xposT04 = 159; yposT04 = 235; xposT03 = 132; yposT03 = 213; xposT02 = 146; yposT02 = 178; xposT01 = 169; yposT01 = 188; SPIRALxposT08 = 175; SPIRALyposT08 = 130; SPIRALxposT07 = 198; SPIRALyposT07 = 146; SPIRALxposT06 = 208; SPIRALyposT06 = 187; SPIRALxposT05 = 191; SPIRALyposT05 = 223; SPIRALxposT04 = 159; SPIRALyposT04 = 235; SPIRALxposT03 = 132; SPIRALyposT03 = 213; SPIRALxposT02 = 146; SPIRALyposT02 = 178; SPIRALxposT01 = 169; SPIRALyposT01 = 188; } void draw() { if (mousePressed) { /* As soon as the mouse is pressed, the GIFs will begin a motion from their present "NotPressed" position to the target position (spiral arrangement) */ if (ComeFromNotPressed > 0.0) { /* Only the first time after a mouse key is pressed the xpos/ypos for each GIF is set as the last position BEFORE the mousePressed event: the last xpos/ypos was saved in the "NotPressed" variables */ ComeFromNotPressed = 0.0; /* from now on, we are in the "Pressed" condition */ ComeFromPressed = 1.0; xposT01 = NotPressedxposT01; yposT01 = NotPressedyposT01; xposT02 = NotPressedxposT02; yposT02 = NotPressedyposT02; xposT03 = NotPressedxposT03; yposT03 = NotPressedyposT03; xposT04 = NotPressedxposT04; yposT04 = NotPressedyposT04; xposT05 = NotPressedxposT05; yposT05 = NotPressedyposT05; xposT06 = NotPressedxposT06; yposT06 = NotPressedyposT06; xposT07 = NotPressedxposT07; yposT07 = NotPressedyposT07; xposT08 = NotPressedxposT08; yposT08 = NotPressedyposT08; } background(255); /* Each GIF moves towards its target position in the spiral arrangement, with a "drag factor" specific to each GIF. */ float difxx1 = SPIRALxposT01 - xposT01; if(abs(difxx1) > 1.0) { xposT01 = xposT01 + difxx1/drag1; } float difyy1 = SPIRALyposT01 - yposT01; if(abs(difyy1) > 1.0) { yposT01 = yposT01 + difyy1/drag1; } float difxx2 = SPIRALxposT02 - xposT02; if(abs(difxx2) > 1.0) { xposT02 = xposT02 + difxx2/drag2; } float difyy2 = SPIRALyposT02 - yposT02; if(abs(difyy2) > 1.0) { yposT02 = yposT02 + difyy2/drag2; } float difxx3 = SPIRALxposT03 - xposT03; if(abs(difxx3) > 1.0) { xposT03 = xposT03 + difxx3/drag3; } float difyy3 = SPIRALyposT03 - yposT03; if(abs(difyy3) > 1.0) { yposT03 = yposT03 + difyy3/drag3; } float difxx4 = SPIRALxposT04 - xposT04; if(abs(difxx4) > 1.0) { xposT04 = xposT04 + difxx4/drag4; } float difyy4 = SPIRALyposT04 - yposT04; if(abs(difyy4) > 1.0) { yposT04 = yposT04 + difyy4/drag4; } float difxx5 = SPIRALxposT05 - xposT05; if(abs(difxx5) > 1.0) { xposT05 = xposT05 + difxx5/drag1; } float difyy5 = SPIRALyposT05 - yposT05; if(abs(difyy5) > 1.0) { yposT05 = yposT05 + difyy5/drag5; } float difxx6 = SPIRALxposT06 - xposT06; if(abs(difxx6) > 1.0) { xposT06 = xposT06 + difxx6/drag6; } float difyy6 = SPIRALyposT06 - yposT06; if(abs(difyy6) > 1.0) { yposT06 = yposT06 + difyy6/drag6; } float difxx7 = SPIRALxposT07 - xposT07; if(abs(difxx7) > 1.0) { xposT07 = xposT07 + difxx7/drag7; } float difyy7 = SPIRALyposT07 - yposT07; if(abs(difyy7) > 1.0) { yposT07 = yposT07 + difyy7/drag7; } float difxx8 = SPIRALxposT08 - xposT08; if(abs(difxx8) > 1.0) { xposT08 = xposT08 + difxx8/drag8; } float difyy8 = SPIRALyposT08 - yposT08; if(abs(difyy8) > 1.0) { yposT08 = yposT08 + difyy8/drag8; } /* The current position referred to the MousePressed condition is saved into the "Pressed" variables */ PressedxposT01 = xposT01; PressedyposT01 = yposT01; PressedxposT02 = xposT02; PressedyposT02 = yposT02; PressedxposT03 = xposT03; PressedyposT03 = yposT03; PressedxposT04 = xposT04; PressedyposT04 = yposT04; PressedxposT05 = xposT05; PressedyposT05 = yposT05; PressedxposT06 = xposT06; PressedyposT06 = yposT06; PressedxposT07 = xposT07; PressedyposT07 = yposT07; PressedxposT08 = xposT08; PressedyposT08 = yposT08; /* GIFs are displayed at their current "Pressed" position */ image(Tricolia08, PressedxposT08, PressedyposT08); image(Tricolia07, PressedxposT07, PressedyposT07); image(Tricolia06, PressedxposT06, PressedyposT06); image(Tricolia05, PressedxposT05, PressedyposT05); image(Tricolia04, PressedxposT04, PressedyposT04); image(Tricolia03, PressedxposT03, PressedyposT03); image(Tricolia02, PressedxposT02, PressedyposT02); image(Tricolia01, PressedxposT01, PressedyposT01); } else { /* At first, the GIFs will begin a motion from the default spiral arrangement. Each time the mouse is pressed then released, the motion of each GIF will start from its current "Pressed" position. The target arrangement is inline from mousex,mousey with the eighth GIF remaining in its central position, as in the spiral arrangement. */ if (ComeFromPressed > 0.0) { /* Only the first time after a mouse key is released the xpos/ypos for each GIF is set as the last position BEFORE the mouse key release: the last xpos/ypos was saved in the "Pressed" variables */ ComeFromPressed = 0.0; /* from now on, we are in the "NotPressed" condition */ ComeFromNotPressed = 1.0; xposT01 = PressedxposT01; yposT01 = PressedyposT01; xposT02 = PressedxposT02; yposT02 = PressedyposT02; xposT03 = PressedxposT03; yposT03 = PressedyposT03; xposT04 = PressedxposT04; yposT04 = PressedyposT04; xposT05 = PressedxposT05; yposT05 = PressedyposT05; xposT06 = PressedxposT06; yposT06 = PressedyposT06; xposT07 = PressedxposT07; yposT07 = PressedyposT07; xposT08 = PressedxposT08; yposT08 = PressedyposT08; } background(255); /* Provisions for the inline arrangement: the increment polarity is calculated for X and Y, respective to the position of the eighth "fixed" gif (the one at the spiral arrangement's center) */ if (mouseX>175) {ifactorx = -1;} else {ifactorx = 1;} if (mouseY>130) {ifactory = -1;} else {ifactory = 1;} /* Provisions for the inline arrangement: the increment value is determined as a seventh of the distance between the coordinates of the first GIF and the coordinates of the eighth GIF - Please note that an offset is added, so that the first GIF will be centered on the mouse cursor; the polarity of this offset is inverted for obvious reasons - this is why the multiplying polarity factor is applied twice*/ incrhor=abs(((mouseX - ((20 *ifactorx)*ifactorx))-175)/7); incrver=abs(((mouseY - ((25 *ifactory)*ifactory))-130)/7); /* now the target coordinates can be calculated. The choice of starting from the fixed position of the eighth GIF, gives an effect like a string with a fixed end, and this couples well with the spiral arrangement. If the same calculation was made backward starting from the mouse position, this would generate a much different effect, with all the eight GIFs moving, and with both the ends slowly stretching towards their final position */ LINExposT08 = 175; LINEyposT08 = 130; LINExposT07 = 175 - (incrhor*ifactorx); LINEyposT07 = 130 - (incrver*ifactory); LINExposT06 = 175 - ((incrhor*2)*ifactorx); LINEyposT06 = 130 - ((incrver*2)*ifactory); LINExposT05 = 175 - ((incrhor*3)*ifactorx); LINEyposT05 = 130 - ((incrver*3)*ifactory); LINExposT04 = 175 - ((incrhor*4)*ifactorx); LINEyposT04 = 130 - ((incrver*4)*ifactory); LINExposT03 = 175 - ((incrhor*5)*ifactorx); LINEyposT03 = 130 - ((incrver*5)*ifactory); LINExposT02 = 175 - ((incrhor*6)*ifactorx); LINEyposT02 = 130 - ((incrver*6)*ifactory); LINExposT01 = mouseX - ((20 *ifactorx)*ifactorx); LINEyposT01 = mouseY - ((25 *ifactory)*ifactory); /* Each GIF moves towards its target position in the inline arrangement, with a "drag factor" specific to each GIF. The most suitable setting for the drag factor is INCREASING the drag factor towards the fixed central GIF. */ float difx1 = LINExposT01 - xposT01; if(abs(difx1) > 1.0) { xposT01 = xposT01 + difx1/drag1; } float dify1 = LINEyposT01 - yposT01; if(abs(dify1) > 1.0) { yposT01 = yposT01 + dify1/drag1; } float difxx2 = LINExposT02 - xposT02; if(abs(difxx2) > 1.0) { xposT02 = xposT02 + difxx2/drag2; } float difyy2 = LINEyposT02 - yposT02; if(abs(difyy2) > 1.0) { yposT02 = yposT02 + difyy2/drag2; } float difxx3 = LINExposT03 - xposT03; if(abs(difxx3) > 1.0) { xposT03 = xposT03 + difxx3/drag3; } float difyy3 = LINEyposT03 - yposT03; if(abs(difyy3) > 1.0) { yposT03 = yposT03 + difyy3/drag3; } float difxx4 = LINExposT04 - xposT04; if(abs(difxx4) > 1.0) { xposT04 = xposT04 + difxx4/drag4; } float difyy4 = LINEyposT04 - yposT04; if(abs(difyy4) > 1.0) { yposT04 = yposT04 + difyy4/drag4; } float difxx5 = LINExposT05 - xposT05; if(abs(difxx5) > 1.0) { xposT05 = xposT05 + difxx5/drag5; } float difyy5 = LINEyposT05 - yposT05; if(abs(difyy5) > 1.0) { yposT05 = yposT05 + difyy5/drag5; } float difxx6 = LINExposT06 - xposT06; if(abs(difxx6) > 1.0) { xposT06 = xposT06 + difxx6/drag6; } float difyy6 = LINEyposT06 - yposT06; if(abs(difyy6) > 1.0) { yposT06 = yposT06 + difyy6/drag6; } float difxx7 = LINExposT07 - xposT07; if(abs(difxx7) > 1.0) { xposT07 = xposT07 + difxx7/drag7; } float difyy7 = LINEyposT07 - yposT07; if(abs(difyy7) > 1.0) { yposT07 = yposT07 + difyy7/drag7; } float difxx8 = LINExposT08 - xposT08; if(abs(difxx8) > 1.0) { xposT08 = xposT08 + difxx8/drag8; } float difyy8 = LINEyposT08 - yposT08; if(abs(difyy8) > 1.0) { yposT08 = yposT08 + difyy8/drag8; } /* The current position referred to the Mouse Not Pressed Pressed condition is saved into the "NotPressed" variables */ NotPressedxposT01 = xposT01; NotPressedyposT01 = yposT01; NotPressedxposT02 = xposT02; NotPressedyposT02 = yposT02; NotPressedxposT03 = xposT03; NotPressedyposT03 = yposT03; NotPressedxposT04 = xposT04; NotPressedyposT04 = yposT04; NotPressedxposT05 = xposT05; NotPressedyposT05 = yposT05; NotPressedxposT06 = xposT06; NotPressedyposT06 = yposT06; NotPressedxposT07 = xposT07; NotPressedyposT07 = yposT07; NotPressedxposT08 = xposT08; NotPressedyposT08 = yposT08; /* GIFs are displayed at their current "NotPressed" position */ image(Tricolia08, NotPressedxposT08, NotPressedyposT08); image(Tricolia07, NotPressedxposT07, NotPressedyposT07); image(Tricolia06, NotPressedxposT06, NotPressedyposT06); image(Tricolia05, NotPressedxposT05, NotPressedyposT05); image(Tricolia04, NotPressedxposT04, NotPressedyposT04); image(Tricolia03, NotPressedxposT03, NotPressedyposT03); image(Tricolia02, NotPressedxposT02, NotPressedyposT02); image(Tricolia01, NotPressedxposT01, NotPressedyposT01); } }