[plugin] Canvas2ImagePlugin

Post your work in progress addons and get feedback

Post » Sun Sep 21, 2014 2:04 am

Hi guys

I have a project where I need to save the canvas screenshot to mobile photo gallery
The browser plugin with its example from C2 only worked on PC browsers but not on android. after 20+ hours of research I managed to do it.
apparently, there is a third party Cordova plugin for this purpose, it is called "Canvas2ImagePlugin"

I am not an expert in programming, let alone writing plugins from scratch. so I edited the "Canvas" plugin from R0J0hound to test it and it WORKED.

Here is what I did:
1) add the following in Canvas "edittime.js"
AddAction(44, 0, "Canvas2Image", "Canvas", "Canvas2Image", "Canvas2Image", "Canvas2Image");

2) add the following in Canvas "runtime.js"
acts.Canvas2Image= function ()
{
var imageData = this.canvas.toDataURL().replace(/data:image\/png;base64,/,'');
cordova.exec(null, null, "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]);
};
3) create a new C2 project, drop a canvas, color it red, create a button and on the even sheet, button on clicked => Canvas2Image

note: running the project on PC will not save the file for you, actually it will give you an error because it does not understand cordova.exec.

4) on Intel XDK, you need to add the plugin and enable few permissions:
4.1) Add Canvas2ImagePlugin:
from Cordova Setting, go to Third Party Plugins
click on "get plugin from the web" and fill in the following:
name=: Canvas2ImagePlugin
Plugin ID=: org.devgeeks.Canvas2ImagePlugin
Repro URL=:
(I do not have previllage to post URLs on this forum yet, maybe someone can do that for me, it is on github under /devgeeks/Canvas2ImagePlugin.git)

4.2) enable external storage read/write permission in order to be able to access the photo gallery:
Click on permission -> Android-Crosswalk
in add Permission text field add the following (submit after each enetry):
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
note: I am not sure if I need read access, but I added it anyway.

we are done, now just export the project and build it

now run the app on your mobile and click the button. go and check your gallery and you will find a sweet red image sitting there :-)

hope you guys find this helpful.
B
7
S
1
Posts: 26
Reputation: 530

Post » Sun Sep 21, 2014 4:17 am

It might be better that turn this plugin to a behavior for rojo's canvas, so that you do not need repeat canvas code.
B
109
S
27
G
276
Posts: 4,480
Reputation: 154,420

Post » Sun Sep 21, 2014 7:05 am

having it as a behavior is a good idea, actually if you think about it, it does not have to be a canvas extension only.
if I am not mistaken "Sprites" can also benefit from it as they are made of canvases as well.
B
7
S
1
Posts: 26
Reputation: 530

Post » Sun Sep 21, 2014 11:43 am

Yes, it will be more powerful.
B
109
S
27
G
276
Posts: 4,480
Reputation: 154,420

Post » Wed Jan 14, 2015 6:00 pm

Hi guys, is there a way to download the snapshot Canvas on android/crosswalk? i've tried to install the plugin "Canvas2ImagePlugin" and the permissions for external and internal storage on xdk but when i press the button for download (with "Invoke download of CanvasSnapshot) nothing happen :/
B
5
Posts: 1
Reputation: 227

Post » Sun Jan 25, 2015 11:04 pm

hi, @hassiba! I followed your tutorial step by step but not work [using the 1621 version of XDK]: I get a black screen both in emulation panel and after apk build.

any idea?
thx
B
15
S
6
Posts: 20
Reputation: 1,199

Post » Sat Mar 07, 2015 3:58 am

is it possible to make a new folder, and save images just for my game there?
"If you want to move a mountain tomorrow, you should start by lifting stones today."
B
64
S
14
G
2
Posts: 292
Reputation: 6,678

Post » Sat Feb 18, 2017 10:18 am

dokthor wrote:hi, @hassiba! I followed your tutorial step by step but not work [using the 1621 version of XDK]: I get a black screen both in emulation panel and after apk build.

any idea?
thx


Me too.
B
8
Posts: 6
Reputation: 312

Post » Sun Sep 03, 2017 5:58 pm

Anyone use this plugin today? If not, is there any similar? Thanks!
B
53
S
33
G
15
Posts: 230
Reputation: 12,962


Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 2 guests