Skip to content

Instantly share code, notes, and snippets.

@baxeico
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baxeico/36fe29ce484b194943aa to your computer and use it in GitHub Desktop.
Save baxeico/36fe29ce484b194943aa to your computer and use it in GitHub Desktop.
angular.module('canvas_share', ['ionic', 'ngCordova'])
.controller('CanvasShareCtrl', function($scope, $cordovaSocialSharing, $q) {
var buildImage = function() {
var deferred = $q.defer();
// create the canvas
var canvas = document.createElement('canvas');
canvas.width = 400;
canvas.height = 120;
var context = canvas.getContext('2d');
// draw a rectangular white frame for our content
context.beginPath();
context.fillStyle = "white";
context.rect(1, 1, 398, 118);
context.fill();
context.stroke();
// draw some text, leaving space for the avatar image
context.fillStyle = "black";
context.font = "18px Arial";
context.fillText("Jack", 120, 25, 190);
context.font = "24px Arial";
context.fillStyle = "green";
context.fillText("10 points", 120, 55, 190);
// draw avatar image on the left
var avatar = new Image();
avatar.onload = function() {
context.drawImage(avatar, 10, 10, 100, 100);
deferred.resolve(canvas);
};
avatar.src = "img/avatar.png";
return deferred.promise;
};
// function to call from a button in your template with ng-click
$scope.share = function() {
buildImage().then(function(canvas) {
return $cordovaSocialSharing.share(null, null, canvas.toDataURL());
})
.then(function() {
console.log('Shared successfully');
}, function(err) {
console.log('Error while sharing');
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment