简体   繁体   English

Internet Explorer中损坏的d3.js动画

[英]Broken d3.js animation in Internet Explorer

I have a d3.js animation that works magnificently in all other browsers except Explorer. 我有一个d3.js动画在除Explorer之外的所有其他浏览器中都很有效。 The desired effect is for it to be centered while it grows and shrinks, then cycles again, ad infinitum. 期望的效果是它在生长和收缩时居中,然后再次循环,无限期。 I made a jsfiddle here . 我在这里做了一个小提琴。 The problem is that the animation goes off the screen as well as loses it's center point in the middle of the screen. 问题是动画离开屏幕并且在屏幕中间丢失了它的中心点。

After, comparing the svg in the DOM in IE11 and Chrome, I see that scale() in IE11 only has one value "scale(x)" instead of two "scale(x, y)". 之后,比较IE11和Chrome中DOM的svg,我看到IE11中的scale()只有一个值“scale(x)”而不是两个“scale(x,y)”。 I'm sure that's just one of a few issues. 我确信这只是一些问题之一。

var duration = 5000;

var start = 50,
startScale = 1,
endScale = 10,
startTranslate = 225,
endTranslate = 0;

var startTrans = "scale(" + startScale + "," + startScale + ")translate(" + startTranslate + "," + startTranslate + ")",
endTrans = "scale(" + endScale + "," + endScale + ") translate(" + endTranslate + "," + endTranslate + ")";

d3.select("#slider_td").append("input")
    .attr("type", "range")
    .attr("id", "slider")
    .attr("min", 3000)
    .attr("max", 7000)
    .attr("value", duration)
    .on("change", function () {
    duration = this.value;
    d3.select("#_rb").property("value", d3.round(duration / 1000));
});

var svg = d3.select("#animation_td").append("svg")
    .attr("width", 500)
    .attr("height", 500)
    .style("background-color", "#ADD9F7");

svg.append("use")
    .attr("xlink:href", "#Livello_1")
    .attr("width", start)
    .attr("height", start)
    .attr("transform", startTrans)
    .call(transition, startTrans, endTrans);

svg.append("circle")
    .attr("cx", 250)
    .attr("cy", 730)
    .attr("r", 350)
    .style("fill", "#99CC00")
    .style("stroke", "white");

function transition(element, start, end) {
    element.transition()
        .duration(duration)
        .attr("transform", end)
        .each("end", function () {
        d3.select(this).call(transition, end, start);
    });
}

The problem seems to be that IE is ignoring the width and height attributes on the <use> element. 问题似乎是IE忽略了<use>元素的width和height属性。

For comparison, see this version of the fiddle where I've removed those attributes (and also the animation) -- you'll get the same off-center positioning in the other browsers as in IE. 为了比较,看到这个版本的小提琴 ,我已经删除了这些属性(以及动画) - 你将获得与IE中其他浏览器相同的偏心定位。

This is a bug, and one I hadn't come across yet. 这是一个错误,我还没有遇到过。 Nor could I find anything on their bug report site . 我也无法在他们的错误报告网站上找到任何内容。

You might want to put together a simpler example and make the bug report, in the meantime here's a workaround: 您可能希望将更简单的示例放在一起并制作错误报告,同时这是一个解决方法:

The effect of width and height attributes on a <use> element is to scale the referenced graphic to that size. width和height属性对<use>元素的影响是将引用的图形缩放到该大小。 It only has an effect when the referenced graphic is a <symbol> or <svg> with a viewBox attribute. 仅当引用的图形是具有viewBox属性的<symbol><svg>时,它才有效。

Therefore, my first guess was that you should be able to make the adjustments yourself by (a) removing the width and height attributes on the <use> element, and (b) adding an additional scale factor: 因此,我的第一个猜测是您应该能够通过以下方法自行进行调整:(a)删除<use>元素上的width和height属性,以及(b)添加额外的比例因子:

var basicScale = 50/557.8,   
    //50px is the desired width
    //557.8px is the width and height of the original graphic
    startScale = 1,
    endScale = 10,
    startTranslate = 225,
    endTranslate = 0;

var startTrans = "scale(" + startScale + ") translate(" 
                  + startTranslate + "," + startTranslate + ") scale(" 
                  + basicScale + ")",
    endTrans = "scale(" + endScale + ") translate(" 
                  + endTranslate + "," + endTranslate + ") scale(" 
                  + basicScale + ")";

http://jsfiddle.net/U9L7w/5/ http://jsfiddle.net/U9L7w/5/

Unfortunately, while the above example is a significant improvement, it isn't quite right -- the sun and the hill aren't centered one above the other in IE, even though they are with the exact same code in Firefox and Chrome. 不幸的是,虽然上面的例子是一个重大的改进,但它并不完全正确 - 在IE中,太阳和山丘并不是一个在另一个上面的中心,即使它们在Firefox和Chrome中具有完全相同的代码。

It seems that IE doesn't just ignore the width and height attributes, it applies a default value of 100% -- meaning it scales the referenced SVG to be the same size as the parent SVG (500px width and height in your example) before applying any transformations. IE似乎不仅忽略宽度和高度属性,它应用默认值100% - 这意味着它将引用的SVG缩放为与父SVG(在示例中为500px宽度和高度)相同的大小应用任何转换。

So to get things to work nicely in all browsers, you'll need to (a) tell the other browsers to scale to 100% using the width and height attributes, then (b) factor in that scaling factor when you create your new scaling factor: 因此,为了让所有浏览器都能很好地工作,你需要(a)告诉其他浏览器使用width和height属性缩放到100%,然后(b)在创建新缩放时考虑缩放因子因子:

/* To adjust for a lack of IE support for width and height on <use>, 
   add in an extra scale transform.  For other browsers,  
   set the 100% width and height explicitly on the <use> element   
   to match IE's behaviour.
*/
var basicScale = 50/500,  
    //50px is the desired width
    //500px is the width and height of the referencing graphic
    startScale = 1,
    endScale = 10,
    startTranslate = 225,
    endTranslate = 0;

var startTrans = "scale(" + startScale + ") translate(" 
                  + startTranslate + "," + startTranslate + ") scale(" 
                  + basicScale + ")",
    endTrans = "scale(" + endScale + ") translate(" 
                  + endTranslate + "," + endTranslate + ") scale(" 
                  + basicScale + ")";

/* ... */

svg.append("use")
    .attr("xlink:href", "#Livello_1")
    .attr("width", "100%")
    .attr("height", "100%")
    .attr("transform", startTrans)
    .call(transition, startTrans, endTrans);

http://jsfiddle.net/U9L7w/6/ http://jsfiddle.net/U9L7w/6/

It's a rather round-about way of getting to the end result for browsers that implement the specs properly, but you get the desired result in both them and in IE. 对于正确实现规范的浏览器来说,这是一种相当全面的方法,但是在IE和IE中都能得到理想的结果。

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

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