Skip to main content
3-Newcomer
July 30, 2024
Question

Temporarily change button appearance for 2 seconds using Javascript

  • July 30, 2024
  • 1 reply
  • 1767 views

I would like to temporarily change a button's appearance for 2 seconds using Javascript. I'm planning to use the pre-built properties of "Image when Pressed" and Image when not Pressed" in Studio, if it makes sense. I need guidance on the javascript that could perform the temporary change. This provides user feedback when they press the button. 

I have attached two different icons that would change color/appearance to indicate to the user they have pressed the button. 

I am using Vuforia Studio 9.21.3.0

1 reply

21-Topaz I
August 7, 2024

Hi @NJ_10402268 ,

I think there could be different approaches to   implement the requested functionality . Here I want to provide one possible option:

 

  • supposing you want to use 3DImage widget on mobile Vuforia Studio Project. So we use there the click event  
  •  where the function will have as arguments the 3DImage widget name, the delay -2000ms - 2 seconds and the name of the picture what is inactive. the pictures file should be uploaded to the Project Upload folder
    app.testPressedDelay('3DImage-1',2000,'icon_freeze_inactive.png')​​

    The code for the call will be added to the UI click box of the 3DImage widget - here it is the 3DImage-1

 

  • 2024-08-07_16-39-31.jpg

 

  • It is important to block the action of the function during of the execution - 2 second . this could be done by global variable:

 

$scope.app.blocked=false​

 

 where then in the function we will check something like 

 

... 
... 
if($scope.app.blocked) return; 
$scope.app.blocked=true 
... 
...

 

 

  • at the end the complete code of that option  could be something like this:
    //call from click event of the 3DImage
    //app.testPressedDelay('3DImage-1',2000,'icon_freeze_inactive.png')
    //=========================================
    //blocks the event when it is called for the delay
    $scope.app.blocked=false
    //=========================================
    $scope.app.testPressedDelay= (wdgName,delay,pressedPic) => {
     
     if($scope.app.blocked) return;
     $scope.app.blocked=true
     
     let currentPicture = $scope.getWidgetProp(wdgName,'src')
     $scope.setWidgetProp(wdgName,'src',"app/resources/Uploaded/"+pressedPic)
     $scope.$applyAsync(); 
     
     setTimeout( (wdgName,pic)=> { try{
     
     $scope.setWidgetProp(wdgName,'src',pic)
     $scope.$applyAsync()
     $scope.app.blocked=false
     
     
     console.info("delay ="+delay+" > wdgName: "+wdgName+" to "+pic)
     } catch(e){ console.error("outer",e);} },delay, wdgName,currentPicture) 
     
     
    }​

I attached also a simple demo project to this post

 

3-Newcomer
August 7, 2024

Hi Roland,

 

Thank you for the example and guidance. I appreciate it. However, I have tried to take your js and apply it to a 2d image or a toggle button, but the desired effects do not work. It doesn't appear that your code is specific to this example. It should translate to other click events or pressed events, right? I have attached my edit of your project. 

3-Newcomer
August 7, 2024

Will this code conflict with a toggle button already having binding to navigate within a model? I currently have rewind, play, and forward buttons in my experience and they all have bindings to the model.

a10e3e28-7924-4f73-aea9-74b424d20aef.png

a4982034-55d3-4259-b742-d2b775ffa97d.png