在raphael.js中缩放填充图案

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画布没有按比例缩放,因此在路径缩放后,填充物的大小会变为原来的两倍。 有没有简单的方法可以使填充图案与SVG的其余部分一起缩放呢?

2个回答

7

仅使用raphael库的函数是不可能做到的。

当您在raphael对象上应用比例函数时,它会创建一个新的svg节点,并缩放坐标,但不幸的是,它不会修改填充属性。

无论如何,当您添加带有url的属性“fill”时,库会创建一个模式。如果这是您使用的第一个“fill”属性,则此模式称为raphael-pattern-0,下一个称为raphael-pattern-1,依此类推)

了解这一点后,就可以根据SVG规范更改模式的属性

您可以使用document.getElementById("raphael-pattern-0")获取模式的属性,并使用setAttribute更改其属性。例如(取决于您真正想要做什么),可以像以下示例一样:

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

您还可以修改xypatternUnitspatternContentUnits属性。

希望这能回答您的问题。


3

我不知道为什么,但我的Raphael库版本(我使用最新版本)没有像@ThibThib描述的那样添加ID。这可能是因为现在我们已经到了2013年 :)

我也会发布我的解决方案:

<!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>

注意以下几点:
  • 访问VML填充图像在此处描述:如何访问VML中的Raphael填充图像

  • 更改图像大小后,我必须为VML版本重置描边

  • 由于某种原因,对我来说jQuery .attr无法正常工作,因此我使用了setAttribute(Chrome)

  • 我设置了preserveAspectRatio以实现与VML相同的效果。如果您想查看差异,则可以禁用它(请参阅文档

  • setTimeout用于等待图像加载完成,因为SVG在加载图像后设置参数,并且会覆盖我的大小更改

当然,您也可以尝试不同的设置:

VML填充元素文档

SVG模式

SVG图像元素


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接