简体   繁体   English

IE9上基于JavaScript的SVG渐变操作

[英]JavaScript based SVG gradient manipulation on IE9

I have an SVG file which specifies a gradient and a circle like below. 我有一个SVG文件,它指定了一个渐变和一个如下所示的圆。 The embedded script toggles the orientation of the gradient onClick(), which works in all current browsers except IE9. 嵌入式脚本可切换渐变onClick()的方向,该梯度可在除IE9之外的所有当前浏览器中使用。 My suspicion is that IE does not redraw the gradient. 我怀疑IE不会重绘渐变。 I tried a few things, such as setting the fill to a solid colour first and thenm reassign the (altered) gradient, to trigger a redraw but no dice so far. 我尝试了一些操作,例如首先将填充设置为纯色,然后nm重新分配(更改的)渐变,以触发重绘,但到目前为止没有骰子。 My question is, does anybody how I can work around that issue or, even better, solve it. 我的问题是,有人能解决这个问题,甚至更好地解决它吗? Thanks. 谢谢。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
  <![CDATA[
    function flipGrad(evt) {
      var g=document.getElementById('grad1');
      var y1 = g.getAttribute('y1');
      var y2 = g.getAttribute('y2');
      g.setAttribute('y2', y1);
      g.setAttribute('y1', y2);
    }
   ]]>
  </script>
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <circle cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#grad1)" onclick="flipGrad(evt)" />
</svg>

EDIT: 编辑:

Editing the stops help, so that might become workable. 编辑停靠点帮助,使它变得可行。 Truth is though, my actual file looks more like the following, which is Inkscape svg output, where gradients are split into colour sections and geometry sections and only the geometry is linked to from and object but the colour is reused in multiple other gradients. 事实是,我的实际文件看起来更像以下内容,即Inkscape svg输出,其中将渐变分为颜色部分和几何部分,并且仅将几何体链接到from和object,但是颜色在其他多个渐变中重复使用。 The approach to swap the stops would effect all objects linking to that color gradient: 交换停靠点的方法将影响链接到该颜色渐变的所有对象:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
  <![CDATA[
    function flipGrad(evt) {
      // var g=document.getElementById('gradGeometry');
      // var y1 = g.getAttribute('y1');
      // var y2 = g.getAttribute('y2');
      // g.setAttribute('y2', y1);
      // g.setAttribute('y1', y2);\
      var s1=document.getElementById('stop1');
      var s2=document.getElementById('stop2');
      var s1s = s1.getAttribute('style');
      var s2s = s2.getAttribute('style');
      s1.setAttribute('style', s2s);
      s2.setAttribute('style', s1s);
    }
   ]]>
  </script>
  <defs>
    <linearGradient id="gradColour">
      <stop id="stop1" offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop id="stop2" offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
    <linearGradient id="gradGeometry1" x1="0%" y1="0%" x2="0%" y2="100%" xlink:href="#gradColour" />
    <linearGradient id="gradGeometry2" x1="0%" y1="0%" x2="100%" y2="0%" xlink:href="#gradColour" />
  </defs>
  <circle cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#gradGeometry1)" onclick="flipGrad(evt)" />
  <circle cx="90" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#gradGeometry2)" onclick="flipGrad(evt)" />
</svg>

After testing a bit on IE9, it seems that the gradient vector is immutable once defined in IE. 在IE9上进行了一些测试之后,似乎梯度向量一旦在IE中定义,便是不可变的。 Your only choice is to use id'd gradient stops, which are mutable. 您唯一的选择是使用可变的id渐变挡块。

<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
  <![CDATA[
    function flipGrad(evt) { 
      var s1=document.getElementById('stop1');
      var s2=document.getElementById('stop2');
      var s1s = s1.getAttribute('style');
      var s2s = s2.getAttribute('style');
      s1.setAttribute('style', s2s);
      s2.setAttribute('style', s1s);
    }
   ]]>
  </script>
  <defs>
      <linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
        <stop id="stop1" offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
        <stop id="stop2" offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>

  <circle id="bubble" cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#grad1)" onclick="flipGrad(evt)" />
</svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM