2009-07-08 1 views
6
paper=Raphael('previewBody',480,480); 
paper.path({"stroke-width":1},'M0,0 L480,240 L480,480 L240,480 z') 
    .attr('fill','url(bg.png)')) 
    .scale(.5,.5,0,0); 

私の問題は、塗りつぶしがsvgキャンバスでスケーリングされていないことになります。比例して、パスのスケールの2倍のサイズになります。 残りのsvgといっしょに塗りつぶしパターンをスケールする簡単な方法はありますか?raphael.jsでの塗りつぶしパターンのスケーリング

答えて

7

これは、グラフィカルライブラリの機能のみを使用して行うことができます。 「あなたはラファエロのオブジェクトのスケール関数を適用すると座標がスケールで

は、それが、新しいSVGノードを作成していますが、属性を追加する場合、残念ながら、それは、

とにかく塗りつぶしのプロパティを変更しません。 URLで「塗りつぶす」と、ライブラリはパターンを作成します。 それはこのことを知って、あなたが使用して属性、このパターンは、次のいずれかがraphael-pattern-1呼ばれraphael-pattern-0と呼ばれている、など...)

を「埋める」最初である場合、パターンの属性を変更することが可能です

var pat = document.getElementById("raphael-pattern-0"); 
pat.setAttribute("height", pat.getAttribute("height")*0.5); 
pat.setAttribute("width", pat.getAttribute("width")*0.5); 
SVG specifications

に応じてあなたがdocument.getElementById("raphael-pattern-0")で、パターンの属性を取得することができますし、(あなたが本当に何をしたいかに応じて)例えばsetAttribute とそのプロパティを変更、それはのようなものかもしれません

また、x,y,patternUnitsおよびpatternContentUnitsのプロパティを変更することもできます。

ご質問にお答えします。

3

なぜ私のラファエルライブラリ(私は最新のものを使用しています)のIDは@ThibThibと記述されていません。 How to access Raphael fill image in VML

:ここで説明されてVMLは画像を埋めるアクセス

  • <!DOCTYPE html> 
    <html lang="en"> 
        <head> 
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
         <meta charset="utf-8"> 
         <title>Raphael Test</title> 
    
         <script type="text/javascript" src="js/libs/jquery.js"></script> 
         <script type="text/javascript" src="js/libs/raphael.js"></script> 
        </head> 
        <body> 
         <div id="raphael"></div> 
         <script type="text/javascript"> 
          $(document).ready(function() { 
    
           var raphael, path, pattern; 
    
           raphael = Raphael(document.getElementById('raphael'), 500, 500); 
    
           path = raphael.circle(200, 200, 180); 
           path.attr('stroke', '#000000'); 
           path.attr('stroke-width', 3); 
           path.attr('stroke-opacity', 1); 
           path.attr('fill', 'url(http://3.bp.blogspot.com/_M4WdA9j-b-g/TLMF9JJJwcI/AAAAAAAAAV4/p0Y_Wo3S3sQ/s1600/Landscape1.jpg)'); 
           pattern = $(path.node).attr('fill'); 
           if(pattern) { 
            pattern = pattern.replace('url(', '').replace(')', ''); 
            pattern = $(pattern); 
           } 
    
           // Shape element documentation: http://msdn.microsoft.com/en-us/library/hh846327(v=vs.85).aspx#shape_element 
           // Fill element documentation: http://msdn.microsoft.com/en-us/library/bb229596(v=vs.85).aspx 
    
           setTimeout(function() { 
            if(Raphael.vml) { // SVG not supported (IE7 & IE8) and it doesn't work :/ 
    
             var html = $(path.node).html(); 
             html = html.replace(/rvml:/g, ''); // remove prefix 
             html = html.replace(/ = /g, '='); 
             html = html.substr(html.indexOf('/>') + 2); // remove xml element 
    
             var src = ''; 
             $(html).each(function() { 
              if($(this).prop("tagName") == 'FILL') { 
               src = $(this).attr('src'); 
              } 
             }); 
    
             if(src != '') { 
              var html = $(path.node).html(); 
              html = html.replace(src, src + '" size="50,50'); 
              $(path.node).html(html); 
              path.attr('stroke', '#000000'); 
              path.attr('stroke-width', 3); 
              path.attr('stroke-opacity', 1); 
             } 
            } 
            else { // SVG supported and it prints correct URL 
             $(pattern)[0].setAttribute('width', 50); 
             $(pattern)[0].setAttribute('height', 50); 
             $(pattern).find('image')[0].setAttribute('width', 50); 
             $(pattern).find('image')[0].setAttribute('height', 50); 
             $(pattern).find('image')[0].setAttribute('preserveAspectRatio', 'defer none meet'); 
            } 
           }, 1000); 
          }); 
         </script> 
        </body> 
    </html> 
    

    お知らせいくつかのこと:私たちは今:)

    私も私の解決策を掲載する予定2013年を持っているので、それはおそらくです

  • イメージのサイズを変更した後、VMLバージョンのストロークをリセットする必要がありました

  • 私はVMLと同様の効果を達成するために preserveAspectRatioを設定jQueryの .attrが私のために動作しませんでしたので、私は setAttributeを使用し、いくつかの理由(クローム)

  • 。あなたは違い(see documentation)を参照したい場合は、画像が読み込まれた後SVGはのparamsを設定したとして、イメージがロードされるのを待つために使用され、そしてそれは私のサイズ変更に

を上書きした setTimeout

  • それを無効にすることができます

    あなたはもちろん、同様に異なる設定で遊ぶことができます:

    VML Fill element documentation

    SVG Patterns

    SVG Image element