用js在svg图上的上动态添加一张任意格式的图片 5
1个回答
展开全部
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script type="text/javascript">
function init() {
var svg_obj = document.getElementById("svg_obj");
svg_obj.onclick = function(event) {
var xmlns = "http://www.w3.org/2000/svg";
var tsvg_obj = document.getElementById("svg_obj");
var svg_img = document.createElementNS(xmlns, "image");
svg_img.href.baseVal = "example.jpg";
svg_img.setAttributeNS(null, "x", event.pageX);
svg_img.setAttributeNS(null, "y", event.pageY);
svg_img.setAttributeNS(null, "height", "16px");
svg_img.setAttributeNS(null, "width", "16px");
tsvg_obj.appendChild(svg_img);
}
}
</script>
</head>
<body onload="init();">
<svg id="svg_obj" version="1.1"
xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg>
</body>
</html>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script type="text/javascript">
function init() {
var svg_obj = document.getElementById("svg_obj");
svg_obj.onclick = function(event) {
var xmlns = "http://www.w3.org/2000/svg";
var tsvg_obj = document.getElementById("svg_obj");
var svg_img = document.createElementNS(xmlns, "image");
svg_img.href.baseVal = "example.jpg";
svg_img.setAttributeNS(null, "x", event.pageX);
svg_img.setAttributeNS(null, "y", event.pageY);
svg_img.setAttributeNS(null, "height", "16px");
svg_img.setAttributeNS(null, "width", "16px");
tsvg_obj.appendChild(svg_img);
}
}
</script>
</head>
<body onload="init();">
<svg id="svg_obj" version="1.1"
xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg>
</body>
</html>
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询