Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Scaling While Keeping Original Coordinates

edited August 2014 in Questions Posts: 145

I am trying to scale a sprite while keeping its original location. However, the sprite moves whenever the image is scaled. How can I scale this and maintain the same original position?

For example, in this program I want the heart to always stay in the center of the rectangles even when scaled:

 function setup()
  end

      function draw()
         -- This sets a dark background color 
        background(40, 40, 50)

         -- This sets the line thickness
           strokeWidth(5)

         -- Do your drawing here

            scalefactor=1
            scale(scalefactor)

            fill(255,255,255,40)

            rect(WIDTH/12.4271,HEIGHT/4*1.7,WIDTH/3.072,HEIGHT/13.653333)

            fill(255, 255, 255, 255)



            sprite("Planet Cute:Heart",WIDTH/4.1513,HEIGHT/2.174)    





            fill(255,255,255,50)

            rect(WIDTH/1.695,HEIGHT/4*1.7,WIDTH/3.072,HEIGHT/13.653333)

            fill(255, 255, 255, 255)



            sprite("Planet Cute:Heart",WIDTH/1.3241,HEIGHT/2.1694)

 end

Comments

  • edited August 2014 Posts: 145

    I have solved the problem.

    Here is the solution:

    function setup()
        --Creates a global variable called Radius
        parameter.number("Scalefactor", 1.0, 10.0, 1.0)
    end
    
    function draw()
        background(128)
    
        scale(Scalefactor)
        sprite("Cargo Bot:Condition Green",(WIDTH/2)/Scalefactor,(HEIGHT/2)/Scalefactor)
    end
    
  • Posts: 428

    An alternative is to translate and then scale:

    translate(WIDTH/2,HEIGHT/2)
    scale(Scalefactor)
    sprite("Cargo Bot:Condition Green",0,0)
    

    (You might need to experiment with different spriteModes to get the right part of the sprite fixed in place.)

Sign In or Register to comment.