Skip to content

Instantly share code, notes, and snippets.

@mlynch
Created April 18, 2014 21:02
Show Gist options
  • Save mlynch/11064296 to your computer and use it in GitHub Desktop.
Save mlynch/11064296 to your computer and use it in GitHub Desktop.
Starter directive
angular.module('myApp.directives', [])
.directive('myCanvas', function() {
return {
// Angular needs to know what type of usage we want to allow for this (attribute, class name, or tag).
// For this example, a tag is great.
restrict: 'E',
// We want to replace the original custom tag with a template
replace: true,
// The template code to replace our tag with. You can also use templateUrl
// to laod this from a template
template: '<canvas></canvas>',
// The link function is called after the element is compiled and then
// attached to the active scope
link: function($scope, $element, $attr) {
// $element[0] is now our canvas element after replacing the
// original tag with the template
var ctxSource = $element[0].getContext('2d');
// can do our magic now
var img = new Image();
img.src = "images/tina.png";
img.onload = function() {
ctxSource.drawImage(img, 0, 0);
};
}
}
})
@mlynch
Copy link
Author

mlynch commented Apr 18, 2014

Then you can use this like this:

<my-canvas></my-canvas>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment