【问题标题】:Raphael.js: Why is the SVG positioned -0.5px to the left?Raphael.js:为什么 SVG 位于左侧 -0.5px?
【发布时间】:2012-10-05 16:48:59
【问题描述】:

我正在开发的 Web 应用程序的一项功能允许将 DIV 中的图像替换为 SVG 的精确副本。这是使用 Raphael.js 库完成的。

下面是一个包含图像的 HTML 元素的示例:

<div id="ow-0" reference="0" class="object-wrapper" style="z-index: 3; width: 231px; left: 116px; top: 217px; outline: rgb(0, 0, 0) solid 1px; ">
    <img id="o-0" reference="0" class="object image" src="image-gallery/intermediate/3295094303_2b50e5fe03_z.jpg" />
</div>

当调用名为addSVG()的方法时,图像父div被设置为Raphael对象paper,并且在隐藏原始图像后添加了图像的SVG:

            // Create the SVG and add it to the Viewport
            this.addSVG = function(){

                // Hide the HTML element before replacing it with the SVG
                $("#o-"+this.ref).hide();

                // Create the "paper" (svg canvas) before embedding the actual SVG
                this.canvas = new Raphael(document.getElementById("ow-"+this.ref), this.width, this.height);                

                if (this.type=="image" || this.type=="QR"){
                    this.svg = this.canvas.image(this.src,0,0,this.width,this.height);

                }
                else {

                }
            }

几乎所有东西都是完美的,除了 svg 的位置,它在左侧 -0.5px(当然,这很烦人)。下面是操作后的HTML代码:

    <div id="ow-0" reference="0" class="object-wrapper" style="z-index: 3; width: 231px; left: 116px; top: 217px; outline: rgb(0, 0, 0) solid 1px; ">
        <svg style="overflow: hidden; position: relative; left: -0.5px; " height="170" version="1.1" width="231" xmlns="http://www.w3.org/2000/svg">
            <desc style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); ">Created with Raphaël 2.1.0</desc>
            <defs style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); "></defs>
            <image style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); " x="0" y="0" width="231" height="170" preserveAspectRatio="none" xlink:href="image-gallery/intermediate/3295094303_2b50e5fe03_z.jpg"></image>
        </svg>
        <img id="o-0" reference="0" class="object image" src="image-gallery/intermediate/3295094303_2b50e5fe03_z.jpg" style="display: none; ">
    </div>

为什么要添加 -0.5px 以及如何防止它发生? 顺便说一句,我调整了浏览器窗口的大小,作为一个实验,并且 -0.5px 不再添加......

【问题讨论】:

  • 似乎无法在 Chrome 中重现,请参阅 jsfiddle.net/Xe88g/33
  • 在您的 HTML 中,任何父元素的偏移量是否会是相对计算的结果 - 即 margin-left: 10%?我听说过这种情况发生在父元素从其环境中继承分数偏移的情况下。另外:您是否尝试过在创建 Raphael 的纸元素后显式修改它,将 style:left 设置为 0?
  • 不,SVG 在其父 DOM 元素中的位置是绝对的。使用 CSS 显式设置 SVG 样式(顶部和左侧 = 0)是我同时发现的解决问题的解决方案。

标签: javascript html raphael


【解决方案1】:

我发现解决此问题的唯一解决方案是将SVG的top和left属性(使用CSS)显式设置为0,并将CSS规则标记为!important。

svg {
   top: 0 !important;
   left: 0 !important;
}

但是,我仍然不知道问题的原因,所以如果有人有答案,请与我们分享。

【讨论】:

    【解决方案2】:

    您需要调用 Paper.renderfix() 才能在 IE9 和 Firefox 中修复此问题。

    http://raphaeljs.com/reference.html#Paper.renderfix

    Paper.renderfix()

    修复了 Firefox 和 IE9 关于亚像素渲染的问题。如果 回流后纸张依赖于其他元素,它可能会移动一半 导致线条失去清晰度的像素。此方法修复 问题。

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 1970-01-01
      • 2021-01-21
      • 2011-11-05
      • 1970-01-01
      • 2011-04-02
      • 2011-12-28
      • 1970-01-01
      • 2016-10-22
      相关资源
      最近更新 更多