Debian Codegraph @

samples/starfield.svg

raw
1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
3 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
4
5 <!--
6
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 -->
23 <!-- ========================================================================= -->
24 <!-- A star field -->
25 <!-- -->
26 <!-- @author [email protected] -->
27 <!-- @version $Id$ -->
28 <!-- ========================================================================= -->
29
30 <svg xmlns="http://www.w3.org/2000/svg"
31 xmlns:xlink="http://www.w3.org/1999/xlink"
32 width="450" height="500" viewBox="0 0 450 500">
33 <title>Starfield</title>
34
35 <defs>
36
37 <!-- ######################################################################### -->
38 <style type="text/css">
39 <![CDATA[
40 .title {
41 font-size: 52;
42 font-style: italic;
43 font-weight: bolder
44 }
45 /* Colors */
46 #background {
47 fill: #080014
48 }
49 .fps-label, .fps-unit, .help, .title {
50 fill: #ddf
51 }
52 .button-back, .gauge-back {
53 fill: #edf
54 }
55 .button-fore, .gauge-fore {
56 fill: #769
57 }
58 .panel-back {
59 fill: #347;
60 fill-opacity:0.3
61 }
62 .star {
63 fill: white /*url(#star-fill)*/
64 }
65 .stop-c1 {
66 stop-color: white
67 }
68 .stop-c2 {
69 stop-color: #ffc
70 }
71 .stop-c3 {
72 stop-color: #aa8
73 }
74 /* <text/> default property values */
75 text {
76 font-size: 9;
77 text-anchor: middle
78 }
79 /* The property values for a <text/> preceded by another <text/> */
80 text + text {
81 text-anchor: start
82 }
83 ]]>
84 </style>
85
86 <!-- ######################################################################### -->
87 <radialGradient id="star-fill" gradientUnits="userSpaceOnUse"
88 cx="0" cy="0" fx="-5" fy="-5" r="30">
89 <stop class="stop-c1" offset="0%"/>
90 <stop class="stop-c2" offset="20%"/>
91 <stop class="stop-c3" offset="100%"/>
92 </radialGradient>
93
94 <!-- ######################################################################### -->
95 <g id="plus-button">
96 <circle class="button-back" cx="10" cy="10" r="10"/>
97 <rect class="button-fore" x="8" y="4" width="4" height="12"/>
98 <rect class="button-fore" x="4" y="8" width="12" height="4"/>
99 </g>
100
101 <!-- ######################################################################### -->
102 <g id="minus-button">
103 <circle class="button-back" cx="10" cy="10" r="10"/>
104 <rect class="button-fore" x="4" y="8" width="12" height="4"/>
105 </g>
106
107 <!-- ######################################################################### -->
108 <g id="gauge">
109 <rect class="gauge-back" x="0" y="0" width="150" height="20"/>
110 </g>
111
112
113 <!-- ######################################################################### -->
114 <script type="text/ecmascript">
115 <![CDATA[
116 //
117 // Global declarations
118 //
119 var starIncrement = 5
120 var numberOfStars = starIncrement
121 var unitCount = 1
122 var units = document.getElementById("units")
123 var stars = new Array()
124 var controlPanelMoving = false;
125 var showControlPanel = false
126 var helpText1 = "Click on the title to show the control panel"
127 var helpText2 = "Click on the title to hide the control panel"
128 var startDate;
129 var nframe;
130
131 //
132 // Decreases the number of stars. If numberOfStars is at his min value,
133 // do nothing.
134 //
135 function decreaseNumberOfStars(evt) {
136 if (numberOfStars == 0) {
137 return
138 }
139
140 for (var i = 0; i < starIncrement; i++) {
141 stars[--numberOfStars].freeze();
142 }
143 updateText("number-label", numberOfStars)
144
145 unitCount--
146 units.setAttributeNS(null, "width", unitCount * 10)
147 }
148
149 //
150 // Increases the number of stars. If numberOfStars is at his max value,
151 // do nothing.
152 //
153 function increaseNumberOfStars(evt) {
154 if (numberOfStars == starIncrement * 15) {
155 return
156 }
157
158 for (var i = 0; i < starIncrement; i++) {
159 stars[numberOfStars++].activate();
160 }
161 updateText("number-label", numberOfStars)
162
163 unitCount++
164 units.setAttributeNS(null, "width", unitCount * 10)
165
166 }
167
168 //
169 // Updates a text element content.
170 //
171 function updateText(id, lbl) {
172 var elt = document.getElementById(id)
173 var grp = elt.parentNode;
174 var newelt = elt.cloneNode(true)
175
176 if (newelt.firstChild == null) {
177 newelt.appendChild(document.createTextNode(lbl))
178 } else {
179 newelt.replaceChild(document.createTextNode(lbl), newelt.firstChild)
180 }
181 grp.replaceChild(newelt, elt)
182 }
183
184 var itv;
185
186 //
187 // Starts the animation loops.
188 //
189 function initAnimations(evt) {
190 for (var i = 0; i < starIncrement * 15; i++) {
191 stars[i] = new Star()
192 }
193
194 for (var i = 0; i < starIncrement; i++) {
195 stars[i].activate()
196 }
197 updateText("help", helpText1)
198
199 itv = setInterval("animateStars()", 10)
200
201 startDate = new Date()
202 nframe = 0
203 }
204
205 //
206 // Animates the stars.
207 //
208 function animateStars() {
209 for (var i = 0; i < starIncrement * 10; i++) {
210 stars[i].update()
211 }
212 if (nframe < 10) {
213 nframe++
214 } else {
215 var d = new Date()
216 var dt = d - startDate
217 var val = Math.round(10000 / dt)
218
219 updateText("fps1", val);
220 updateText("fps2", val);
221 // System.err.println("FPS: " + val);
222 startDate = d
223 nframe = 0
224 }
225 }
226
227 var controlPanelDy;
228 var controlPanelSgn;
229
230 //
231 // Shows/hides the control panel.
232 //
233 function animateControlPanel(evt) {
234 if (!controlPanelMoving) {
235 controlPanelMoving = true;
236 showControlPanel = !showControlPanel
237 if (showControlPanel) {
238 updateText("help", helpText2)
239 controlPanelDy = 20
240 controlPanelSgn = 1
241 setTimeout("controlPanelAnimation()", 50)
242 } else {
243 updateText("help", helpText1)
244 controlPanelDy = 0
245 controlPanelSgn = -1
246 setTimeout("controlPanelAnimation()", 50)
247 }
248 }
249 }
250
251 //
252 // Animates the control panel.
253 //
254 function controlPanelAnimation() {
255 var elt = document.getElementById("control-panel");
256
257 elt.setAttributeNS(null, "transform", "translate(0 " + ((controlPanelDy - 100) * controlPanelSgn) + ")");
258
259 if (controlPanelDy < 200) {
260 controlPanelDy += 20
261 setTimeout("controlPanelAnimation()", 50)
262 } else {
263 controlPanelMoving = false;
264 }
265 }
266
267 //
268 // The star object.
269 //
270 function Star() {
271 this.element = document.createElementNS("http://www.w3.org/2000/svg",
272 "circle")
273 //
274 // This method activates the star.
275 //
276 this.activate = function() {
277 this.isActive = true
278 this.dx = (Math.random() - 0.5) * 100 + 1
279 this.dy = (Math.random() - 0.5) * 100 + 1
280 this.dr = Math.random() / 5
281 }
282
283 //
284 // This method freezes the star.
285 //
286 this.freeze = function() {
287 this.x = 225
288 this.y = 225
289 this.r = 0.01
290
291 this.isActive = false
292 this.updateElement()
293 }
294
295 //
296 // This method updates the star size and position, if
297 // the star has been activated.
298 //
299 this.update = function() {
300 if (this.isActive) {
301 this.x += this.dx
302 this.y += this.dy
303 this.r += this.dr
304
305 this.dx += (this.dx > 0) ? 1 : -1
306 this.dy += (this.dy > 0) ? 1 : -1
307 this.dr += 0.04
308
309 if ((this.x < -20 || this.x > 470) ||
310 (this.y < -20 || this.y > 470) ||
311 this.r > 30) {
312 this.freeze()
313 this.activate()
314 return
315 }
316
317 this.updateElement()
318 }
319 }
320 this.updateElement = function() {
321 this.element.setAttributeNS(null, "transform",
322 "translate("+this.x+" "+this.y+")")
323 this.element.setAttributeNS(null, "r", this.r)
324 }
325
326 // Start as inactive...
327 this.element.setAttributeNS(null, "class", "star")
328 this.freeze()
329 var stars = document.getElementById("stars")
330 stars.insertBefore(this.element, stars.firstChild)
331 }
332 ]]>
333 </script>
334
335 </defs>
336
337 <!-- ######################################################################### -->
338 <!-- ######################################################################### -->
339 <g id="body" onload="initAnimations(evt)">
340 <rect id="background" x="0" y="0" width="100%" height="100%"/>
341
342 <g id="stars"><!-- Used to store the stars --></g>
343
344 <!-- The control panel -->
345 <g id="control-panel" style="opacity: 0.7" transform="translate(0, -100)">
346
347 <text class="help" id="help" x="50%" y="170"/> <!-- set by script -->
348
349 <rect class="panel-back" x="0" y="-190" width="100%" height="70%"/>
350
351 <!-- The title -->
352 <text class="title" x="50%" y="150" onclick="animateControlPanel(evt)">
353 Starfield
354 </text>
355
356 <!-- The controls -->
357 <use xlink:href="#minus-button" transform="translate(90 20)"
358 onclick="decreaseNumberOfStars(evt)"/>
359
360 <use xlink:href="#gauge" transform="translate(150 20)"/>
361
362 <rect id="units" class="gauge-fore" x="150" y="20" width="10" height="20"/>
363
364 <text id="number-label" x="195" y="33">5</text>
365 <text x="205" y="33">Stars at a time</text>
366
367 <use xlink:href="#plus-button" transform="translate(340 20)"
368 onclick="increaseNumberOfStars(evt)"/>
369
370 <!-- The frame rate labels -->
371 <text class="fps-label" id="fps1" x="20" y="-80"/> <!-- set by script -->
372 <text class="fps-unit" x="30" y="-80">Frames per second</text>
373
374 <rect class="panel-back" x="0" y="560" width="100%" height="70%"/>
375
376 <text class="fps-label" id="fps2" x="20" y="583">0</text>
377 <text class="fps-unit" x="30" y="583">Frames per second</text>
378 </g>
379 </g>
380 <!-- ============================================================= -->
381 <!-- Batik sample mark -->
382 <!-- ============================================================= -->
383 <use xlink:href="batikLogo.svg#Batik_Tag_Box" />
384 </svg>