简体   繁体   English

在 chrome 中使用 javascript 设置 SVG 属性

[英]Setting SVG Attributes with javascript in chrome

Maybe this is a bug with Chrome (88.0.4324.150) svg rendering, it appears reprodutible only in this browser.也许这是 Chrome (88.0.4324.150) svg 渲染的错误,它似乎只能在此浏览器中重现。 But maybe I am doing something wrong?但也许我做错了什么?

When I set the fill-mode attribute dynamically and repeatedly with javascript by using setAttribute, it is not always visible applied although correctly shown in the inspector.当我使用 setAttribute 使用 javascript 动态且重复地设置填充模式属性时,尽管在检查器中正确显示,但它并不总是可见应用。 If the window getsa redraw eg.如果 window 得到重绘,例如。 through resizing, it suddenly gets applied.通过调整大小,它突然被应用。

Why does the attribute change not show up?为什么属性变化不显示? Am I missing some (undocumented) update call or is this just a browser bug?我是否错过了一些(未记录的)更新调用,或者这只是一个浏览器错误?

You can see the issue in the snippet, repeatedly click on the fill-mode links.您可以在代码段中看到问题,反复单击填充模式链接。

 const doc = document.createElementNS("http://www.w3.org/2000/svg", 'svg'); doc.setAttribute("xmlns", "http://www.w3.org/2000/svg"); doc.setAttribute("viewBox", "0 0 200 200"); doc.setAttribute("width", 200); doc.setAttribute("height", 200); const polygon = document.createElementNS("http://www.w3.org/2000/svg", 'polygon'); polygon.setAttribute('stroke', 'black'); polygon.setAttribute('stroke-width', 3); polygon.setAttribute('fill', 'blue'); polygon.setAttribute('fill-rule', 'evenodd'); polygon.setAttribute('points', '150,100,59,129,115,52,115,147,59,70'); doc.appendChild(polygon); document.getElementById('dest').appendChild(doc); // setting up event handlers [...document.querySelectorAll('[data-color]')].forEach((el) => el.addEventListener( "click", (e) => polygon.setAttribute('fill', e.target.getAttribute('data-color')) ) ); [...document.querySelectorAll('[data-fill-rule]')].forEach((el) => el.addEventListener( "click", (e) => polygon.setAttribute('fill-rule', e.target.getAttribute('data-fill-rule')) ) );
 <a href="#" data-color="red">set red</a> - <a href="#" data-color="green">set green</a> - <a href="#" data-color="blue">set blue</a><br> Problematic, swap multiple: <a href="#" data-fill-rule="evenodd">set evenodd</a> <a href="#" data-fill-rule="nonzero">set nonzero</a> <div id="dest"></div>

Definitely a bug.绝对是一个错误。
getComputedstyle(polygon).fill-rule shows the correct fill-rule state, but it is not applied. getComputedstyle(polygon).fill-rule显示正确的fill-rule state,但未应用。

Interesting.. the MDN page has no Browser compatibility info:有趣.. MDN 页面没有浏览器兼容性信息:
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-rule https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-rule

A work-around is to move the polygon inside the SVG with: this.closest('svg').append(this);一种解决方法是移动SVG的多边形: this.closest('svg').append(this);

If you have more complex SVG you can use insertBefore :如果你有更复杂的 SVG 你可以使用insertBefore
https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore
to move the element 'inplace' before itself将元素“就地”移动到自身之前

Moving redraws the polygon with the correct fill-rule state移动使用正确的fill-rule state重绘多边形
(but you may get unwanted side-effects with animations) (但您可能会通过动画获得不需要的副作用)

  • SO snippet below has incorrect behavior on a click (in Chromium browsers)下面的 SO 代码段在点击时有不正确的行为(在 Chromium 浏览器中)

  • Hold down the Ctrl key on a click to use the work-around按住 Ctrl 键单击以使用解决方法
    (2 clicks ofcourse when the previous click did not apply the state) (当上一次点击未应用状态时,当然点击了 2 次)

 <b>Click Red color to toggle fill-rule: evenodd/nonzero</b><br> <svg xmlns="http://www.w3.org/2000/svg" viewBox="50 50 100 100" width="200"> <polygon fill-rule="evenodd" fill="red" points="150,100,59,129,115,52,115,147,59,70" onclick="let fr = this.getAttribute('fill-rule')=='evenodd'? 'nonzero': 'evenodd'; this.setAttribute('fill-rule', fr ); if (event.ctrlKey) this.closest('svg').append(this)"></polygon> </svg>

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

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