Debian Codegraph @

samples/3D.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 <!-- 3D demo -->
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 id="body" width="640" height="480" viewBox="0 0 640 480">
33
34 <script type="text/ecmascript"><![CDATA[
35
36 // constants
37 var MAX_FOCAL_DISTANCE = 1100;
38 var MIN_FOCAL_DISTANCE = 400;
39 var INVISIBLE_FOCAL_DISTANCE = 20;
40
41 var OBJECT_COME = 1;
42 var OBJECT_GO = 2;
43 var OBJECT_PLAY = 3;
44
45 var _xOrigin = 320;
46 var _yOrigin = 240;
47 var svgNS = "http://www.w3.org/2000/svg";
48
49 // 3D object collection
50 var _objects;
51 var _currentObject;
52
53 // starting time for _animationState='OBJECT_PLAY'
54 var _time;
55
56 // object definition
57 var _points3d;
58 var _faces;
59
60 // svg shapes that represent the object
61 var _shapes;
62
63 // transformation matrix
64 var _matrix = new Array(1, 0, 0, 0,
65 0, 1, 0, 0,
66 0, 0, 1, 0,
67 0, 0, 0, 1);
68 var _gElt;
69
70 var _minFocalDistance;
71 var _focalDistance;
72 var _incFocalDistance = (MAX_FOCAL_DISTANCE - MIN_FOCAL_DISTANCE) / 75;
73 var _currentPoints3d;
74 var _alpha = 0;
75 var _incAlpha = Math.PI*2 / 100;
76 var _beta = 33;
77 var _incBeta = Math.PI*2 / 117;
78 var _gamma = 33;
79 var _incGamma = Math.PI*2 / 117;
80 var _animationState;
81
82 // Animates the object
83 function animate() {
84 if (_animationState == OBJECT_COME) {
85 comeObject();
86 } else if (_animationState == OBJECT_GO) {
87 goObject();
88 } else if (_animationState == OBJECT_PLAY) {
89 playObject();
90 }
91 animateObject();
92 }
93
94 // Makes the current object to come
95 function comeObject() {
96 if (_focalDistance+_incFocalDistance > MAX_FOCAL_DISTANCE) {
97 _minFocalDistance = MIN_FOCAL_DISTANCE;
98 _animationState = OBJECT_PLAY;
99 _time = new Date();
100 }
101 }
102
103 // Makes the current object to disapear
104 function goObject() {
105 if (_focalDistance+_incFocalDistance <= _minFocalDistance) {
106 switchToObject();
107 _animationState = OBJECT_COME;
108 }
109 }
110
111 // Makes the current object moving for a time
112 function playObject() {
113 var newTime = new Date();
114 if (newTime - _time > 8000) {
115 _animationState = OBJECT_GO;
116 _minFocalDistance = INVISIBLE_FOCAL_DISTANCE;
117 }
118 }
119
120 // Animates the object
121 function animateObject() {
122 setToRotate(_matrix, _alpha, _beta, _gamma);
123 transformObject(_matrix, _points3d, _currentPoints3d);
124 _alpha = (_alpha + _incAlpha) % (Math.PI*2);
125 _beta = (_beta + _incBeta) % (Math.PI*2);
126 _gamma = (_gamma + _incGamma) % (Math.PI*2);
127 for (var i = 0; i < _faces.length; ++i) {
128 buildShape(i);
129 }
130 _focalDistance += _incFocalDistance;
131 if (_focalDistance > MAX_FOCAL_DISTANCE) {
132 _incFocalDistance = -_incFocalDistance;
133 } else if (_focalDistance < _minFocalDistance) {
134 _incFocalDistance = -_incFocalDistance;
135 }
136 }
137
138 // Global initialization
139 function startAnimation(evt) {
140 var i = 0;
141 _objects = new Array();
142
143 var object;
144
145 object = new Array(3);
146 object[0] = buildCubePoints();
147 object[1] = buildCubeFaces();
148 object[2] = buildCubeColors();
149 object[3] = buildCubeAnimationDefinition();
150 _objects[i++] = object;
151
152 object = new Array(3);
153 object[0] = buildTriPoints();
154 object[1] = buildTriFaces();
155 object[2] = buildTriColors();
156 object[3] = buildTriAnimationDefinition();
157 _objects[i++] = object;
158
159 object = new Array(3);
160 object[0] = buildRectPoints();
161 object[1] = buildRectFaces();
162 object[2] = buildRectColors();
163 object[3] = buildRectAnimationDefinition();
164 _objects[i++] = object;
165
166 _gElt = evt.target;
167
168 _focalDistance = INVISIBLE_FOCAL_DISTANCE;
169 _minFocalDistance = INVISIBLE_FOCAL_DISTANCE;
170 _animationState = OBJECT_COME;
171
172 _currentObject = -1;
173 switchToObject();
174
175 // launch animation
176 setInterval('animate()', 20);
177 }
178
179 // switch object
180 function switchToObject() {
181 _currentObject++;
182 if (_currentObject == _objects.length) {
183 _currentObject = 0;
184 }
185 var object = _objects[_currentObject];
186 _points3d = object[0];
187 _faces = object[1];
188
189 // updates the animation definition
190 var defs = object[3];
191 _incFocalDistance = defs[0];
192 _incAlpha = defs[1];
193 _incBeta = defs[2];
194 _incGamma = defs[3];
195
196 // copy current object's coordinates to currentPoints3d
197 _currentPoints3d = new Array(_points3d.length);
198 for (var i = 0; i < _points3d.length; ++i) {
199 var pt3d = _points3d[i];
200 var cPt3d = new Array(3);
201 cPt3d[0] = pt3d[0];
202 cPt3d[1] = pt3d[1];
203 cPt3d[2] = pt3d[2];
204 _currentPoints3d[i] = cPt3d;
205 }
206
207 // clean container
208 while (_gElt.firstChild != null) {
209 _gElt.removeChild(_gElt.firstChild);
210 }
211
212 // build svg shapes
213 _shapes = new Array(_faces.length);
214 var colors = object[2];
215 for (var i = 0; i < _faces.length; ++i) {
216 var e = document.createElementNS(svgNS, "polygon");
217 e.setAttribute("style", "fill:"+colors[i]);
218 _shapes[i] = e;
219 buildShape(i);
220 _gElt.appendChild(e);
221 }
222 }
223
224 // Builds the shapes according to the current points 3D
225 function buildShape(index) {
226 var e = _shapes[index];
227 var face = _faces[index];
228 var pointsData = "";
229 if (!isFaceVisible(index)) {
230 pointsData = "-10 -10"; // hide the shape
231 } else {
232 var pt2d = new Array(2);
233 for (var i = 0; i < face.length; ++i) {
234 projectPoint3d(_currentPoints3d[face[i]], pt2d);
235 pointsData += pt2d[0]+" "+pt2d[1]+" ";
236 }
237 }
238 e.setAttribute("points", pointsData);
239 }
240
241 // --------------------------------------------------------------------
242 // 2D functions
243 // --------------------------------------------------------------------
244
245 // Projects a 3D point on the screen
246 function projectPoint3d(srcPt3d, destPt2d) {
247 var x = srcPt3d[0];
248 var y = srcPt3d[1];
249 var z = srcPt3d[2];
250 if (z == 0) {
251 z = 1;
252 }
253 destPt2d[0] = _focalDistance * x / (800-z) + _xOrigin;
254 destPt2d[1] = _focalDistance * y / (800-z) + _yOrigin;
255 }
256
257
258 // --------------------------------------------------------------------
259 // 3D functions
260 // --------------------------------------------------------------------
261
262 // Returns true if the face at the specified index is visible
263 function isFaceVisible(index) {
264 var face = _faces[index];
265 var p1 = _currentPoints3d[face[0]];
266 var p2 = _currentPoints3d[face[1]];
267 var p3 = _currentPoints3d[face[2]];
268 return ((p3[0]-p1[0])*(p3[1]-p2[1])-(p3[0]-p2[0])*(p3[1]-p1[1]) > 0);
269 }
270
271 // Transforms according to the specified matrix, the specified 3D points
272 function transformObject(matrix, points3d, currentPoints3d) {
273 for (var i = 0; i < currentPoints3d.length; ++i) {
274 transformPoint3d(matrix, points3d[i], currentPoints3d[i]);
275 }
276 }
277
278 // Transforms according to the specified matrix, the specified point
279 function transformPoint3d(matrix, srcPt3d, destPt3d) {
280 var x = srcPt3d[0];
281 var y = srcPt3d[1];
282 var z = srcPt3d[2];
283 destPt3d[0] = x*matrix[0]+y*matrix[1]+z*matrix[2] + matrix[3];
284 destPt3d[1] = x*matrix[4]+y*matrix[5]+z*matrix[6] + matrix[7];
285 destPt3d[2] = x*matrix[8]+y*matrix[9]+z*matrix[10] + matrix[11];
286 }
287
288 // Sets the specified matrix to a rotation matrix
289 function setToRotate(matrix, alpha, beta, gamma) {
290 // Assuming the angles are in radians
291 var c1 = Math.cos(alpha);
292 var s1 = Math.sin(alpha);
293 var c2 = Math.cos(beta);
294 var s2 = Math.sin(beta);
295 var c3 = Math.cos(gamma);
296 var s3 = Math.sin(gamma);
297
298 matrix[0] = c1 * c2;
299 matrix[1] = s1 * c2;
300 matrix[2] = -s2;
301 matrix[3] = 0;
302
303 matrix[4] = -(s1 * c3)+(c1 * s2 * s3);
304 matrix[5] = (c1*c3) + (s1 * s2 * s3);
305 matrix[6] = c2 * s3;
306 matrix[7] = 0;
307
308 matrix[8] = (s1 * s3) + (c1 * s2 * c3);
309 matrix[9] = -(c1 * s3) + (s1 * s2 * c3);
310 matrix[10] = c2*c3;
311 matrix[11] = 0;
312 }
313
314 // --------------------------------------------------------------------
315 // Object collection
316 // --------------------------------------------------------------------
317
318 // --- Cube ---
319
320 // Constructs the animation definition
321 function buildCubeAnimationDefinition() {
322 var defs = new Array();
323 defs[0] = (MAX_FOCAL_DISTANCE - MIN_FOCAL_DISTANCE) / 75; // incFocalDistance
324 defs[1] = Math.PI*2 / 100; // _incAlpha
325 defs[2] = Math.PI*2 / 117; // _incBeta
326 defs[3] = 0; // _incGamma
327 return defs;
328 }
329
330 // Constructs the color palette for the object
331 function buildCubeColors() {
332 return new Array("#04a", "#025", // front
333 "#4a4", "#252", // back
334 "#a44", "#522", // top
335 "#aa4", "#552", // bottom
336 "#a4a", "#525", // left
337 "#aaa", "#555"); // right
338 }
339
340 // Constructs the points array, anti-clockwise
341 function buildCubeFaces() {
342 var faces = new Array(12);
343 faces[0] = new Array(0, 2, 3); // front
344 faces[1] = new Array(0, 3, 1);
345 faces[2] = new Array(6, 4, 5); // back
346 faces[3] = new Array(6, 5, 7);
347 faces[4] = new Array(4, 0, 1); // top
348 faces[5] = new Array(4, 1, 5);
349 faces[6] = new Array(2, 6, 7); // bottom
350 faces[7] = new Array(2, 7, 3);
351 faces[8] = new Array(4, 6, 2); // left
352 faces[9] = new Array(4, 2, 0);
353 faces[10] = new Array(1, 3, 7); // right
354 faces[11] = new Array(1, 7, 5);
355 return faces;
356 }
357
358 // Constructs the points array
359 function buildCubePoints() {
360 var points3d = new Array(8);
361
362 // front coordinates
363 points3d[0] = new Array(-50, -50, -50); // x,y,z coordinates
364 points3d[1] = new Array(50, -50, -50);
365 points3d[2] = new Array(-50, 50, -50);
366 points3d[3] = new Array(50, 50, -50);
367
368 // back coordinates
369 points3d[4] = new Array(-50, -50, 50);
370 points3d[5] = new Array(50, -50, 50);
371 points3d[6] = new Array(-50, 50, 50);
372 points3d[7] = new Array(50, 50, 50);
373
374 return points3d;
375 }
376
377 // --- Rect ---
378
379 // Constructs the animation definition
380 function buildRectAnimationDefinition() {
381 var defs = new Array();
382 defs[0] = (MAX_FOCAL_DISTANCE - MIN_FOCAL_DISTANCE) / 100; // incFocalDistance
383 defs[1] = Math.PI*2 / 200; // _incAlpha
384 defs[2] = Math.PI*2 / 217; // _incBeta
385 defs[3] = Math.PI*2 / 128; // _incGamma
386 return defs;
387 }
388
389 // Constructs the color palette for the object
390 function buildRectColors() {
391 return new Array("#04a", "#025", // front
392 "#4a4", "#252", // back
393 "#a44", "#522", // top
394 "#aa4", "#552", // bottom
395 "#a4a", "#525", // left
396 "#aaa", "#555"); // right
397 }
398
399 // Constructs the points array, anti-clockwise
400 function buildRectFaces() {
401 var faces = new Array(12);
402 faces[0] = new Array(0, 2, 3); // front
403 faces[1] = new Array(0, 3, 1);
404 faces[2] = new Array(6, 4, 5); // back
405 faces[3] = new Array(6, 5, 7);
406 faces[4] = new Array(4, 0, 1); // top
407 faces[5] = new Array(4, 1, 5);
408 faces[6] = new Array(2, 6, 7); // bottom
409 faces[7] = new Array(2, 7, 3);
410 faces[8] = new Array(4, 6, 2); // left
411 faces[9] = new Array(4, 2, 0);
412 faces[10] = new Array(1, 3, 7); // right
413 faces[11] = new Array(1, 7, 5);
414 return faces;
415 }
416
417 // Constructs the points array
418 function buildRectPoints() {
419 var points3d = new Array(8);
420
421 // front coordinates
422 points3d[0] = new Array(-60, -20, -50); // x,y,z coordinates
423 points3d[1] = new Array(60, -20, -50);
424 points3d[2] = new Array(-60, 20, -50);
425 points3d[3] = new Array(60, 20, -50);
426
427 // back coordinates
428 points3d[4] = new Array(-60, -20, 50);
429 points3d[5] = new Array(60, -20, 50);
430 points3d[6] = new Array(-60, 20, 50);
431 points3d[7] = new Array(60, 20, 50);
432
433 return points3d;
434 }
435
436 // --- Triangle ---
437
438 // Constructs the animation definition
439 function buildTriAnimationDefinition() {
440 var defs = new Array();
441 defs[0] = (MAX_FOCAL_DISTANCE - MIN_FOCAL_DISTANCE) / 100; // incFocalDistance
442 defs[1] = 0; // _incAlpha
443 defs[2] = Math.PI*2 / 217; // _incBeta
444 defs[3] = Math.PI*2 / 128; // _incGamma
445 return defs;
446 }
447
448 // Constructs the color palette for the object
449 function buildTriColors() {
450 return new Array("#0a0", "#a00", "#00a", "#a0a");
451 }
452
453 // Constructs the points array, anti-clockwise
454 function buildTriFaces() {
455 var faces = new Array(4);
456 faces[0] = new Array(0, 3, 1);
457 faces[1] = new Array(1, 3, 2);
458 faces[2] = new Array(1, 2, 0);
459 faces[3] = new Array(3, 0, 2);
460 return faces;
461 }
462
463 // Constructs the points array
464 function buildTriPoints() {
465 var points3d = new Array(4);
466
467 points3d[0] = new Array(20, 20, 20); // x,y,z coordinates
468 points3d[1] = new Array(50, 50, 100);
469 points3d[2] = new Array(100, 20, 20);
470 points3d[3] = new Array(50, 100, 20);
471 return points3d;
472 }
473
474 ]]></script>
475
476 <defs>
477 <linearGradient id="grad1">
478 <stop offset="0" style="stop-color:blue" />
479 <stop offset=".2" style="stop-color:red" />
480 <stop offset=".4" style="stop-color:yellow" />
481 <stop offset=".6" style="stop-color:green" />
482 <stop offset=".8" style="stop-color:cyan" />
483 <stop offset="1" style="stop-color:blue" />
484 </linearGradient>
485 </defs>
486
487 <rect x="0" y="10%" width="100%" height="80%" style="fill:black" />
488 <rect x="0" y="0" width="100%" height="10%" style="fill:#004" />
489 <rect x="0" y="90%" width="100%" height="10%" style="fill:#004" />
490
491 <rect x="0" y="10%" width="100%" height="1" style="fill:url(#grad1)" />
492 <rect x="0" y="90%" width="100%" height="1" style="fill:url(#grad1)" />
493 <text x="50%" y="40" style="text-anchor:middle; font-family:Impact;font-size:36; fill:white;">Batik 3D</text>
494 <g id="container" onload="startAnimation(evt)">
495
496 </g>
497
498 <!-- ============================================================= -->
499 <!-- Batik sample mark -->
500 <!-- ============================================================= -->
501 <use transform="translate(598,447)" width="27" height="28"
502 xlink:href="batikLogo.svg#Batik_Squiggle" />
503
504 </svg>