Raphael.js:为什么SVG定位在-0.5px的左边?

问题描述 投票:4回答:2

我正在使用的Web应用程序的一项功能允许用精确的副本作为SVG替换DIV中的图像。这是使用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 ...

javascript html raphael
2个回答
5
投票

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

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

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


1
投票

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

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

Paper.renderfix()

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

© www.soinside.com 2019 - 2024. All rights reserved.