devgeeks / Canvas2ImagePlugin

PhoneGap / Cordova plugin for iOS to save the contents of an HTML canvas to the device's Photo Library
211 stars 218 forks source link

Canvas2ImagePlugin.js:10Uncaught ReferenceError: module is not defined #65

Open sornanun opened 8 years ago

sornanun commented 8 years ago

I work on ionic framework. I added canvas2ImagePlugin to my project and link it in index.html. But I have some problem that I don't know how to fix. I'm a beginner with ionic and angularJS.

Error : Canvas2ImagePlugin.js:10Uncaught ReferenceError: module is not defined

module.exports = {   /// I got error this line.
    saveImageDataToLibrary:function(successCallback, failureCallback, canvasId) {
        // successCallback required
        if (typeof successCallback != "function") {
            console.log("Canvas2ImagePlugin Error: successCallback is not a function");
        }
        else if (typeof failureCallback != "function") {
            console.log("Canvas2ImagePlugin Error: failureCallback is not a function");
        }
        else {
            var canvas = (typeof canvasId === "string") ? document.getElementById(canvasId) : canvasId;
            var imageData = canvas.toDataURL().replace(/data:image\/png;base64,/,'');
            return cordova.exec(successCallback, failureCallback, "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]);
        }
    }
  };

and I will use this code in my controller.js to save image to mobile storage.

var canvas = document.createElement( 'pwCanvasMain' );
        var ctx = canvas.getContext( '2d' );
        void ctx.drawImage( this, 0, 0, img.width, img.height);
        var dataURI = canvas.toDataURL().replace( /data:image\/png;base64,/, '' );
        function successCallback( result ) {
            q.resolve( 'file:///' + result );
        }
        function failureCallback( err ) {
            console.error( err );
            q.reject( err );
        }
        cordova.exec( successCallback, failureCallback, "Canvas2ImagePlugin", "saveImageDataToLibrary", [dataURI] );

I want to convert my canvas as image then save on my mobile device storage. But my issue is module is not defined. Please help. Thanks.

ghost commented 8 years ago

@sornanun The correct way to call this plugin inside your controller:

window.canvas2ImagePlugin.saveImageDataToLibrary( function(msg){ console.log(msg); //This is your successCallback, msg - here should return the local path where the image was stored }, function(err){ console.log(err); //This is your failureCallback }, document.getElementById('myCanvas') // This is your canvas id );

Don't use > cordova.exec( successCallback, failureCallback, "Canvas2ImagePlugin", "saveImageDataToLibrary", [dataURI] ); This is already defined in the plugin interface file.

commonpike commented 6 years ago

@ghost, that is not the error OP was having ? Also, cordova.exec(..) using a dataURI as last argument is a working (though in this case apparently redundant) call on Android using 0.6.0