如何将图案图像动态添加到 svg?

Ale*_*bal 5 html javascript css jquery svg

我是 SVG 图像的新手。我正在使用它们为夹克绘制多次不同颜色的颜色,而无需为每种颜色绘制图像。这是通过 jQuery 完成的。

那是“容易”的部分。我通过将fill: #color;CSS 规则应用于<path>inside解决了这个问题<svg>

困难的部分是尝试用图像填充 svg 路径时。这真的很奇怪。我的代码在 内的 html 中打印得很好<svg>,但根本不起作用。当我在 chrome 的开发工具中剪切<defs>元素,然后将其准确粘贴到原来的位置时,它突然起作用了!这让我疯狂 :(。

我的代码:

var items = [
            {
                title: 'Africa',
                // color: '#776254'
            },
            {
                title: 'Aguamarina',
                // color: '#9cd3be'
            },
            {
                title: 'Aluminio',
                // color: '#9b9b9b'
            },
            {
                title: 'Amarillo Jamaica',
                // color: '#ffcd01'
            },
            {
                title: 'Amatista',
                // color: '#4d4169'
            },
            {
                title: 'Ambar Brillante',
                // color: '#eb6608'
            },
            {
                title: 'Arándano',
                // color: '#604483'
            }
        ];

        $(function () {
            var PrendaShow = {
                $mac_wrapper: $('#prendas-mac-slider-wrapper .mac-slider'),
                $fullprints_wrapper: $('#fullprints-gallery'),
                img: {
                    src: 'image.svg',
                    width: 350,
                    height: 188
                },
                init: function () {
                    this.makeItems();
                    this.bindEvents();
                },
                bindEvents: function () {
                    // events
                },
                makeItems: function() {
                    var self = this,
                        $model = $('<div/>', { class: 'mac-item' });
                    $model.append($('<div/>', { class: 'item-img' }));
                    $.each(items, function() {
                         var $item = $model.clone();
                        self.$mac_wrapper.append($item);
                    });
                    this.svgDraw();
                },
                svgDraw: function () {
                    var self = this;
                    $.get(this.img.src, function(data) {
                        var $svg = self.normalizeSvg(data);
                        self.appendImgs($svg);
                    });
                },
                normalizeSvg: function (data) {
                    var $svg = $(data).find('svg');
                    $svg = $svg.removeAttr('xmlns:a');
                    $svg.width(this.img.width).height(this.img.height);
                    return $svg;
                },
                appendImgs: function ($svg) {
                    var items = this.$mac_wrapper.find('.mac-item');
                    $.each(items, function() {
                        var $clone = $svg.clone(),
                            color = $(this).data('color');
                        $clone.find('path').css('fill', 'url(#img1)');
                        var img = document.createElementNS('http://www.w3.org/2000/svg', 'image');
                        img.setAttributeNS(null,'height','536');
                        img.setAttributeNS(null,'width','536');
                        img.setAttributeNS('http://www.w3.org/1999/xlink','href','http://www.boogdesign.com/examples/svg/daisy-grass-repeating-background.jpg');
                        img.setAttributeNS(null,'x','10');
                        img.setAttributeNS(null,'y','10');
                        img.setAttributeNS(null, 'visibility', 'visible')
                        $clone.prepend($('<defs/>').html('<pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100"></pattern>'));
                        $clone.find('pattern').append(img)
                        $(this).find('.item-img').append($clone);
                    });
                }
            };
            PrendaShow.init();
        });
Run Code Online (Sandbox Code Playgroud)
#prendas-mac-slider-wrapper {
  margin-top: 80px;
}
.mac-slider {
  display: flex;
  font-size: 0;
  padding: 0 100px;
}
.mac-item {
  width: 100%;
  height: 160px;
  min-width: 0;
  position: relative;
  display: inline-block;
  text-align: center;
  cursor: pointer;
}
.item-img {
  background: url('https://ae01.alicdn.com/kf/HTB1Cqi6KXXXXXbpXVXXq6xXFXXXy/Classic-font-b-White-b-font-font-b-Jacket-b-font-font-b-Men-b-font.jpg') center center no-repeat;
  background-size: contain;
  pointer-events: none;
  position: absolute;
  top: 0;
  left: -9999px;
  right: -9999px;
  margin: auto;
}
svg {
  mix-blend-mode: multiply;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hero">
        <div class="container-fluid">
            <div id="prendas-mac-slider-wrapper" class="row">
                <div class="mac-slider">
                </div>
            </div>
        </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

由于图像上传,我无法给出我的确切示例。我真的希望你明白我在这里想要做什么,以及出了什么问题。


编辑:不确定这是否值得在答案中,但我更喜欢在这里写它,因为我认为这不是正确的方法。

据我所知,从 chrome 开发工具中,如果我删除该<defs>元素并再次粘贴它,它会起作用,我尝试了这个并起作用:

var svg_html = $svg.html();
$svg.html('');
$svg.html(svg_html);
Run Code Online (Sandbox Code Playgroud)

Pau*_*eau 0

你的问题在这里:

$clone.prepend($('<defs/>').html('<pattern ...
Run Code Online (Sandbox Code Playgroud)

您不能使用 jQuery 创建这样的 SVG 元素。createElementNS()当您创建元素时,您正在以正确的方式(使用 )执行此操作<image><defs>但创建和 <元素时也需要使用相同的方法pattern>