Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /iSlide.js

Parent Directory Parent Directory | Revision Log Revision Log


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

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