【问题标题】:How to save rounded corner Image using todataUrl?如何使用 todataUrl 保存圆角图像?
【发布时间】:2013-09-10 10:26:24
【问题描述】:

我需要将绘制的图像(来自 base64 编码)保存到圆角画布中。画布使用以下方式定义:

<canvas id="thumbnailImage" width="86" height="86" style="border:1px solid #d3d3d3;border-radius:10px;">

图像按预期显示(使用 ctx.drawImage 等...),带有圆角。然后我使用

获取圆角图像的base64编码数据
        var imageData = $(jImageId)[0].toDataURL("image/jpeg",qly);

不幸的是,当没有圆角画布显示图像时,角落仍然存在......

问题:有没有一种简单的方法来获取显示在画布上的 base64 图像数据,还是我必须经历痛苦的​​像素剪切考验?

谢谢!

【问题讨论】:

    标签: html image-processing canvas base64 image-clipping


    【解决方案1】:

    似乎边框半径只是 HTML 的样式,而不是画布内的图像数据,因此您必须将角擦掉才能获得圆形图像。

    搜索后我找到了theseposts,它教会了我如何擦除形状而不是重新排列,所以我们开始吧:

    JSFiddle(虽然由于 COR 限制,最终导出部分无法使用)

    ctx.drawImage(src,0,0);
    ctx.save();
    ctx.globalCompositeOperation="destination-out";
    ctx.beginPath();
    ctx.moveTo(0,0);
    ctx.lineTo(10,0);
    ctx.arcTo(0,0,0,10,10);
    ctx.closePath();
    ctx.fill();
    ctx.beginPath();
    ctx.moveTo(100,0);
    ctx.lineTo(90,0);
    ctx.arcTo(100,0,100,10,10);
    ctx.closePath();
    ctx.fill();
    ctx.beginPath();
    ctx.moveTo(0,100);
    ctx.lineTo(0,90);
    ctx.arcTo(0,100,10,100,10);
    ctx.closePath();
    ctx.fill();
    ctx.beginPath();
    ctx.moveTo(100,100);
    ctx.lineTo(90,100);
    ctx.arcTo(100,100,100,90,10);
    ctx.closePath();
    ctx.fill();
    ctx.restore();
    des.src=can.toDataURL("image/png");
    

    基于您已经知道圆角半径的事实。

    这是我本地主机上的结果截图:

    【讨论】:

    • 感谢详细的回答,真的很有帮助!
    猜你喜欢
    • 1970-01-01
    • 2020-06-07
    • 2013-08-01
    • 2017-11-19
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    相关资源
    最近更新 更多