Get opacity through JS

Problem: Hi, I want to identify all objects on a given slide that have an opacity that is not zero. Can this be done through JS? I know I can set opacity through JS but I fail to get the opacity of an element through JS. I tried

prez.object(‘Shape_3’).node.style.opacity

but it did not work.

Is there a way to get the opacity ?

Thanks, Rolf

ActivePresenter version: 10

OS: Win 11

Notes:

This worked for me for your other project.

prez.object("Circle").node.style.opacity

Perhaps retype your command above. Looks like it has smart quotes. That could be breaking it.

2 Likes

Thanks Greg,

hmmm, looks like that is not the problem. It works in the other project once I have zoomed the circle in and out. But if I have a circle and want to get its opacity, it’s an empty string. Once I set opacity through JS I then get the opacity.

In theory, I could set opacity on load of slides, but that seems kind of messy.

Best, Rolf

@Rolf -

I see… you need opacity right away on load prior to any interaction?

Is the value itself important or do you just need to know which ones are visible? So zero or one?

I was wondering if there was something we could do with adding a show/hide when you fade out, then hide and show then fade in. Perhaps then you could make use of another tactic.

prez.object("Circle").visible();

This would give you a true or false result but at least you might be able to work with that.

Not sure if that would be a working approach for you or not - just trying to brainstorm a bit.

Hi,

This is set using the CSS filter property, so you can’t get it from the opacity property.
Please try using the following script instead, it should work better:

getFilterOpacity(prez.object('Shape_3').node);

// return value from 0 to 1
function getFilterOpacity(el) {
    var filter = getComputedStyle(el).filter;
    var match = filter.match(/opacity\(([^)]+)\)/);

    if (!match) return 1; // no filter opacity applied

    var value = match[1];

    if (value.includes('%')) {
        return parseFloat(value) / 100;
    }

    return parseFloat(value);
}
1 Like

Thanks Greg, no, that doesn’t seem to work.

Thanks, Hang, as always: excellent

Yeah - Hang’s solution is not one that I would have come up with on my own either. LOL

Your input is always much appreciated, though!!! R