SVG背景可以互动吗?

时间:2013-04-07 06:52:44

标签: javascript css html5 dom svg

我创建了一个SVG图像。它是一个内有圆圈的矩形。圆圈使用JavaScript跟随用户鼠标。图像由以下代码表示:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlspace="preserve" preserveAspectRatio="xMidYMin slic">
<style>
    * { vector-effect:non-scaling-stroke }
    rect { fill: blue; }
    circle { fill:orange; opacity:0.75; }
</style>
<rect cx="50%" cy="0" width="720" height="1278" id="origin" />
<circle cx="50%" cy="116" r="72" id="dot"    />
<script>
    var svg  = document.documentElement,
        pt   = svg.createSVGPoint(),
        dot  = document.querySelector('#dot');

    svg.addEventListener('mousemove',function(evt){
      var loc = cursorPoint(evt);
        dot.setAttribute('cx',loc.x);
        dot.setAttribute('cy',loc.y);
    },false);

    function rotateElement(el,originX,originY,towardsX,towardsY){
        var degrees = Math.atan2(towardsY-originY,towardsX-originX)*180/Math.PI + 90;
        el.setAttribute(
            'transform',
            'translate('+originX+','+originY+') translate('+(-originX)+','+(-originY)+')'
        );
    }

    // Get point in global SVG space
    function cursorPoint(evt){
      pt.x = evt.clientX; pt.y = evt.clientY;
      return pt.matrixTransform(svg.getScreenCTM().inverse());
    }
</script> 
</svg>

我想用这张图片做的是用它作为CSS背景。如果我使用CSS将图像设置为背景{background: url("image.svg");},则JavaScript不再有效,即圆圈不再跟随光标。我相信这是因为当图像是背景时,它上面堆叠了其他元素。

那么我怎样才能让图像成为背景并保持互动?任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:4)

使用CSS4 element()获取脚本化背景SVG的方法之一。它目前仅通过-moz-element()在Firefox 4+中实现。

An example:

<div id="bg" style="width: 400px; height: 400px;">
    <svg width="400" height="400" viewPort="0 0 400 400">
        <!-- force correct 0,0 coordinates -->
        <rect x="0" y="0" width="1" height="1" fill="transparent" />
        <rect x="0" y="0" id="animable1" width="120" height="120" fill="blue" />
        <rect x="0" y="0" id="animable2" width="60" height="60" fill="red" />
    </svg>
</div>

<div id="target" style="border: 4px dashed black; height: 400px; width: 400px; background: gray -moz-element(#bg); background-size: 20%;"></div>

<script type="text/javascript">
    var divTarget = document.getElementById("target");
    var animable1 = document.getElementById("animable1");
    var animable2 = document.getElementById("animable2");
    document.addEventListener("mousemove", function(event){
        var rotation = Math.atan2(event.clientY, event.clientX);
        animable1.setAttribute("transform", "translate(140 140) rotate(" + (rotation / Math.PI * 360) + " 60 60)");
        animable2.setAttribute("transform", "translate(170 170) rotate(" + (360 - rotation / Math.PI * 360) + " 30 30)");
    }, false);
    animable1.setAttribute("transform", "translate(140 140) rotate(0 60 60)");
    animable2.setAttribute("transform", "translate(170 170) rotate(0 30 30)");
</script>

答案 1 :(得分:0)

你应该创建一个.css文件的两个文件,另一个是当然,最好有单独的文件,因为它实际上是容器的一部分是html。这个容器中的SVG是JavaScripts。这种分而治之的简化或事件。因此,外部文件实际上被保存为JavaScripts。这是一种不会在代码中松散的好方法。

SVG定义:

<div><object id="circle-svg" width="400" height="300" type="image/svg+xml" data="moving_circle.svg"></object></div>

这里,您描述自己的SVG的数据部分

例如,canvas:

            canvas = d3.select("#circle-svg")
                    .on("mouseover", mouseover)
                    .on("mousemove", mousemove)
                    .on("mouseout", mouseout);

1部分: 看一下这段代码(基于html)

<head>
<title>Controlling an SVG with Javascript</title>
<script type='text/javascript' src='svg-interaction.js'></script>
   <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>

           <style type="text/css">
            div.tooltip {
                    position: absolute;
                      text-align: center;
                    z-index: 10;
                      width: 140px;
                      height: auto;
                    padding: 8px;
                    font: 24px sans-serif;
                    background: red;
                    color: white;
                    border: solid 1px #aaa;
                    border-radius: 8px;
                    opacity: 0;
            }
    </style>
     <head>

    <body>
    <h2>Controlling SVG with Javascript</h2>
     <div class="page-content">
      <div><object id="circle-svg" width="400" height="300" type="image/svg+xml" data="moving_circle.svg"></object></div>

您可以在那里定义脚本

然后你继续第二阶段(你的SVG研究)

<svg viewBox="0 0 400 400" preserveAspectRatio="none"
 xmlns="http://www.w3.org/2000/svg"
 xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
 a3:scriptImplementation="Adobe"
 onload="init(evt)"
 onzoom="updateTracker(evt)"
 onscroll="updateTracker(evt)"
 onresize="updateTracker(evt)">
<script type="text/ecmascript" a3:scriptImplementation="Adobe"><![CDATA[
    /*****
    *
    *   Globals
    *
    *****/
    var elems = {
        tracker: false,
        cursor:  false,
        trans:   true,
        scale:   true,
        mx:      true,
        my:      true,
        ux:      true,
        uy:      true
    };
    var frame = {
        x_trans: 0,
        y_trans: 0,
        zoom   : 1,
        x_scale: 1,
        y_scale: 1
    };