Flexing Stripes
in
Programming Questions
•
1 year ago
Hi everyone,
I'm posting a mockup of a problem I am trying to solve in a larger project. Basically, I need the width of a stripe that's being hovered over (in a canvas of vertical stripes) to scale up in width. That part is easy. The hard part is scaling all the other stripes down and offsetting them so that all still fill the space but flex their size to accommodate the one that is now fatter...
In this mockup, I'm scaling one stripe up and I have a strategy in place for scaling all other stripes down by a factor, but I'm stuck on trying to figure out how to offset the positions.
Can you recommend a better way to approach this, or a way to finish what I've begun?
Thanks so much.. Michael
- ArrayList<Stripe> stripes;
- public int numStripes = 7;
- void setup(){
- size(700,400);
- smooth();
- rectMode(CENTER);
- colorMode(HSB, 360,100,100);
- stripes = new ArrayList<Stripe>();
- for(int i=0; i<numStripes; i++){
- stripes.add( new Stripe( (width-50) - i*100 ) );
- }
- }
- void draw(){
- background(300);
- for(int i=0; i<stripes.size(); i++){
- Stripe s = stripes.get(i);
- s.checkHover();
- if(s.hover){
- for(int j=0; j<stripes.size(); j++){
- if(j!=i){
- //pass in xpos so in the class we can
- //determine if it's position should be offset
- //to the left or right.
- stripes.get(j).scaleYourself(s.xpos);
- }
- }
- }
- s.display();
- }
- }
- //--------------------------
- public class Stripe{
- public float xpos;
- private float w, oldw, targetw;
- public float h;
- public boolean hover;
- private float scaleFactor;
- private float xoff;
- private float rh;
- public Stripe(float xpos){
- oldw = 100;
- targetw = oldw;
- rh = random(200,300);
- this.xpos = xpos;
- h = random(360);
- scaleFactor = 1.5;
- }
- public void display(){
- noStroke();
- fill(h, 90,90);
- rect(xpos+xoff, height/2, w, rh);
- stroke(0);
- line(xpos+xoff, 0, xpos+xoff,height);
- }
- public void checkHover(){
- if( abs(dist(mouseX,0, xpos, 0)) < 5 ){
- scaleFactor = 1.5;
- hover = true;
- } else {
- scaleFactor = 1;
- hover = false;
- }
- targetw = oldw*scaleFactor;
- w += (targetw - w) * 0.1;
- }
- public void scaleYourself(float comparex){
- int direction;
- if(this.xpos < comparex){
- direction = -1;
- } else {
- direction = 1;
- }
- xoff = 0;//
- scaleFactor = 1.0 - (numStripes / (oldw*1.5 - oldw));
- targetw = oldw*scaleFactor;
- w += (targetw - w) * 0.1;
- }
- }
1