html2canvas边框半径不适用于img标签

用户名

需要帮助,当我通过css将边界半径应用于imgae时,渲染不正确,渲染的图像应该是相同的预览图像。我使用html2canvas将div转换为图像。

所附图像供参考,第一个是带边框半径的普通预览,第二个是不带边框半径的普通预览。

在此处输入图片说明

<div id="mydiv">
<img src="1.jpg" /> 
<p>text!</p>
</div>

<p>Generated image</p>
<div id="image">
<p>Image:</p>
</div>

的CSS

    <style>
    #mydiv {
    width: 300px;
    height: 200px;
    background:#000;


}
#mydiv img {
    width: 200px;
    height: 200px;
    border-radius:100%;


}

    </style>

js

html2canvas([document.getElementById('mydiv')], {
    onrendered: function (canvas) {
        //document.getElementById('canvas').appendChild(canvas);
        var data = canvas.toDataURL('image/png');
        // AJAX call to send `data` to a PHP file that creates an image from the dataURI string and saves it to a directory on the server

        var image = new Image();
        image.src = data;
        document.getElementById('image').appendChild(image);
    }
});

在此处输入图片说明

品牌

这是一个未解决的错误(已报告该问题):

http://github.com/niklasvh/html2canvas/issues/346

您可以将画布的剪切功能用作解决方法。

演示:http : //jsfiddle.net/m1erickson/8wL2q/

在此处输入图片说明

使用剪切区域而不是边界半径的示例代码:

// save the context in its unaltered state
ctx.save();

// fill canvas with black
ctx.fillStyle="black";
ctx.fillRect(0,0,canvas.width,canvas.height);

// create clipping region which will display portion of image
// The image will only be visible inside the circular clipping path
ctx.beginPath();
ctx.arc(100,100,100,0,Math.PI*2);
ctx.closePath();
ctx.clip();

// draw the image into the clipping region
ctx.drawImage(img,0,0);

// restore the context to its unaltered state
ctx.restore();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章