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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
---
title: Exemplo
slug: Web/SVG/Namespaces_Crash_Course/Example
tags:
- SVG
- XML
translation_of: Web/SVG/Namespaces_Crash_Course/Example
original_slug: Web/SVG/Namespaces_Crash_Course/Exemplo
---
<p>In this example, we use <a href="/en-US/docs/XHTML">XHTML</a>, <a href="/pt-PT/docs/Web/SVG">SVG</a>, <a href="/pt-PT/docs/Web/JavaScript">JavaScript</a>, and the <a href="/pt-PT/docs/DOM/DOM_Reference">DOM</a> to animate a swarm of "motes". These motes are governed by two simple principles. First, each mote tries to move towards the mouse cursor, and second each mote tries to move away from the average mote position. Combined, we get this very natural-looking behavior.</p>
<p>This is done completely in W3C Standards–XHTML, SVG, and JavaScript–no Flash or any vendor-specific extensions. This example should work in Firefox 1.5 and above.</p>
<p><a class="internal button liveSample" href="http://developer.mozilla.org/samples/svg/swarm-of-motes.xhtml">Ver o exemplo</a></p>
<pre class="brush:xml"><?xml version='1.0'?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:svg="http://www.w3.org/2000/svg">
<head>
<title>A swarm of motes</title>
<style type='text/css'>
<![CDATA[
label, input
{
width: 150px;
display: block;
float: left;
margin-bottom: 10px;
}
label
{
text-align: right;
width: 75px;
padding-right: 20px;
}
br
{
clear: left;
}
]]>
</style>
</head>
<body onload='update()'>
<svg:svg id='display' width='400' height='300'>
<svg:circle id='cursor' cx='200'
cy='150' r='7' fill='#0000ff' fill-opacity='0.5'/>
</svg:svg>
<p>A swarm of motes, governed by two simple principles.
First, each mote tries to move towards the cursor, and
second each mote tries to move away from the average
mote position. Combined, we get this very natural
looking behavior.
</p>
<p>
This is done completely in W3C Standards–XHTML,
SVG and JavaScript–no flash or any vendor specific
extensions. Currently, this will work in Mozilla Firefox
version 1.5 and above.
</p>
<div>
(C) 2006 <a id='emailme' href='#'>Nick Johnson</a>
<script type='text/javascript'>
<![CDATA[
// foil spam bots
var email = '@riovia.net';
email ='nick' + email;
document.getElementById('emailme').href = 'mailto:'+email;
]]>
</script>
This software is free for you to use in any way whatsoever,
and comes with no warranty at all.
</div>
<form action="" onsubmit="return false;">
<p>
<label>Number of motes:</label>
<input id='num_motes' value='5'/>
<br/>
<label>Max. Velocity:</label>
<input id='max_velocity' value='15'/>
<br/>
<label>Attraction to cursor:</label>
<input id='attract_cursor' value='6'/>
<br/>
<label>Repulsion from peers:</label>
<input id='repel_peer' value='5'/>
<br/>
</p>
</form>
<script type='text/javascript'>
<![CDATA[
// Array of motes
var motes;
// Get the display element.
function Display()
{
return document.getElementById('display');
}
// Determine dimensions of the display element.
// Return this as a 2-tuple (x,y) in an array
function Dimensions()
{
// Our Rendering Element
var display = Display();
var width = parseInt( display.getAttributeNS(null,'width') );
var height = parseInt( display.getAttributeNS(null,'height') );
return [width,height];
}
// This is called by mouse move events
var mouse_x = 200, mouse_y = 150;
function OnMouseMove(evt)
{
mouse_x = evt.clientX;
mouse_y = evt.clientY;
var widget = document.getElementById('cursor');
widget.setAttributeNS(null,'cx',mouse_x);
widget.setAttributeNS(null,'cy',mouse_y);
}
document.onmousemove = OnMouseMove;
// Determine (x,y) of the cursor
function Cursor()
{
return [mouse_x, mouse_y];
}
// Determine average (x,y) of the swarm
function AverageMotePosition()
{
if( !motes )
return [0,0];
if( motes.length == 0 )
return [0,0];
var i;
var sum_x=0, sum_y=0;
for(i=0; i<motes.length; i++)
{
sum_x += motes[i].x;
sum_y += motes[i].y;
}
return [sum_x/motes.length, sum_y/motes.length];
}
// A nicer, integer random
function Rand(modulo)
{
return Math.round( Math.random() * (modulo-1));
}
// Class Mote
function Mote()
{
// Dimensions of drawing area.
var dims = Dimensions();
var width = dims[0], height = dims[1];
// Choose a random coordinate to start at.
this.x = Rand( width );
this.y = Rand( height );
// Nil initial velocity.
this.vx = this.vy = 0;
// A visual element---initially none
this.elt = null;
}
// Turn this into a class.
new Mote();
// Mote::applyForce() -- Adjust velocity
// towards the given position.
// Warning: Pseudo-physics -- not really
// governed by any /real/ physical principles.
Mote.prototype.applyForce = function(pos, mag)
{
if( pos[0] > this.x )
this.vx += mag;
else if( pos[0] < this.x )
this.vx -= mag;
if( pos[1] > this.y )
this.vy += mag;
else if( pos[1] < this.y )
this.vy -= mag;
}
// Mote::capVelocity() -- Apply an upper limit
// on mote velocity.
Mote.prototype.capVelocity = function()
{
var max = parseInt( document.getElementById('max_velocity').value );
if( max < this.vx )
this.vx = max;
else if( -max > this.vx )
this.vx = -max;
if( max < this.vy )
this.vy = max;
else if( -max > this.vy )
this.vy = -max;
}
// Mote::capPosition() -- Apply an upper/lower limit
// on mote position.
Mote.prototype.capPosition = function()
{
var dims = Dimensions();
if( this.x < 0 )
this.x = 0;
else if( this.x >= dims[0] )
this.x = dims[0]-1;
if( this.y < 0 )
this.y = 0;
else if( this.y >= dims[1] )
this.y = dims[1]-1;
}
// Mote::move() -- move a mote, update the screen.
Mote.prototype.move = function()
{
// Apply attraction to cursor.
var attract = parseInt( document.getElementById('attract_cursor').value );
var cursor = Cursor();
this.applyForce(cursor, attract);
// Apply repulsion from average mote position.
var repel = parseInt( document.getElementById('repel_peer').value );
var average = AverageMotePosition();
this.applyForce(average, -repel);
// Add some randomness to the velocity.
this.vx += Rand(3)-1;
this.vy += Rand(3)-1;
// Put an upper limit on velocity.
this.capVelocity();
// Apply velocity.
var old_x = this.x, old_y = this.y;
this.x += this.vx;
this.y += this.vy;
this.capPosition();
// Draw it.
if( this.elt === null )
{
var svg = 'http://www.w3.org/2000/svg';
this.elt = document.createElementNS(svg,'line');
this.elt.setAttributeNS(null,'stroke','green');
this.elt.setAttributeNS(null,'stroke-width','3');
this.elt.setAttributeNS(null,'stroke-opacity','0.5');
Display().appendChild( this.elt );
}
this.elt.setAttributeNS(null,'x1',old_x);
this.elt.setAttributeNS(null,'y1',old_y);
this.elt.setAttributeNS(null,'x2',this.x);
this.elt.setAttributeNS(null,'y2',this.y);
}
function update()
{
// First call?
if( !motes )
motes = [];
// How many motes should there be?
var num = parseInt( document.getElementById('num_motes').value );
if( num < 0 )
num = 0;
// Make sure we have exactly that many...
// Too few?
while( motes.length < num )
motes.push( new Mote() );
// Or too many?
if( num == 0 )
motes = [];
else if( motes.length > num )
motes = motes.slice(0,num-1);
// Move a random mote
if( motes.length > 0 )
motes[ Rand( motes.length ) ].move();
// And do this again in 1/100 sec
setTimeout('update()', 10);
}
]]>
</script>
</body>
</html>
</pre>
<div id="SL_balloon_obj" style="display: block;">
<div class="SL_ImTranslatorLogo" id="SL_button" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%; opacity: 0; display: block; left: -8px; top: -25px; transition: visibility 2s ease 0s, opacity 2s linear 0s;"> </div>
<div id="SL_shadow_translation_result2" style="display: none;"> </div>
<div id="SL_shadow_translator" style="display: none;">
<div id="SL_planshet">
<div id="SL_arrow_up" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;"> </div>
<div id="SL_Bproviders">
<div class="SL_BL_LABLE_ON" id="SL_P0" title="Google">G</div>
<div class="SL_BL_LABLE_ON" id="SL_P1" title="Microsoft">M</div>
<div class="SL_BL_LABLE_ON" id="SL_P2" title="Translator">T</div>
</div>
<div id="SL_alert_bbl" style="display: none;">
<div id="SLHKclose" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;"> </div>
<div id="SL_alert_cont"> </div>
</div>
<div id="SL_TB">
<table id="SL_tables">
<tbody><tr>
<td class="SL_td"><input></td>
<td class="SL_td"><select><option value="auto">Detectar idioma</option><option value="af">Africâner</option><option value="sq">Albanês</option><option value="de">Alemão</option><option value="ar">Arabe</option><option value="hy">Armênio</option><option value="az">Azerbaijano</option><option value="eu">Basco</option><option value="bn">Bengali</option><option value="be">Bielo-russo</option><option value="my">Birmanês</option><option value="bs">Bósnio</option><option value="bg">Búlgaro</option><option value="ca">Catalão</option><option value="kk">Cazaque</option><option value="ceb">Cebuano</option><option value="ny">Chichewa</option><option value="zh-CN">Chinês (Simp)</option><option value="zh-TW">Chinês (Trad)</option><option value="si">Cingalês</option><option value="ko">Coreano</option><option value="ht">Crioulo haitiano</option><option value="hr">Croata</option><option value="da">Dinamarquês</option><option value="sk">Eslovaco</option><option value="sl">Esloveno</option><option value="es">Espanhol</option><option value="eo">Esperanto</option><option value="et">Estoniano</option><option value="fi">Finlandês</option><option value="fr">Francês</option><option value="gl">Galego</option><option value="cy">Galês</option><option value="ka">Georgiano</option><option value="el">Grego</option><option value="gu">Gujarati</option><option value="ha">Hauça</option><option value="iw">Hebraico</option><option value="hi">Hindi</option><option value="hmn">Hmong</option><option value="nl">Holandês</option><option value="hu">Húngaro</option><option value="ig">Igbo</option><option value="id">Indonésio</option><option value="en">Inglês</option><option value="yo">Ioruba</option><option value="ga">Irlandês</option><option value="is">Islandês</option><option value="it">Italiano</option><option value="ja">Japonês</option><option value="jw">Javanês</option><option value="kn">Kannada</option><option value="km">Khmer</option><option value="lo">Laosiano</option><option value="la">Latim</option><option value="lv">Letão</option><option value="lt">Lituano</option><option value="mk">Macedônico</option><option value="ml">Malaiala</option><option value="ms">Malaio</option><option value="mg">Malgaxe</option><option value="mt">Maltês</option><option value="mi">Maori</option><option value="mr">Marathi</option><option value="mn">Mongol</option><option value="ne">Nepalês</option><option value="no">Norueguês</option><option value="fa">Persa</option><option value="pl">Polonês</option><option value="pt">Português</option><option value="pa">Punjabi</option><option value="ro">Romeno</option><option value="ru">Russo</option><option value="sr">Sérvio</option><option value="st">Sesotho</option><option value="so">Somália</option><option value="sw">Suaíli</option><option value="su">Sudanês</option><option value="sv">Sueco</option><option value="tg">Tadjique</option><option value="tl">Tagalo</option><option value="th">Tailandês</option><option value="ta">Tâmil</option><option value="cs">Tcheco</option><option value="te">Telugo</option><option value="tr">Turco</option><option value="uk">Ucraniano</option><option value="ur">Urdu</option><option value="uz">Uzbeque</option><option value="vi">Vietnamita</option><option value="yi">Yiddish</option><option value="zu">Zulu</option></select></td>
<td class="SL_td">
<div id="SL_switch_b" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Alternar Idiomas"> </div>
</td>
<td class="SL_td"><select><option value="af">Africâner</option><option value="sq">Albanês</option><option value="de">Alemão</option><option value="ar">Arabe</option><option value="hy">Armênio</option><option value="az">Azerbaijano</option><option value="eu">Basco</option><option value="bn">Bengali</option><option value="be">Bielo-russo</option><option value="my">Birmanês</option><option value="bs">Bósnio</option><option value="bg">Búlgaro</option><option value="ca">Catalão</option><option value="kk">Cazaque</option><option value="ceb">Cebuano</option><option value="ny">Chichewa</option><option value="zh-CN">Chinês (Simp)</option><option value="zh-TW">Chinês (Trad)</option><option value="si">Cingalês</option><option value="ko">Coreano</option><option value="ht">Crioulo haitiano</option><option value="hr">Croata</option><option value="da">Dinamarquês</option><option value="sk">Eslovaco</option><option value="sl">Esloveno</option><option value="es">Espanhol</option><option value="eo">Esperanto</option><option value="et">Estoniano</option><option value="fi">Finlandês</option><option value="fr">Francês</option><option value="gl">Galego</option><option value="cy">Galês</option><option value="ka">Georgiano</option><option value="el">Grego</option><option value="gu">Gujarati</option><option value="ha">Hauça</option><option value="iw">Hebraico</option><option value="hi">Hindi</option><option value="hmn">Hmong</option><option value="nl">Holandês</option><option value="hu">Húngaro</option><option value="ig">Igbo</option><option value="id">Indonésio</option><option selected value="en">Inglês</option><option value="yo">Ioruba</option><option value="ga">Irlandês</option><option value="is">Islandês</option><option value="it">Italiano</option><option value="ja">Japonês</option><option value="jw">Javanês</option><option value="kn">Kannada</option><option value="km">Khmer</option><option value="lo">Laosiano</option><option value="la">Latim</option><option value="lv">Letão</option><option value="lt">Lituano</option><option value="mk">Macedônico</option><option value="ml">Malaiala</option><option value="ms">Malaio</option><option value="mg">Malgaxe</option><option value="mt">Maltês</option><option value="mi">Maori</option><option value="mr">Marathi</option><option value="mn">Mongol</option><option value="ne">Nepalês</option><option value="no">Norueguês</option><option value="fa">Persa</option><option value="pl">Polonês</option><option value="pt">Português</option><option value="pa">Punjabi</option><option value="ro">Romeno</option><option value="ru">Russo</option><option value="sr">Sérvio</option><option value="st">Sesotho</option><option value="so">Somália</option><option value="sw">Suaíli</option><option value="su">Sudanês</option><option value="sv">Sueco</option><option value="tg">Tadjique</option><option value="tl">Tagalo</option><option value="th">Tailandês</option><option value="ta">Tâmil</option><option value="cs">Tcheco</option><option value="te">Telugo</option><option value="tr">Turco</option><option value="uk">Ucraniano</option><option value="ur">Urdu</option><option value="uz">Uzbeque</option><option value="vi">Vietnamita</option><option value="yi">Yiddish</option><option value="zu">Zulu</option></select></td>
<td class="SL_td">
<div id="SL_TTS_voice" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Ouça"> </div>
</td>
<td class="SL_td">
<div class="SL_copy" id="SL_copy" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Copiar"> </div>
</td>
<td class="SL_td">
<div id="SL_bbl_font_patch"> </div>
<div class="SL_bbl_font" id="SL_bbl_font" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Tamanho da fonte"> </div>
</td>
<td class="SL_td">
<div id="SL_bbl_help" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Ajuda"> </div>
</td>
<td class="SL_td">
<div class="SL_pin_off" id="SL_pin" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Fixar a janela de pop-up"> </div>
</td>
</tr>
</tbody></table>
</div>
</div>
<div id="SL_shadow_translation_result" style=""> </div>
<div class="SL_loading" id="SL_loading" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;"> </div>
<div id="SL_player2"> </div>
<div id="SL_alert100">A função de fala é limitada a 200 caracteres</div>
<div id="SL_Balloon_options" style="background: rgb(255, 255, 255) repeat scroll 0% 0%;">
<div id="SL_arrow_down" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;"> </div>
<table id="SL_tbl_opt" style="width: 100%;">
<tbody><tr>
<td><input></td>
<td>
<div id="SL_BBL_IMG" style="background: rgba(0, 0, 0, 0) repeat scroll 0% 0%;" title="Mostrar o botão do ImTranslator 3 segundos"> </div>
</td>
<td><a class="SL_options" title="Mostrar opções">Opções</a> : <a class="SL_options" title="Histórico de tradução">Histórico</a> : <a class="SL_options" title="Comentários">Comentários</a> : <a class="SL_options" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GD9D8CPW8HFA2" title="Faça sua contribuição">Donate</a></td>
<td><span id="SL_Balloon_Close" title="Encerrar">Encerrar</span></td>
</tr>
</tbody></table>
</div>
</div>
</div>
|