2

I have this very simple LaTeX file that uses the media9 package to insert 3D objects. I want to animate parts of the 3D object using Javascript, preferably on a button click.

My problem is that when I call my Javascript function through a TimeEventHandler or a MouseClickEvent, the animation works well. But when I create a \mediabutton to call the very same function, I get an error.

The following is a part of my LaTeX code (where animate() is a function defined in animation.js):

\includemedia[
    label=model,
    activate=pageopen,
    add3Djscript=animation.js
]{}{model.u3d}

\mediabutton[
    jsaction=model:{annotRM[`model'].context3D.animate();}
]{\fbox{Play Animation}}

Here is the simplest version of my JS code that still fails:

cube=scene.meshes.getByName("Cube");

function animate(){
    cube.visible=false;
}

While debugging, I found that I get the following error in JS and I have no idea why.

SyntaxError: illegal character
1:Field:Mouse Up

I even tried using media9's sample code and files, and I get the same error even though they should work perfectly. (The error only happens with the sample files that use \mediabutton with Javascript; the other samples work properly.)

I've been trying to figure out whether this was a problem with LaTeX or JS, and seeing that the JS code works perfectly as long as I don't use the button, I figured I'd try to find any solutions here.

No one else seems to have any problems attaching JS functions to media buttons. Any insight would be very much appreciated.

If it matters, I'm using MikTeX pdflatex.

  • 2
    animation.js which is part of the media9 package doesn't define a function animate(). – AlexG Aug 18 '16 at 12:08
  • Sorry if it was confusing. The animation.js file I mentioned here is my own, with an animate() function I defined. (The actual filename is different but I changed it here to be more understandable, but I guess that made you think I was referring to media9's animation.js) – yushi Aug 18 '16 at 12:58
  • Edited my question to include my JS code. – yushi Aug 18 '16 at 23:43
  • The leading backtick of `model' is the character that JS complains about. Replace it with a single quote. – AlexG Aug 19 '16 at 7:19
  • @AlexG Replacing it with a single quote worked. Thanks! When I type in TeXworks, the single quote gets turned to backticks automatically so I assumed that was correct. – yushi Aug 19 '16 at 8:30
2

In order to play a keyframe animation embedded in a U3D file, animation.js defines a TimeEventHandler object "myTimer" that is added to the 3D runtime object. To pause the animation, this TimeEventHandler must be removed from the runtime object again. This can be implemented using \mediabutton as follows:

\mediabutton[
  jsaction=model:{
    cntxt3d=annotRM['model'].context3D;
    cntxt3d.runtime.removeEventHandler(cntxt3d.myTimer);
  }
]{\fbox{Pause Animation}}
\mediabutton[
  jsaction=model:{
    cntxt3d=annotRM['model'].context3D;
    cntxt3d.runtime.addEventHandler(cntxt3d.myTimer);
  }
]{\fbox{Play Animation}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.