Develop and Download Open Source Software

Browse Subversion Repository

Contents of /iSlide.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations) (download) (as text)
Thu Jul 22 13:46:34 2010 UTC (13 years, 8 months ago) by isao-hara
File MIME type: application/x-javascript
File size: 24120 byte(s)
color palette
1 /*
2 iSlide.js
3
4 iSlideMaker
5 http://sourceforge.jp/projects/islidemaker/simple/
6
7 Copyright(c) 2010, Isao Hara, isao-hara@users.sourceforge.jp
8
9 All rights reserved. This program is made available under the terms of the
10 Eclipse Public License v1.0 which accompanies this distribution, and is
11 available at http://www.eclipse.org/legal/epl-v10.html
12
13 Contributors: Isao Hara.
14
15 */
16
17 ////////////////
18 var Slide;
19 var touchX;
20 var touchY;
21 var touchMX;
22 var touchMY;
23 var orientation;
24
25 ///////////////
26
27 function iSlide_init(){
28 Slide = new iSlide();
29 Slide.showSlideByIndex(0);
30 }
31
32 function setSlideIndex(n){
33 Slide.showSlideByIndex(n);
34 }
35
36 function hasClass(element, className) {
37 if (!element.className) return false;
38 return (element.className.search('(^|\\s)' + className + '(\\s|$)') != -1);
39 }
40
41 function hasValue(element, value) {
42 if (!element) return false;
43 return (element.search('(^|\\s)' + value + '(\\s|$)') != -1);
44 }
45
46 function removeClass(element,className) {
47 if (!element) return;
48 element.className = element.className.replace(new RegExp('(^|\\s)'+className+'(\\s|$)'), RegExp.$1+RegExp.$2);
49 }
50
51 function addClass(element,className) {
52 if (!element || hasClass(element, className)) return;
53 if (element.className) {
54 element.className += ' '+className;
55 } else {
56 element.className = className;
57 }
58 }
59
60 function GetElementsWithClassName(top, elementName,className) {
61
62 var allElements = top.getElementsByTagName(elementName);
63 var elemColl = new Array();
64 for (var i = 0; i< allElements.length; i++) {
65 if (hasClass(allElements[i], className)) {
66 elemColl[elemColl.length] = allElements[i];
67 }
68 }
69 return elemColl;
70 }
71
72 function isParentOrSelf(element, id) {
73 if (element == null || element.nodeName=='BODY') return false;
74 else if (element.id == id) return true;
75 else return isParentOrSelf(element.parentNode, id);
76 }
77
78 function getSlideId(element) {
79 if (element == null || element.nodeName=='BODY') return null;
80 else if (element.id != null && element.id.substr(0,5) == 'slide') return element.id;
81 else return getSlideId(element.parentNode);
82 }
83
84 function nodeValue(node) {
85 var result = "";
86 if (node.nodeType == 1) {
87 var children = node.childNodes;
88 for (var i = 0; i < children.length; ++i) {
89 result += nodeValue(children[i]);
90 }
91 }
92 else if (node.nodeType == 3) {
93 result = node.nodeValue;
94 }
95 return(result);
96 }
97
98 //////// iSlide Class
99 var iSlide = function(){
100 if(window.innerWidth>window.innerHeight){
101 orientation = 'landscape';
102 this.WinWidth=window.innerWidth;
103 this.WinHeight=window.innerHeight;
104 this.scale = 1;
105 }else{
106 orientation = 'portrate';
107 this.WinWidth=window.innerHeight;
108 this.WinHeight=window.innerWidth;
109 this.scale = window.innerWidth/ window.innerHeight;
110 }
111
112
113 this.current_incs=0;
114 this.thumbMaxX=5;
115 this.current_slide_index = -1;
116 this.current_slide_id = null;
117 this.mode;
118 this.slides=null;
119 this.incrementals=null;
120 this.header=null;
121 this.footer=null;
122 this.num_slides=0;
123
124 this.initSlide();
125 }
126
127 iSlide.prototype.initSlide=function(){
128 var top = document.getElementById('presentation');
129 if(!top){
130 alert('No presentation dvi...');
131 return;
132 }
133
134 this.slides = GetElementsWithClassName(top, 'div', 'slide');
135 this.num_slides=this.slides.length;
136 this.incrementals = this.createIncrementals();
137
138 this.createControls();
139 this.slideLabel();
140
141 this.header = document.getElementById('header');
142 this.header.setAttribute('class','header');
143 this.footer = document.getElementById('footer');
144 this.footer.setAttribute('class','footer');
145 this.num_slides=this.slides.length;
146
147 for(var i=0;i<this.slides.length; i++){
148 var header = this.header.cloneNode(true);
149 var footer = this.footer.cloneNode(true);
150 var obj = this.slides[i];
151
152 obj.setAttribute('id', 'slide'+i);
153 header.setAttribute('id','');
154 footer.setAttribute('id','');
155
156 obj.insertBefore(header, obj.childNodes[0]);
157 obj.appendChild(footer);
158
159 if(navigator.userAgent.match("iPad")){
160
161 obj.addEventListener('touchstart', selectSlide, false);
162 obj.addEventListener('touchmove', moveEventHandle, false);
163 obj.addEventListener('touchend', touchSlide, false);
164
165 }else{
166 document.onmousedown=selectSlideWithMouse;
167 document.onmousemove=moveEventHandleWithMouse;
168 document.onmouseup=touchSlideWithMouse;
169 }
170
171 obj.style.width=this.WinWidth+'px';
172 obj.style.height=this.WinHeight+'px';
173 obj.style.position='absolute';
174 obj.style.display='none';
175 }
176 }
177
178 iSlide.prototype.updateOrientation=function() {
179 if(window.innerWidth > window.innerHeight){
180 orientation = 'landscape';
181 this.WinWidth=window.innerWidth;
182 this.WinHeight=window.innerHeight;
183 this.scale = 1;
184 }else{
185 orientation = 'portrate';
186 this.WinWidth=window.innerHeight;
187 this.WinHeight=window.innerWidth;
188 this.scale = window.innerWidth/ window.innerHeight;
189 }
190 }
191
192 iSlide.prototype.slideLabel=function() {
193 var list = document.getElementById('jumplist');
194 smax = this.num_slides;
195 for (var n = 0; n < smax; n++) {
196 var obj = this.slides[n];
197
198 var did = 'slide' + n.toString();
199 obj.setAttribute('id',did);
200
201 var otext = '';
202 var menu = obj.firstChild;
203 if (!menu) continue;
204 while (menu && menu.nodeType == 3) {
205 menu = menu.nextSibling;
206 }
207 if (!menu) continue;
208
209 var menunodes = menu.childNodes;
210 for (var o = 0; o < menunodes.length; o++) {
211 otext += nodeValue(menunodes[o]);
212 }
213
214 list.options[list.length] = new Option(n + ' : ' + otext, n);
215 }
216 }
217
218 iSlide.prototype.createControls = function() {
219 var controlsDiv = document.getElementById("controls");
220 var hideDiv, hideList = '';
221
222 if (!controlsDiv) return;
223
224 controlsDiv.innerHTML = '<div id="navLinks">' +
225 '<button onClick="hideAllSlides();fileSelector();">End<\/button>'+
226 '<button onClick="hideAllSlides();editCurrentSlide();">Edit<\/button>'+
227 '<button onClick="showThumbnail();">Thumb<\/button>'+
228 '<select id="jumplist" onchange="jumpSlide();"><\/select>' +
229 '<\/div>';
230
231 controlsDiv.style.display='none';
232 }
233
234 iSlide.prototype.displaySlide = function(idx, scale, x, y, frame){
235 var obj = this.slides[idx];
236 var x0=this.WinWidth*scale;
237 var y0=this.WinHeight*scale;
238 var xx = (this.WinWidth/2) * scale - this.WinWidth/2 + 5 + (5+x0)*x;
239 var yy = (this.WinHeight/2) * scale - this.WinHeight/2 + 5+ (5+y0)*y;
240
241 obj.style.webkitTransform='scale('+scale+')';
242
243 if (frame){
244 obj.style.border="1px solid black";
245 }else{
246 xx=xx-5;
247 yy=yy-5;
248 obj.style.border="1px none black";
249 }
250
251 obj.style.left = xx+'px';
252 obj.style.top = yy+'px';
253
254 obj.style.display='block';
255 }
256
257 iSlide.prototype.showThumbnail = function(){
258 var maxX = this.thumbMaxX;
259 if(this.slides == null) return;
260
261 this.mode='thumbnail';
262 this.current_slide_index = -1;
263
264 scale = (this.WinWidth - 10)/maxX/this.WinWidth;
265
266 var y = 0;
267 var x = 0;
268
269 for(var i=0;i<this.num_slides;i++){
270 y = Math.floor(i/maxX);
271 x = i % maxX;
272 this.displaySlide(i, scale, x, y, true);
273 }
274
275 var svg_objs = document.getElementsByTagName('object');
276 for(var i=0;i<svg_objs.length;i++){
277 svg_objs[i].style.display='none';
278 }
279 }
280
281 iSlide.prototype.currentSlide = function() {
282 var cs = document.getElementById('currentSlide');
283 cs.innerHTML = '<span id="csHere">' + this.current_slide_index + '<\/span> ' +
284 '<span id="csSep">\/<\/span> ' +
285 '<span id="csTotal">' + (this.num_slides-1) + '<\/span>';
286 if (this.current_slide_index == 0) { cs.style.visibility = 'hidden'; }
287 else { cs.style.visibility = 'visible'; }
288 }
289
290 iSlide.prototype.getIncrementals=function(obj) {
291 var incrs = new Array();
292
293 if (!obj) return incrs;
294
295 var children = obj.childNodes;
296
297 for (var i = 0; i < children.length; i++) {
298 var child = children[i];
299 if (hasClass(child, 'incremental')) {
300 if (child.nodeName == 'OL' || child.nodeName == 'UL') {
301 removeClass(child, 'incremental');
302 for (var j = 0; j < child.childNodes.length; j++) {
303 if (child.childNodes[j].nodeType == 1) {
304 addClass(child.childNodes[j], 'incremental');
305 }
306 }
307 } else {
308 incrs[incrs.length] = child;
309 removeClass(child,'incremental');
310 }
311 }
312 if (hasClass(child, 'show-first')) {
313 if (child.nodeName == 'OL' || child.nodeName == 'UL') {
314 removeClass(child, 'show-first');
315 if (child.childNodes[isGe].nodeType == 1) {
316 removeClass(child.childNodes[isGe], 'incremental');
317 }
318 } else {
319 incrs[incrs.length] = child;
320 }
321 }
322 incrs = incrs.concat(this.getIncrementals(child));
323 }
324 return incrs;
325 }
326
327 iSlide.prototype.createIncrementals=function() {
328 this.incrementals = new Array();
329 for (var i = 0; i < this.num_slides; i++) {
330 this.incrementals[i] = this.getIncrementals(this.slides[i]);
331 }
332 return this.incrementals;
333 }
334
335 iSlide.prototype.jumpSlide=function(){
336 var jl = document.getElementById('jumplist');
337 var pos = parseInt(jl.value);
338 this.showSlideByIndex(pos);
339 }
340
341 iSlide.prototype.showSlideByIndex=function(idx){
342 if(!this.slides) return false;
343
344 var incs = this.incrementals[idx];
345
346 for(var i=0;i < incs.length;i++){
347 removeClass(incs[i], "current");
348 addClass(incs[i], "incremental");
349 }
350 this.current_incs = 0;
351 this.showSlide(idx);
352 }
353
354 iSlide.prototype.showSlide=function(idx){
355 var svg_objs = document.getElementsByTagName('object');
356 for(var i=0;i<svg_objs.length;i++){ svg_objs[i].style.display='block'; }
357
358 for(var i=0; i<this.num_slides; i++){
359 if(i != idx) this.slides[i].style.display='none';
360 }
361 hideControls();
362 this.current_slide_index = idx;
363 this.displaySlide(idx, this.scale, 0, 0, false);
364 this.currentSlide();
365 drawCanvas(this.slides[idx]);
366 this.mode='presentation';
367 }
368
369 iSlide.prototype.hideAllSlides=function(){
370 for(i in this.slides){
371 this.slides[i].style.display='none';
372 }
373 hideControls();
374 }
375
376 iSlide.prototype.nextSlide=function(){
377 if(this.current_slide_index < 0 || this.current_slide_index == this.num_slides -1 ) return false;
378 this.current_incs = 0;
379 this.showSlideByIndex(this.current_slide_index + 1);
380 }
381
382 iSlide.prototype.prevSlide=function(){
383 if(this.current_slide_index <= 0 ) return false;
384 this.current_incs = 0;
385 this.showSlideByIndex(this.current_slide_index -1);
386 }
387
388 iSlide.prototype.modeEditFile=function(){
389 this.mode = 'editfile';
390 }
391
392 iSlide.prototype.showCurrentSlide=function(){
393 if(this.current_slide_index >= 0 && this.current_slide_index < this.num_slides)
394 this.showSlideByIndex(this.current_slide_index);
395 }
396
397 iSlide.prototype.nextStep=function(){
398 var incs = this.incrementals[this.current_slide_index];
399
400 if(incs.length == 0 || incs.length < this.current_incs){
401 this.nextSlide();
402 }else if(incs.length > Slide.current_incs){
403 if(this.current_incs > 0) removeClass(incs[this.current_incs-1], "current");
404 removeClass(incs[this.current_incs], "incremental");
405 addClass(incs[this.current_incs], "current");
406 this.current_incs += 1;
407 }else if(incs.length == this.current_incs){
408 if(this.current_incs > 0) removeClass(incs[this.current_incs-1], "current");
409 this.current_incs += 1;
410 }
411 }
412
413 //////
414 function hideControls(){
415 var controlsDiv = document.getElementById("controls");
416 controlsDiv.style.display='none';
417 }
418
419 function hideAllSlides(){
420 Slide.hideAllSlides();
421 }
422
423 function jumpSlide(){
424 Slide.jumpSlide();
425 }
426 function modeEditFile(){
427 Slide.modeEditFile();
428 }
429
430 function showThumbnail(){
431 Slide.showThumbnail();
432 hideControls();
433 }
434
435 function editCurrentSlide(){
436 if(Slide.current_slide_index < 0) return;
437 editSlideByIndex(Slide.current_slide_index);
438 }
439
440 //// Event Handler
441 function selectSlide(e){
442 if(e.touches.length != 1) return false;
443
444 e.preventDefault();
445
446 touchMX=touchX=e.touches[0].clientX;
447 touchMY=touchY=e.touches[0].clientY;
448
449 if(Slide.mode != 'thumbnail') return false;
450
451 var current_slide_id = getSlideId(e.touches[0].target);
452
453 if( current_slide_id !== null){
454 Slide.current_slide_index = parseInt(current_slide_id.substr(5));
455 }else {
456 Slide.current_slide_index = -1;
457 }
458
459 return false;
460 }
461
462
463 function touchSlide(e){
464 e.preventDefault();
465
466 var controlsDiv = document.getElementById("controls");
467
468 if(controlsDiv.style.display != 'none'){
469 controlsDiv.style.display='none';
470 return false;
471 }
472
473 if(Slide.mode == 'thumbnail') {
474 Slide.showCurrentSlide();
475 return false;
476 }
477
478 if(Slide.mode != 'presentation' && Slide.mode != 'editfile') return false;
479
480 if(touchX < Slide.scale*Slide.WinWidth*0.1 && touchY > Slide.scale*Slide.WinHeight*0.9 ){
481 if(Slide.mode == 'editfile'){
482 hideItemById('presentation');
483 showItemById('menuDiv');
484 showItemById('editDiv');
485 return false;
486 }else{
487 showThumbnail();
488 return false;
489 }
490 }
491
492 var deltaX = touchMX - touchX;
493 var deltaY = touchMY - touchY;
494
495 if(deltaX > Slide.scale*Slide.WinWidth*0.1){
496 Slide.nextSlide();
497 }else if(deltaX < -Slide.scale*Slide.WinWidth*0.1){
498 Slide.prevSlide();
499 }else if(touchY > Slide.scale*Slide.WinHeight - 20){
500 if(Slide.mode == 'editfile'){
501 hideItemById('presentation');
502 showItemById('menuDiv');
503 showItemById('editDiv');
504 }else{
505 controlsDiv.style.display='block';
506 }
507 }else {
508 Slide.nextStep();
509 }
510
511 }
512 function moveEventHandle(e){
513 if(e.touches.length != 1) return false;
514 e.preventDefault();
515
516 touchMX=e.touches[0].clientX;
517 touchMY=e.touches[0].clientY;
518 return false;
519 }
520
521 function selectSlideWithMouse(e){
522 touchMX=touchX=e.pageX;
523 touchMY=touchY=e.pageY;
524
525 if(Slide.mode != 'thumbnail') return false;
526
527 var current_slide_id = getSlideId(e.target);
528
529 if( current_slide_id !== null){
530 Slide.current_slide_index = parseInt(current_slide_id.substr(5));
531 }else {
532 current_slide_index = -1;
533 }
534
535 return false;
536 }
537
538 function touchSlideWithMouse(e){
539 var controlsDiv = document.getElementById("controls");
540
541 if(controlsDiv.style.display != 'none'){
542 controlsDiv.style.display='none';
543 return false;
544 }
545
546 if(Slide.mode == 'thumbnail') {
547 Slide.showCurrentSlide();
548 return false;
549 }
550
551 if(Slide.mode != 'presentation' && Slide.mode != 'editfile') return false;
552
553 if(touchX < Slide.scale*Slide.WinWidth*0.1 && touchY > Slide.scale*Slide.WinHeight*0.9 ){
554 if(Slide.mode == 'editfile'){
555 hideItemById('presentation');
556 showItemById('menuDiv');
557 showItemById('editDiv');
558 return false;
559 }else{
560 showThumbnail();
561 return false;
562 }
563 }
564
565 var deltaX = touchMX - touchX;
566 var deltaY = touchMY - touchY;
567
568 if(deltaX > Slide.scale*Slide.WinWidth*0.1){
569 Slide.nextSlide();
570 }else if(deltaX < -Slide.scale*Slide.WinWidth*0.1){
571 Slide.prevSlide();
572 }else if(touchY > Slide.scale*Slide.WinHeight - 20){
573 if(Slide.mode == 'editfile'){
574 hideItemById('presentation');
575 showItemById('menuDiv');
576 showItemById('editDiv');
577 }else{
578 controlsDiv.style.display='block';
579 }
580 }else {
581 Slide.nextStep();
582 }
583
584 }
585
586 function moveEventHandleWithMouse(e){
587
588 touchMX=e.pageX;
589 touchMY=e.pageY;
590 return false;
591 }
592
593
594 ////////
595 function updateOrientation(){
596 Slide.updateOrientation();
597
598 if(mode=='thumbnail'){
599 showThumbnail();
600 }else if(mode=='presentation'){
601 Slide.showCurrentSlide();
602 }
603 }
604
605 //////// SVG compatible canvas functions
606 function drawString(){
607 var x = document.getElementById('toString');
608 var aa = document.getElementById('slide1');
609 if(aa) x.innerHTML = aa.toString();
610 }
611
612 function drawCanvas(element){
613 var canvasEle = element.getElementsByTagName("canvas");
614 for (var i = 0; i< canvasEle.length; i++) {
615 draw(canvasEle[i]);
616 }
617 }
618
619 function draw(canvas){
620 if(!canvas.hasChildNodes()) return false;
621 if(!canvas || !canvas.getContext){ return false; }
622 var ctx = canvas.getContext('2d');
623 var ele = canvas.childNodes;
624 drawAll(ctx, ele);
625 }
626
627 function drawAll(ctx, ele){
628 for(var i=0 ; i<ele.length; i++){
629 var tName = ele[i].tagName;
630 if(!tName) continue;
631
632 var color = ele[i].getAttribute("stroke");
633 var fill = ele[i].getAttribute("fill");
634 var width = ele[i].getAttribute("stroke-width");
635
636 switch(tName){
637 case 'TEXT':
638 var x = ele[i].getAttribute("x");
639 var y = ele[i].getAttribute("y");
640 var f_family = ele[i].getAttribute("font-family");
641 var f_size = ele[i].getAttribute("font-size");
642 var f_style = ele[i].getAttribute("font-style");
643 var txt = ele[i].textContent;
644
645 ctx.beginPath();
646 if(!f_size) f_size = '12pt';
647 if(!f_family) f_family = 'Helvetica';
648 if(!f_style) f_style = '';
649
650 ctx.fillStyle = color;
651 ctx.font = f_style + " " + f_size+"pt" + " '"+f_family + "'";
652 ctx.fillText( txt, x, y);
653 ctx.closePath();
654
655 break;
656 case 'POLYLINE':
657 var points = ele[i].getAttribute("points");
658 polyline(ctx, points, color, fill, width, false);
659 break;
660
661 case 'POLYGON':
662 var points = ele[i].getAttribute("points");
663 polyline(ctx, points, color, fill, width, true);
664 break;
665
666 case 'LINE':
667 var points = ele[i].getAttribute("x1");
668 points += ',' + ele[i].getAttribute("y1");
669 points += ' ' + ele[i].getAttribute("x2");
670 points += ',' + ele[i].getAttribute("y2");
671 polyline(ctx, points, color, 'none', width, false);
672 break;
673
674 case 'IMG':
675 var iname = ele[i].getAttribute("xlink:href");
676 var x = ele[i].getAttribute("x");
677 var y = ele[i].getAttribute("y");
678 var w = ele[i].getAttribute("width");
679 var h = ele[i].getAttribute("height");
680 if(!x) x=0;
681 if(!y) y=0;
682 if(!w) w=-1;
683 if(!h) h=-1;
684 svg_image(ctx, iname, x, y, w, h);
685 break;
686
687 case 'PATH':
688 var d = ele[i].getAttribute("d");
689 var path = d.split(' ');
690 var cmd = path.shift();
691 var pp;
692 var p;
693
694
695
696 ctx.beginPath();
697 if(color)ctx.strokeStyle = color;
698 if(width) ctx.lineWidth = width;
699
700 while(path.length > 0){
701 pp = path.shift();
702 p=pp.split(',');
703
704 if(p.length == 1){
705 cmd=p[0];
706 if(cmd == "Z" || cmd == "z"){
707 ctx.stroke();
708 }
709 continue;
710 }
711
712 if(cmd == "Z" || cmd == "z"){
713 ctx.stroke();
714 }else if (cmd == "M"){
715 ctx.moveTo(p[0], p[1]);
716 }else if (cmd == "m"){
717 }else if (cmd == "L"){
718 ctx.lineTo(p[0], p[1]);
719 }else if (cmd == "l"){
720
721 }else if (cmd == "H"){
722
723 }else if (cmd == "h"){
724
725 }else if (cmd == "V"){
726
727 }else if (cmd == "v"){
728
729 }else if (cmd == "C"){
730 var pp2 = path.shift();
731 var p2=pp2.split(',');
732 if(p2.length == 1){
733 cmd=p2[0];
734 continue;
735 }
736
737 var pp3 = path.shift();
738 var p3=pp3.split(',');
739 if(p3.length == 1){
740 cmd=p3[0];
741 continue;
742 }
743 ctx.bezierCurveTo(p[0], p[1], p2[0], p2[1], p3[0], p3[1]);
744
745 }else if (cmd == "c"){
746 }else if (cmd == "S"){
747 }else if (cmd == "s"){
748 }else if (cmd == "Q"){
749 var pp2 = path.shift();
750 var p2=pp2.split(',');
751 if(p2.length == 1){
752 cmd=p2[0];
753 continue;
754 }else{
755 ctx.quadraticCurveTo(p[0], p[1], p2[0], p2[1]);
756 }
757 }else if (cmd == "q"){
758 }else if (cmd == "T"){
759 }else if (cmd == "t"){
760 }else if (cmd == "A"){
761 }else if (cmd == "a"){
762 }else{
763 }
764 }
765
766 ctx.stroke();
767 ctx.closePath();
768 break;
769
770 case 'RECT':
771 var px = ele[i].getAttribute("x");
772 var py = ele[i].getAttribute("y");
773 var w = ele[i].getAttribute("width");
774 var h = ele[i].getAttribute("height");
775
776 ctx.beginPath();
777 ctx.setTransform(1, 0, 0, 1, 0, 0);
778 if(color)ctx.strokeStyle = color;
779 if(width) ctx.lineWidth = width;
780
781 if(!fill || fill == 'none'){
782 ctx.strokeRect(px, py, w, h);
783 }else if( fill == 'clear'){
784 ctx.clearRect(px, py, w, h);
785 }else{
786 ctx.fillStyle = fill;
787 ctx.fillRect(px, py, w, h);
788 if(color) ctx.strokeRect(px, py, w, h);
789 }
790 ctx.closePath();
791 break;
792
793 case 'CIRCLE':
794 var cx = ele[i].getAttribute("cx");
795 var cy = ele[i].getAttribute("cy");
796 var r = ele[i].getAttribute("r");
797 ctx.beginPath();
798 ctx.setTransform(1, 0, 0, 1, 0, 0);
799 circle(ctx, cx, cy, r, color, fill, width);
800 ctx.closePath();
801 break;
802
803 case 'ELLIPSE':
804 var cx = parseInt(ele[i].getAttribute("cx"));
805 var cy = parseInt(ele[i].getAttribute("cy"));
806 var rx = parseInt(ele[i].getAttribute("rx"));
807 var ry = parseInt(ele[i].getAttribute("ry"));
808
809 ctx.beginPath();
810 ctx.setTransform(1, 0, 0, 1, 0, 0);
811 ellipse(ctx, cx, cy, rx, ry, color, fill, width);
812 ctx.closePath();
813 break;
814
815 defalut:
816 break;
817 }
818 if(ele[i].hasChildNodes()){
819 drawAll(ctx, ele[i].childNodes);
820 }
821 }
822
823 }
824
825 function circle(ctx, cx, cy, r, color, fill, width){
826 if(color)ctx.strokeStyle = color;
827 if(width) ctx.lineWidth = width;
828 if(!fill || fill == 'none'){
829 ctx.arc(cx, cy, r, 0, Math.PI *2 ,true);
830 ctx.stroke()
831 }else{
832 ctx.fillStyle = fill;
833 ctx.arc(cx, cy, r, 0, Math.PI *2 ,true);
834 ctx.fill()
835 if(color){
836 ctx.arc(cx, cy, r, 0, Math.PI *2 ,true);
837 ctx.stroke()
838 }
839 }
840 }
841
842 function ellipse(ctx, cx, cy, rx, ry, color, fill, width) {
843 if(color)ctx.strokeStyle = color;
844 if(width) ctx.lineWidth = width;
845 if(!fill || fill == 'none'){
846 draw_ellipse(ctx, cx, cy, rx, ry, color, fill, width);
847 ctx.stroke()
848 }else{
849 ctx.fillStyle = fill;
850 draw_ellipse(ctx, cx, cy, rx, ry, color, fill, width);
851 ctx.fill()
852 if(color){
853 draw_ellipse(ctx, cx, cy, rx, ry, color, fill, width);
854 ctx.stroke()
855 }
856 }
857 }
858
859 function draw_ellipse(ctx, x, y, w, h) {
860 var kappa = .5522848;
861 ox = w * kappa , // control point offset horizontal
862 oy = h * kappa , // control point offset vertical
863 xe = x + w*2, // x-end
864 ye = y + h*2, // y-end
865 xm = x + w , // x-middle
866 ym = y + h ; // y-middle
867
868 ctx.moveTo(x-w, ym-h);
869 ctx.bezierCurveTo(x-w, ym - oy-h, xm - ox-w, y-h, xm-w, y-h);
870 ctx.bezierCurveTo(xm + ox-w, y-h, xe-w, ym - oy-h, xe-w, ym-h);
871 ctx.bezierCurveTo(xe-w, ym + oy-h, xm + ox-w, ye-h, xm-w, ye-h);
872 ctx.bezierCurveTo(xm - ox-w, ye-h, x-w, ym + oy-h, x-w, ym-h);
873 }
874
875 function polyline(ctx, pts, color, fill, width, isClose){
876 var points = pts.split(' ');
877
878 if(points.length < 2) return false;
879
880 var tmp = points.shift();
881
882 var p = tmp.split(',');
883
884 ctx.beginPath();
885 ctx.setTransform(1, 0, 0, 1, 0, 0);
886 ctx.strokeStyle=color;
887 ctx.lineWidth=width;
888 ctx.moveTo(p[0], p[1]);
889
890 for(var i=0; i <points.length; i++){
891 tmp = points[i];
892 p = tmp.split(',');
893 ctx.lineTo(p[0], p[1]);
894 }
895
896 if (isClose) ctx.closePath();
897
898 if (fill && fill != 'none'){
899 ctx.fillStyle=fill;
900 ctx.fill();
901 }
902 ctx.stroke();
903 }
904
905 function svg_image(ctx,iname, x, y, w, h){
906 if (w == 0 || h == 0) return;
907
908 var img = new Image();
909 img.src = iname;
910
911 img.onload = function (){
912 if (w < 0 || h < 0){
913 ctx.drawImage(img, x, y);
914 }else{
915 ctx.drawImage(img, x, y, w, h);
916 }
917 }
918 }
919
920 function toSVGElement(str, w, h){
921 var xmlsvg = "xmlns:svg=\"http://www.w3.org/2000/svg\""
922 var parser = new DOMParser();
923 var header = "<svg:svg width=\""+w+"\" height=\""+h+"\" "+xmlsvg+">"
924 var footer = "</svg:svg>";
925 var xmlDoc = parser.parseFromString(header+str+footer, "text/xml");
926 var ele = document.importNode(xmlDoc.documentElement,true);
927 return ele;
928 }
929
930 ///////
931 window.addEventListener('orientationchange', updateOrientation, false);

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26