Map the width of canvas to control alpha values with mouseX

I’m trying to map the width of my canvas to an alpha range of 0 to 100, then I’d like the mouseX position to control the alpha.

Could anyone help me out with this code:

float intalpha;
float intalphaMapped = ofMap(mouseX, 0, 0, ofGetWidth(), 100);  // i've mapped the width of the canvas to only be zero to 100
1 Like

Hi there!

To use ofMap, you need to use it like this:

ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax)

You are switching some values. So, in your case:

float intalphaMapped = ofMap(mouseX, 0, ofGetWidth(), 0, 100);

Edit: You can also do something like:

float intalphaMapped = (float)mouseX/ofGetWidth() * 100.f;

And remember, in OF, the alpha range is 0 to 255.

thx so much!