Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/boards/anim/animation.js
blob: 354b48ceedcc3ad5748ab40255603eadf11645ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

 // Copyright (C) 2005 Yves Combe
 //
 //   This program is free software; you can redistribute it and/or modify
 //   it under the terms of the GNU General Public License as published by
 //   the Free Software Foundation; either version 3 of the License, or
 //   (at your option) any later version.
 //
 //   This program is distributed in the hope that it will be useful,
 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //   GNU General Public License for more details.
 //
 //   You should have received a copy of the GNU General Public License
 //   along with this program; if not, see <http://www.gnu.org/Licenses/>.
 //
 // Utility file for anim.py to export gcompris anim in Mozilla SVG

   var root;
   var frames_total;
   var frames_played;
   var AnimItemlist;
   var anim_running;
   var step = 0;

   // <script/> and <defs/>
   var base_stack=3;
   var frame_delay = 250;
   var fps = 4;


   var time_base;
   var next;

function init(){
  root = document.getElementById("svgroot");
  rate_text = document.getElementById("rate_text");
  speed_text = document.getElementById("speed_text");

  var elements;
  AnimItemlist = new Array (); // Array of array !

  var AnimItem ;
  var frames_info;
  var j;
  var k;
  var gc_frame, frame_attributs, gc_attr;
  var i = 0;

  while ( base_stack < root.childNodes.length ){
    // this loop keeps node <script> and the <defs/> elements.
    if  ((root.childNodes[i].nodeName != "rect") && (root.childNodes[i].nodeName != "ellipse") && (root.childNodes[i].nodeName != "use") && (root.childNodes[i].nodeName != "line" && (root.childNodes[i].nodeName != "text") )) {
       if (root.childNodes[i].nodeName == "gcompris:frames_total")
          frames_total = root.childNodes[i].getAttribute('value');
       if ((root.childNodes[i].nodeName != "script") && (root.childNodes[i].nodeName != "foreignObject") && (root.childNodes[i].nodeName != "defs")) {
          root.removeChild(root.childNodes[i]);
          }
          else i++;
       }
    else {
       AnimItem = new Array();

       element = root.childNodes[i];

       AnimItem.push(element);
       root.removeChild(element);

       frames_info = new Array();
       AnimItem.push(frames_info);

       for (j = 0; j < element.childNodes.length; j++){
          if ( element.childNodes[j].nodeType != 1) {
                 element.removeChild(element.childNodes[j]);
                 j--;
                 continue ;
                 }
          frame_attributs = new Array();
          gc_frame =  element.childNodes[j];
          for ( k=0;  k < gc_frame.attributes.length; k++){
	     gc_attr = new Array();
             gc_attr.push(gc_frame.attributes[k].nodeName);
	     gc_attr.push(gc_frame.attributes[k].nodeValue);
             frame_attributs.push(gc_attr);
             frame_attributs[gc_frame.attributes[k].nodeName]= frame_attributs[frame_attributs.length -1];
	     }
          //if (gc_frame.hasChildNodes){
	    // text part of text element
            // supposed there is only one node, type 3 (text)
            //frame_attributs['text'] = gc_frame.childNodes[0];
            //}
          frames_info.push(frame_attributs);

          frames_info['frame_' + gc_frame.getAttribute('time')] = frames_info[frames_info.length -1];

          }

    AnimItem.push(frames_info);
    AnimItemlist.push(AnimItem);
  }
  }


  //for (k = 0; k < frames_total; k++){
  //  apply_frame(k);
  //  }
  //start_animation();
}

function apply_frame(frame_no){
   var i, j, k, gc_frame;

   for (i=0; i < AnimItemlist.length; i++) {
     // alert(AnimItemlist[i][0].nodeName + ' : ' + AnimItemlist.length );
     if (AnimItemlist[i][1]['frame_' + frame_no]){
         if (AnimItemlist[i][1]['frame_' + frame_no]['delete']){
	    root.removeChild(AnimItemlist[i][0]);
            continue;
            }
         if ( AnimItemlist[i][1]['frame_' + frame_no]['text'] ){
            AnimItemlist[i][0].textContent=AnimItemlist[i][1]['frame_' + frame_no]['text'][1];
            }
         for (k=0; k< AnimItemlist[i][1]['frame_' + frame_no].length; k++){
         attr = AnimItemlist[i][1]['frame_' + frame_no][k][0];
         if  ( attr != 'time' && attr != 'z' && attr != 'create' )
            AnimItemlist[i][0].setAttribute(
                     attr, AnimItemlist[i][1]['frame_' + frame_no][k][1]);
         }
	 // create is at end because of viewBox/use need that.
         if (AnimItemlist[i][1]['frame_' + frame_no]['create']){
	    root.appendChild(AnimItemlist[i][0]);
            }
         // z is after create, of course...
         if (AnimItemlist[i][1]['frame_' + frame_no]['z']){
	    set_z(AnimItemlist[i][0], AnimItemlist[i][1]['frame_' + frame_no]['z'][1] );
            }
       }

    }
}

function apply_attr(node, name, value){
    // alert(node.nodeName + " " + name + " " + value );
}

function speed_down() {

   if (fps > 1) {
     fps--;
     frame_delay = Math.floor ( 1000 / fps);
     speed_text.value = fps + ' fps';
     }
}

function speed_up() {

   if (fps < 25) {
     fps++;
     frame_delay = Math.floor ( 1000 / fps);
     speed_text.value =  fps + ' fps';
     }
}

function start_animation()
{
  var d = new Date();
  time_base = d.getTime();
  next = time_base;
  rate_text.value = '';
  frames_played = frames_total - step;
  if (! anim_running ) animate();
}

function stop_animation()
{
  if (!anim_running) return;
  clearTimeout(anim_running);
  anim_running = 0;

}

function animate()
{
  var id = root.suspendRedraw(1000);

  if (step == frames_total) {
     reinit_animation();
     step = 0;
     frames_played = frames_total;
     }
  else apply_frame(step++);
  root.unsuspendRedraw(id);

  var time = new Date();
  leak = time.getTime() - next;
  next = next + frame_delay;
  anim_running = setTimeout("animate()",frame_delay - leak);
}

function reinit_animation() {
  var k;
  while ( base_stack < root.childNodes.length){
     root.removeChild(root.childNodes[base_stack]);
     }
   var e = new Date();
   var time_actuel=e.getTime();

   var interval = (time_actuel - time_base)/frames_played;

   var rate = Math.round(1000/interval);

   rate_text.value = rate + ' fps'

  while ( base_stack < root.childNodes.length){
     root.removeChild(root.childNodes[base_stack]);
     }

   var f = new Date();
   time_base = f.getTime();
}


function set_z(node, index) {
        index = parseInt(index) + base_stack;

        if (node.parentNode.childNodes[index] == node) return;

	var parent = node.parentNode;
	parent.removeChild(node);

        if (index < base_stack) index = base_stack;
        if (index >= parent.childNodes.length) {
           parent.appendChild(node);
           return;
        }

	parent.insertBefore(node,parent.childNodes[index]);
}