• <tfoot id='WKrJ9'></tfoot>
      <bdo id='WKrJ9'></bdo><ul id='WKrJ9'></ul>

        <i id='WKrJ9'><tr id='WKrJ9'><dt id='WKrJ9'><q id='WKrJ9'><span id='WKrJ9'><b id='WKrJ9'><form id='WKrJ9'><ins id='WKrJ9'></ins><ul id='WKrJ9'></ul><sub id='WKrJ9'></sub></form><legend id='WKrJ9'></legend><bdo id='WKrJ9'><pre id='WKrJ9'><center id='WKrJ9'></center></pre></bdo></b><th id='WKrJ9'></th></span></q></dt></tr></i><div id='WKrJ9'><tfoot id='WKrJ9'></tfoot><dl id='WKrJ9'><fieldset id='WKrJ9'></fieldset></dl></div>

        <legend id='WKrJ9'><style id='WKrJ9'><dir id='WKrJ9'><q id='WKrJ9'></q></dir></style></legend>

        <small id='WKrJ9'></small><noframes id='WKrJ9'>

      1. Chrome 和 Firefox 中的 SVG 文本对齐不一致

        时间:2023-08-08

        <i id='C2UHu'><tr id='C2UHu'><dt id='C2UHu'><q id='C2UHu'><span id='C2UHu'><b id='C2UHu'><form id='C2UHu'><ins id='C2UHu'></ins><ul id='C2UHu'></ul><sub id='C2UHu'></sub></form><legend id='C2UHu'></legend><bdo id='C2UHu'><pre id='C2UHu'><center id='C2UHu'></center></pre></bdo></b><th id='C2UHu'></th></span></q></dt></tr></i><div id='C2UHu'><tfoot id='C2UHu'></tfoot><dl id='C2UHu'><fieldset id='C2UHu'></fieldset></dl></div>
      2. <legend id='C2UHu'><style id='C2UHu'><dir id='C2UHu'><q id='C2UHu'></q></dir></style></legend>
        • <bdo id='C2UHu'></bdo><ul id='C2UHu'></ul>

                <tbody id='C2UHu'></tbody>

              <small id='C2UHu'></small><noframes id='C2UHu'>

              1. <tfoot id='C2UHu'></tfoot>
                  本文介绍了Chrome 和 Firefox 中的 SVG 文本对齐不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在 Leaflet.js 地图上绘制 SVG 标记图标.图标代表气象站,它们会根据风向旋转,并以叠加的形式显示平均风速.

                  I'm drawing SVG marker icons on a Leaflet.js map. The icons represent weather stations and they are rotated according to the wind direction and show the average wind speed as an overlay.

                  我已经能够让它在 Chrome 中按需要工作,但在 Firefox 中文本位置已关闭.

                  I have been able to get this to work as desired in Chrome but the text position is off in Firefox.

                  左边是 Chrome (55.0.2883.95),右边是 Firefox (50.0.1).

                  这是我正在使用的自定义 Leaflet.Icon 类:

                  This is the custom Leaflet.Icon class I'm using:

                  window.RemoteWind = window.RemoteWind || {};
                  
                  // This uses Chroma.js to build a color scale which is used for wind speed.
                  // http://gka.github.io/chroma.js
                  RemoteWind.color_scale = chroma.scale([
                    'lightblue',
                    'green',
                    'red',
                    'purple'
                  ])
                  .mode('hsl') // the blending mode
                  .domain([0, 7, 15, 25]); // the distinct steps for each.
                  
                  RemoteWind.VectorIcon = L.Icon.extend({
                    options: {
                      height: 26,
                      width: 26,
                      stroke: 'white',
                      strokeWidth: 2,
                      circle: {
                        cx: 13,
                        cy: 13,
                        r: 13
                      }
                    },
                    _createSVG: function(){
                      var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
                      svg.setAttributeNS(null, 'version', '1.1')
                      svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
                      svg.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
                      svg.setAttribute('height', this.options.height);
                      svg.setAttribute('width',  this.options.width);
                      return svg;
                    }
                  });
                  
                  /*
                  * Vector based icon for a station
                  */
                  RemoteWind.StationIcon = RemoteWind.VectorIcon.extend({
                    options: {
                      className: 'leaflet-station-icon active',
                      speed: 0,
                      direction: 0,
                      path: {
                        d: "M26,19c0-2.2-0.6-4.4-1.6-6.2C22.2,8.8,13,0,13,0S3.8,8.7,1.6,12.8c-1,1.8-1.6,4-1.6,6.2c0,7.2,5.8,13,13,13 S26,26.2,26,19z"
                      }
                    },
                    createIcon: function (oldIcon) {
                      var div = (oldIcon && oldIcon.tagName === 'DIV') ? oldIcon : document.createElement('div'),
                      svg = this._createSVG(),
                      g = document.createElementNS('http://www.w3.org/2000/svg', 'g'),
                      txt_g = document.createElementNS('http://www.w3.org/2000/svg', 'g'),
                      options = this.options,
                      path,
                      txt;
                  
                      g.setAttributeNS(null, "transform", "translate(0,-6)");
                  
                      path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
                      path.setAttributeNS(null, 'd', "M26,19c0-2.2-0.6-4.4-1.6-6.2C22.2,8.8,13,0,13,0S3.8,8.7,1.6,12.8c-1,1.8-1.6,4-1.6,6.2c0,7.2,5.8,13,13,13 S26,26.2,26,19z");
                      path.setAttributeNS(null, 'stroke', this.options.stroke);
                      path.setAttributeNS(null, 'stroke-width', this.options.strokeWidth);
                      path.setAttributeNS(null, 'fill', RemoteWind.color_scale(options.speed).name() );
                      path.setAttributeNS(null, 'transform', 'rotate(% 13 19)'.replace('%', options.direction));
                  
                      txt = document.createElementNS('http://www.w3.org/2000/svg', 'text');
                      txt.innerHTML = Math.round(options.speed).toString();
                      txt.setAttributeNS(null, 'fill', 'white');
                      txt.setAttributeNS(null, 'x', this.options.height / 2);
                      txt.setAttributeNS(null, 'y', this.options.width / 2);
                      txt.setAttributeNS(null, 'text-anchor', 'middle');
                      txt.setAttributeNS(null, 'alignment-baseline', 'central');
                  
                      g.appendChild(path);
                      txt_g.appendChild(txt);
                      svg.appendChild(g);
                      svg.appendChild(txt_g);
                      div.appendChild(svg);
                      this._setIconStyles(div, 'icon');
                      return div;
                    },
                  
                    createShadow: function () {
                      return null;
                    }
                  });
                  
                  RemoteWind.stationIcon = function (options) {
                    return new RemoteWind.StationIcon(options);
                  };
                  

                  我通过设置 text-anchor="middle"alignment-baseline="central" 加上 x 来定位文本和 y 坐标.

                  I'm positioning the text by setting text-anchor="middle" and alignment-baseline="central" plus the x and y coordinates.

                  这是添加标记时在 DOM 中创建的 HTML:

                  This is the HTML created in the DOM when the marker is added:

                  <div class="leaflet-marker-icon leaflet-station-icon active leaflet-zoom-animated leaflet-interactive" title="Åre strand | 2(3) m/s" tabindex="0" style="transform: translate3d(-367px, 85px, 0px); z-index: 85; outline: none;">
                    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="26" width="26">
                      <g transform="translate(0,-6)">
                        <path d="M26,19c0-2.2-0.6-4.4-1.6-6.2C22.2,8.8,13,0,13,0S3.8,8.7,1.6,12.8c-1,1.8-1.6,4-1.6,6.2c0,7.2,5.8,13,13,13 S26,26.2,26,19z" stroke="white" stroke-width="2" fill="#64e0d2" transform="rotate(338 13 19)"></path>
                      </g>
                      <g>
                        <text fill="white" x="13" y="13" text-anchor="middle" alignment-baseline="central">2</text>
                      </g>
                    </svg>
                  </div>
                  

                  为什么 firefox 不能正确定位文本?

                  Why is firefox not positioning the text correctly?

                  推荐答案

                  Firefox 目前不支持 对齐基线.它确实支持 dominant-baseline,这对你的用例来说几乎是一样的.

                  Firefox does not currently support alignment-baseline. It does support dominant-baseline, which amounts to pretty much the same thing for your use case.

                  这篇关于Chrome 和 Firefox 中的 SVG 文本对齐不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Leaflet JS - 实现手势处理以强制 2 手指滚动 下一篇:传单重叠形状不透明度

                  相关文章

                  最新文章

                    <bdo id='JusiR'></bdo><ul id='JusiR'></ul>
                    <tfoot id='JusiR'></tfoot>

                      <i id='JusiR'><tr id='JusiR'><dt id='JusiR'><q id='JusiR'><span id='JusiR'><b id='JusiR'><form id='JusiR'><ins id='JusiR'></ins><ul id='JusiR'></ul><sub id='JusiR'></sub></form><legend id='JusiR'></legend><bdo id='JusiR'><pre id='JusiR'><center id='JusiR'></center></pre></bdo></b><th id='JusiR'></th></span></q></dt></tr></i><div id='JusiR'><tfoot id='JusiR'></tfoot><dl id='JusiR'><fieldset id='JusiR'></fieldset></dl></div>
                      <legend id='JusiR'><style id='JusiR'><dir id='JusiR'><q id='JusiR'></q></dir></style></legend>

                      <small id='JusiR'></small><noframes id='JusiR'>