最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

adding image to svg file using javascript and jquery - Stack Overflow

programmeradmin0浏览0评论

I'm trying to add an image to an SVG file.. I tried this code::

drawImage : function(src, x, y, h, w) {
        var img = document.createElementNS("", "image");
        var $img = $(img);
        $img.attr('x', x);
        $img.attr('y', y);
        $img.attr('width', w);
        $img.attr('height', h);
        $img.attr('xlink:href', src);
        $('g').append($img);
    }

but the image does not appear. when I select all images.. using $('image') I find the image, and I can select it, but still.. it is not visible. I tried to investigate this issue, but found nothing. what am I doing wrong??

I'm trying to add an image to an SVG file.. I tried this code::

drawImage : function(src, x, y, h, w) {
        var img = document.createElementNS("http://www.w3/2000/svg", "image");
        var $img = $(img);
        $img.attr('x', x);
        $img.attr('y', y);
        $img.attr('width', w);
        $img.attr('height', h);
        $img.attr('xlink:href', src);
        $('g').append($img);
    }

but the image does not appear. when I select all images.. using $('image') I find the image, and I can select it, but still.. it is not visible. I tried to investigate this issue, but found nothing. what am I doing wrong??

Share Improve this question asked Apr 19, 2012 at 20:13 zeacusszeacuss 2,6332 gold badges29 silver badges32 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

You can use the setAttributeNS method:

var img = document.createElementNS('http://www.w3/2000/svg','image');
img.setAttributeNS(null,'height','200');
img.setAttributeNS(null,'width','200');
img.setAttributeNS(null,'id','theID');
img.setAttributeNS('http://www.w3/1999/xlink','href','src');
img.setAttributeNS(null,'x','0');
img.setAttributeNS(null,'y','0');
$('#g').append(img);

How about assigning a width and height to $('g'), which is holding the $img in your code?

I found a jQuery plugin that solved my problem jQuery.svg

it has several methods to draw different shapes, including images.

It seems that it adds images by creating a dom node using native JavaScript.. and it works

发布评论

评论列表(0)

  1. 暂无评论