interaction-javascript.tex /size: 3766 b    last modification: 2023-12-21 09:43
1% language=us
2
3\environment interaction-style
4
5\startcomponent interaction-javascript
6
7\startchapter[title={JavaScript}]
8
9Annotations can be controlled with \JAVASCRIPT\ but it really depends on the
10viewer if it works out well. Using these scripts is a multi||step process where
11common functions and data structures can be shared and collected in preambles:
12
13\starttyping
14\startJSpreamble {name}
15  MyCounter = 0 ;
16\stopJSpreamble
17\stoptyping
18
19The more action oriented scripts are defined as:
20
21\starttyping
22\startJScode {increment}
23  MyCounter = MyCounter + 1 ; // or: ++MyCounter ;
24\stopJScode
25\stoptyping
26
27This script is executed with:
28
29\starttyping
30\goto {advance by one} [JS(increment)]
31\stoptyping
32
33Nicer is to define a function:
34
35\starttyping
36\startJSpreamble {helpers} used now
37    function Increment(n) {
38        MyCounter = MyCounter + n ;
39    }
40\stopJSpreamble
41\stoptyping
42
43and then say:
44
45\starttyping
46\goto {advance by one} [JS(Increment{5})]
47\stoptyping
48
49The distribution contains a collection of scripts that can be preloaded and used
50when needed. You can recognize the files by the \type {java-imp-} prefix. To
51prevent all preambles ending up in the \PDF\ file, we can say:
52
53\starttyping
54\startJSpreamble {something} used later
55\stopJSpreamble
56\stoptyping
57
58We already saw that one can also say \type {used now} and there's also a way
59to filter specific preambles on usage:
60
61\starttyping
62\startJScode {mything} uses {something}
63\stopJScode
64\stoptyping
65
66One should be aware of the fact that there is no decent way to check if every
67script is all right! Even worse, the \JAVASCRIPT\ interpreter currently used in
68the \ACROBAT\ tools is not reentrant, and breaks down on typos
69
70The full repertoire of commands is:
71
72\showsetup{startJScode}
73
74\showsetup{startJSpreamble}
75
76\showsetup{addtoJSpreamble}
77
78\showsetup{setJSpreamble}
79
80As we're into \LUA\ and because \LUA\ is so lightweight I've wondered several
81times now if it would make sense to embed \LUA\ in \PDF\ viewers. After all,
82annotations are an extension mechanism. In the early days of \PDF\ this was
83actually quite doable because \ACROBAT\ reader (and exchange) had a plugin model.
84However, the more functionality ended up in the program, the least interesting
85(and popular) the plugins mechanism became. Some open source viewers have an
86\API\ so in principle adding the lightweight \LUA\ interpreter (of course with
87\LPEG, and quite probably without file \IO) is possible. It has been discussed at
88a recent \CONTEXT\ meeting, so who knows \unknown For now we're stuck with
89\JAVASCRIPT.
90
91An example of \JAVASCRIPT\ usage is the following, where we load a video and add
92some controls. Beware that this kind of functionality is very viewer dependent
93and therefore also very unstable over time. Even worse, if you look at the loaded
94\JAVASCRIPT\ file you will notice a dependency on soon obsolete (in \ACROBAT\ at
95least) shockwave support. First we load a library that will predefine a video
96graphic: and then create an instance:
97
98\starttyping
99\useJSscripts[vplayer]
100
101\setupinteraction
102  [state=start]
103
104\externalfigure
105  [shockwave]
106  [frame=on,
107   width=480pt,
108   height=270pt,
109   file=test.mp4,
110   label=foo]
111\stoptyping
112
113The controls are defined with:
114
115\starttyping
116\goto{START} [JS(StartShockwave{foo})]
117\goto{REWIND}[JS(RewindShockwave{foo})]
118\goto{PAUSE} [JS(PauseShockwave{foo})]
119\goto{STOP}  [JS(StopShockwave{foo})]
120\stoptyping
121
122or, as we have some defined reference shortcuts:
123
124\starttyping
125\goto{START} [StartShockwave{foo}]
126\goto{REWIND}[RewindShockwave{foo}]
127\goto{PAUSE} [PauseShockwave{foo}]
128\goto{STOP}  [StopShockwave{foo}]
129\stoptyping
130
131It's actually not that hard to add all kind of functionality if only we could be
132sure of stable support and continuity.
133
134\stopchapter
135
136\stopcomponent
137