透明的HTML5 canvas todataurl仅在移动设备上呈现透明背景

时间:2018-04-03 15:49:05

标签: javascript canvas html5-canvas fabricjs todataurl

已编辑,请参阅问题结尾。

在我的应用程序中,我有两个canvas元素。一个显示分层的透明png,另一个从文件输入中获取图像并屏蔽它。所选图像在未被遮盖的情况下是透明的。然后将此图像转换为dataUrl,进行转换以适合第一个画布并添加为第一个画布的顶层。

桌面浏览器上的一切都按预期工作:Chrome OSX,Safari OSX。我只在加载时添加它,所以我确保不会出现竞争条件。

在Android Chrome和Safari iOS上,经过转换的画布todataURL呈现为透明。如果我将非透明图像添加到第二个画布,渲染的图像甚至会在移动设备上显示。

要检查我是否将所谓的透明画布添加到正文中。它在桌面上正确显示,但在移动浏览器上是透明的。这里是简化的JS。我使用fabric.js是为了方便,但问题是没有lib的问题。我甚至曾经添加了背景色。然后只显示颜色。任何想法为什么移动浏览器上的todataurl只渲染透明像素?

<body>
<canvas id="canv"></canvas>
<script src="fabric.js"></script>
<script>
// main canvas
var c = new fabric.Canvas('canv');
c.setWidth(200);
c.setHeight(200);

var i = document.createElement('img');
i.src = 'dummy.jpg';
// i.src = 'dummy1.png';
i.onload = function(e) {
    //document.body.appendChild(i);

    scale = 1; // resizes the image
    var ci = new fabric.Image(i);

    ci.set({
        left: 0,
        top: 0,
        scaleX: scale,
        scaleY: scale,
        originX: 'left',
        originY: 'top'
    }).setCoords();

    // temporary canvas, will be converted to dataurl, contains transformed image
    var tmpCanvas = new fabric.Canvas();
    tmpCanvas.setWidth(100);
    tmpCanvas.setHeight(100);
    ci.scaleToWidth(100);
    tmpCanvas.add(ci);
    tmpCanvas.renderAll();

    // create image from temporary canvas
    var customImage = new fabric.Image.fromURL(tmpCanvas.toDataURL({ format: 'png' }), function (cImg) {
        // add it to original canvas
        c.clear();
        c.add(cImg);
        c.renderAll();
        data = c.toDataURL({ format: 'png' });

        // resized image 
        var newc = new fabric.StaticCanvas().setWidth(300).setHeight(300);
        var newImg = new fabric.Image.fromURL(data, function (c1Img) {

            newc.add(c1Img);
            newc.renderAll();

            // append to body to check if canvas is rendered correctly
            document.body.appendChild(newc.lowerCanvasEl);
        });
    });
}
</script>

编辑:我解决了这个问题,但在Javascript方面找不到问题。

问题在于我将临时画布复制到另一个画布上。通过在png中找到非透明像素的边界框来计算添加的画布的比例和位置,该png是为此目的而生成的。简而言之。

边界框是在应用程序开头的另一个临时画布中计算的(基于此答案)。虽然掩码及其画布的所有大小都设置正确并且画布从未添加到DOM中,但是当加载到小屏幕上时,边界框的结果与全屏结果不同。经过多次测试后,我发现桌面上也是如此。

因为我已经在这个问题上花了很多时间,所以我决定尝试计算PHP中的边界并将其放入数据属性中。哪个效果很好!

对于那些对PHP解决方案感兴趣的人:

function get_bounding_box($imgPath) {

$img = imagecreatefrompng($imgPath);
$w = imagesx($img);
$h = imagesy($img);

$bounds = [
    'left' => $w,
    'right' => 0,
    'top' => $h,
    'bottom' => 0
];
// get alpha of every pixel, if it is not fully transparent, write it to bounds
for ($yPos = 0; $yPos < $h; $yPos++) {
    for ($xPos = 0; $xPos < $w; $xPos++) {
        // Check, ob Pixel nicht vollständig transparent ist
        $rgb = imagecolorat($img, $xPos, $yPos);
        if (imagecolorsforindex($img, $rgb)['alpha']  < 127) {
            if ($xPos < $bounds['left']) {
                $bounds['left'] = $xPos;
            }

            if ($xPos > $bounds['right']) {
                $bounds['right'] = $xPos;
            }

            if ($yPos < $bounds['top']) {
                $bounds['top'] = $yPos;
            }

            if ($yPos > $bounds['bottom']) {
                $bounds['bottom'] = $yPos;
            }
        }
    }
}
return $bounds;

}

1 个答案:

答案 0 :(得分:0)

问题在于我将临时画布复制到另一个画布上。通过在png中找到非透明像素的边界框来计算添加的画布的比例和位置,该png是为此目的而生成的。简而言之。

边界框是在应用程序开头的另一个临时画布中计算的(基于此答案)。虽然掩码及其画布的所有大小都设置正确并且画布从未添加到DOM中,但是当加载到小屏幕上时,边界框的结果与全屏结果不同。经过多次测试后,我发现桌面上也是如此。

因为我已经在这个问题上花了很多时间,所以我决定尝试计算PHP中的边界并将其放入数据属性中。哪个效果很好!

对于那些对PHP解决方案感兴趣的人:

function get_bounding_box($imgPath) {

$img = imagecreatefrompng($imgPath);
$w = imagesx($img);
$h = imagesy($img);

$bounds = [
    'left' => $w,
    'right' => 0,
    'top' => $h,
    'bottom' => 0
];
// get alpha of every pixel, if it is not fully transparent, write it to bounds
for ($yPos = 0; $yPos < $h; $yPos++) {
    for ($xPos = 0; $xPos < $w; $xPos++) {
        // Check, ob Pixel nicht vollständig transparent ist
        $rgb = imagecolorat($img, $xPos, $yPos);
        if (imagecolorsforindex($img, $rgb)['alpha']  < 127) {
            if ($xPos < $bounds['left']) {
                $bounds['left'] = $xPos;
            }

            if ($xPos > $bounds['right']) {
                $bounds['right'] = $xPos;
            }

            if ($yPos < $bounds['top']) {
                $bounds['top'] = $yPos;
            }

            if ($yPos > $bounds['bottom']) {
                $bounds['bottom'] = $yPos;
            }
        }
    }
}
return $bounds;
}