无法读取未定义的属性'toDataURL' - 将画布另存为图像

时间:2015-12-28 02:42:01

标签: javascript

Canvas2Image.js

return {

    saveAsPNG : function(oCanvas, bReturnImg, iWidth, iHeight) {
        if (!bHasDataURL) {
            return false;
        }
        var oScaledCanvas = scaleCanvas(oCanvas, iWidth, iHeight);
        var strData = oScaledCanvas.toDataURL("image/png");
        if (bReturnImg) {
            return makeImageObject(strData);
        } else {
            saveFile(strData.replace("image/png", strDownloadMime));
        }
        return true;
    },

    saveAsJPEG : function(oCanvas, bReturnImg, iWidth, iHeight) {
        if (!bHasDataURL) {
            return false;
        }

        var oScaledCanvas = scaleCanvas(oCanvas, iWidth, iHeight);
        var strMime = "image/jpeg";
        var strData = oScaledCanvas.toDataURL(strMime);

        // check if browser actually supports jpeg by looking for the mime type in the data uri.
        // if not, return false
        if (strData.indexOf(strMime) != 5) {
            return false;
        }

        if (bReturnImg) {
            return makeImageObject(strData);
        } else {
            saveFile(strData.replace(strMime, strDownloadMime));
        }
        return true;
    },

使用此方式致电:

function exportAndSaveCanvas()  {

    // Get the canvas screenshot as PNG
    var screenshot = Canvas2Image.saveAsPNG(canvas, true);

    // This is a little trick to get the SRC attribute from the generated <img> screenshot
    canvas.parentNode.appendChild(screenshot);
    screenshot.id = "canvasimage";      
    data = $('#canvasimage').attr('src');
    canvas.parentNode.removeChild(screenshot);


    // Send the screenshot to PHP to save it on the server
    var url = 'upload/export.php';
    $.ajax({ 
        type: "POST", 
        url: url,
        dataType: 'text',
        data: {
            base64data : data
        }
    });

想要将其转换为图像并保存但是会收到此错误:未捕获的TypeError:无法读取未定义的属性“toDataURL”。知道我怎么解决它?

**我从其他来源和其他人那里复制这些代码似乎可以正常使用这些代码。我错过了什么吗?

**** oScaledCanvas和oCanvas都是未定义的。为什么呢?

0 个答案:

没有答案