简体   繁体   English

自定义元素/WebComponents 是否与 SVG 一起使用?

[英]Do Custom Elements / WebComponents work with SVG?

For CustomElements I can do this:对于 CustomElements 我可以这样做:

class MyElement extends HTMLElement {}

Can I do the same for SVG Elements?我可以对 SVG 元素做同样的事情吗? like

class MyOwnRectangle extends SVGRectElement {}

If it works, can I trouble someone for a jsbin?能的话,能麻烦谁要一个jsbin吗?

If not, why not?如果不是,为什么不呢?

This has some very good use cases like being able to store model data in the element itself.这有一些非常好的用例,例如能够在元素本身中存储model数据。 SVG manipulation is basically used in 2 major areas: SVG操作主要用于 2 个主要领域:

  1. Animations动画
  2. Diagrams like ER, Organisation Charts, Process flows etc. ER、组织结构图、流程等图表。

A library like JointJS has its own abstractions in the form of classes (which extends a Backbone View) to store model data associated with the view (SVG diagrams on screen).JointJS这样的库有自己的类形式的抽象(它扩展了主干视图)来存储与视图相关的model数据(屏幕上的 SVG 图)。

Like WebComponents can eventually replace jQuery Plugins, Framework X's components in a standard way, why not do the same for SVG?就像 WebComponents 最终可以以标准方式取代 jQuery Plugins,Framework X 的组件,为什么不对 SVG 做同样的事情呢?

Finally ,最后

How is Polymer 0.5 able to do this? Polymer 0.5 是如何做到这一点的?

Does Polymer.js support inner SVG elements? Polymer.js 是否支持内部 SVG 元素?

How is this patch able to do this in Polymer 1.0?这个补丁如何在 Polymer 1.0 中做到这一点?

https://github.com/Polymer/polymer/pull/3372 https://github.com/Polymer/polymer/pull/3372

What is the alternative for now?现在有什么选择?

I guess we can extend HTMLElement .我想我们可以扩展HTMLElement In our ShadowDOM use an <svg> element and then attach all the SVG tags we actually want to extend like <rect> , <polyline> etc?在我们的 ShadowDOM 中使用<svg>元素,然后附加我们实际想要扩展的所有 SVG 标签,如<rect><polyline>等? Perhaps a much cleaner way out of this?也许是一种更简洁的方法?

Will this be implemented in the future?以后会不会实施?

According to the specification 根据规范

If result's namespace is not the HTML namespace, then throw a NotSupportedError. 如果result的命名空间不是HTML命名空间,则抛出NotSupportedError。

All SVG elements are in the SVG namespace. 所有SVG元素都在SVG名称空间中。 So the speification precludes support. 因此,该规定排除了支持。

Ways to store data in SVG include the the <metadata> tag and data- attributes . 在SVG中存储数据的方法包括<metadata>标记数据属性

You can try including your component inside a foreignObject tag encapsulated inside your svg, which requires x,y,height and width properties.您可以尝试将组件包含在封装在 svg 中的 foreignObject 标签中,这需要 x、y、height 和 width 属性。 This is a way to include any html element inside an svg.这是一种在 svg 中包含任何 html 元素的方法。

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

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