繁体   English   中英

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

[英]Transparent HTML5 canvas todataurl only rendering transparent background on mobile devices

编辑,请参阅问题结尾。

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

一切都能在桌面浏览器上正常运行:Chrome OSX,Safari OSX。 我只在加载时添加它,所以我确保不会发生竞争情况。

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

为了检查,我在身体上添加了所谓的透明画布。 它在桌面上正确显示,但在移动浏览器上透明。 这里是简化的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>

编辑:我解决了问题,但找不到Java脚本方面的问题。

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

边界框是在应用程序启动时在另一个临时画布中计算的(基于此答案)。 尽管正确设置了蒙版及其画布的所有大小,并且从未将画布添加到DOM,但是在小屏幕上加载时,边框的结果与全屏结果有所不同。 经过大量测试后,我发现台式机也是如此。

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

对于那些对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;

}

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

边界框是在应用程序启动时在另一个临时画布中计算的(基于此答案)。 尽管正确设置了蒙版及其画布的所有大小,并且从未将画布添加到DOM,但是在小屏幕上加载时,边框的结果与全屏结果有所不同。 经过大量测试后,我发现台式机也是如此。

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

对于那些对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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM