Hide, show SVG shape objects?

I’ve been using shapes API to add on more objects. I understand that if you set layout -> showlegend: true, you can toggle the trace(s) from the legend. Is there a way to toggle the shape objects display?

Here’s an example that I modified from here:

if I toggle ‘data 1’ off, the shape object still exists. Any suggestions? Thanks.


Is there a way to set the visible flag dynamically? Like toggling to show/hide from the legend? Thanks.