clock.tex /size: 4027 b    last modification: 2020-07-01 14:35
1
%D This example dates from late 2001, and ws probaby made for some \TEX\ related
2
%D meeting. It's a relative simple example that uses a few function from a general
3
%D \JAVASCRIPT\ preamble. In fact forcing the \type {FieldStack} code into the
4
%D is the only adaptation from \MKII\ to \MKIV\ (because we load on demand and here
5
%D the code is ude sindirectly).
6
%D
7
%D Nowadays we could layers instead which is probably a bit more ligthweight than
8
%D widgets. Also, we should actually synchronize the time but on the other hand, but
9
%D on the other hand, now it's a sort of stopwatch.
10 11
% \nopdfcompression
12 13
\starttext
14 15
\useJSscripts
[
fld
]
16 17
\useJSpreamble
[
FieldStack
]
18 19
\definepapersize
20
[
clock
]
21
[
width
=
2
.
5
cm
,
22
height
=
2
.
5
cm
]
23 24
\setuppapersize
25
[
clock
]
[
clock
]
26 27
\setuplayout
28
[
header
=
0
pt
,
29
footer
=
0
pt
,
30
backspace
=
.
2
5
cm
,
31
topspace
=
.
2
5
cm
,
32
width
=
middle
,
33
height
=
middle
]
34 35
\startJSpreamble
stepper
used
now
36 37
var
state
=
0
;
38
var
more
=
0
;
39
var
step
=
0
;
40 41
function
step
_
clock
(
)
{
42
try
{
43
if
(
more
=
=
6
0
)
{
44
more
=
0
;
45
Walk
_
Field
(
"
more
"
)
;
46
}
else
{
47
}
48
more
+
=
1
;
49
Walk
_
Field
(
"
clock
"
)
;
50
this
.
dirty
=
false
;
51
}
catch
(
e
)
{
52
}
53
}
54 55
function
start
_
clock
(
)
{
56
try
{
57
if
(
state
=
=
0
)
{
58
step
=
app
.
setInterval
(
"
step
_
clock
(
)
"
,
1
0
0
0
)
;
59
step
.
count
=
0
;
60
state
=
1
;
61
}
else
if
(
state
=
=
1
)
{
62
app
.
clearInterval
(
step
)
;
63
state
=
2
;
64
}
else
if
(
state
=
=
2
)
{
65
app
.
clearInterval
(
step
)
;
66
Reset
_
Fields
(
"
more
"
)
;
67
Reset
_
Fields
(
"
clock
"
)
;
68
Set
_
Field
(
"
more
"
,
"
1
"
)
;
69
Set
_
Field
(
"
clock
"
,
"
1
"
)
;
70
more
=
0
;
71
state
=
0
;
72
}
73
}
catch
(
e
)
{
74
}
75
}
76 77
function
stop
_
clock
(
)
{
78
try
{
79
app
.
clearInterval
(
step
)
;
80
}
catch
(
e
)
{
81
}
82
}
83 84
\stopJSpreamble
85 86
\definereference
[
StopClock
]
[
JS
(
stop
_
clock
)
]
87
\definereference
[
StartClock
]
[
JS
(
start
_
clock
)
]
88 89
\setupinteraction
90
[
state
=
start
,
91
closeaction
=
{
StopClock
,
ForgetChanges
}
,
92
closepageaction
=
{
StopClock
}
]
93 94
\startreusableMPgraphic
{
common
}
95
drawoptions
(
withpen
pencircle
scaled
1
mm
withcolor
.4
white
)
;
96
fill
fullsquare
scaled
2.5
cm
;
97
drawoptions
(
withpen
pencircle
scaled
1
mm
withcolor
.6
green
)
;
98
draw
fullsquare
scaled
2.5
cm
;
99
drawoptions
(
withpen
pencircle
scaled
1
mm
withcolor
.6
red
)
;
100
draw
fullcircle
scaled
2
cm
;
101
drawoptions
(
withpen
pencircle
scaled
2
mm
withcolor
.6
yellow
)
;
102
for
i
=
1
upto
12
:
103
draw
(
0
,
1
cm
)
rotated
(
(
i
-1
)
*
(
360
/
12
)
)
;
104
endfor
;
105
\stopreusableMPgraphic
106 107
\startuseMPgraphic
{
clock
}
108
numeric
stp
;
stp
:
=
(
\MPvar
{
n
}
-1
)
*
(
360
/
60
)
;
109
pair
p
;
p
:
=
(
0
,
\MPvar
{
l
}
-.5
mm
)
;
110
drawoptions
(
withpen
pencircle
scaled
1
mm
withcolor
.6
\MPvar
{
c
}
)
;
111
draw
(
origin
--
p
)
rotated
-
stp
;
112
draw
(
p
shifted
(
-2
mm
,
-2.5
mm
)
-
-
p
-
-
p
shifted
(
2
mm
,
-2.5
mm
)
)
rotated
-
stp
;
113
setbounds
currentpicture
to
fullsquare
scaled
2
cm
;
114
drawoptions
(
withpen
pencircle
scaled
2
mm
withcolor
.6
white
)
;
115
draw
origin
;
116
\stopuseMPgraphic
117 118
\defineoverlay
[
common
]
[
\reuseMPgraphic
{
common
}
]
119
\defineoverlay
[
start
]
[
\overlaybutton
{
StartClock
}
]
120 121
\setupbackgrounds
122
[
page
]
123
[
background
=
{
common
,
start
}
]
124 125
\let
\clocklist
\empty
126
\let
\morelist
\empty
127 128
\dorecurse
{
6
0
}
{
129
\appendtocommalist
{
step
:
#
1
}
\clocklist
130
\definesymbol
131
[
step
:
#
1
]
132
[
\useMPgraphic
{
clock
}
{
n=#1,l=1cm,c=blue
}
]
133
\appendtocommalist
{
more
:
#
1
}
\morelist
134
\definesymbol
135
[
more
:
#
1
]
136
[
\useMPgraphic
{
clock
}
{
n=#1,l=.75cm,c=green
}
]
137
}
138 139
\definefieldstack
140
[
clock
]
141
[
\clocklist
]
142
[
width
=
2
cm
,
height
=
2
cm
,
offset
=
overlay
,
frame
=
off
]
143 144
\definefieldstack
145
[
more
]
146
[
\morelist
]
147
[
width
=
2
cm
,
height
=
2
cm
,
offset
=
overlay
,
frame
=
off
]
148 149
\starttext
150 151
\startoverlay
152
{
\fieldstack
[
more
]
}
153
{
\fieldstack
[
clock
]
}
154
\stopoverlay
155 156
\stoptext
157