cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

              <div ng-app="CanvasTest" ng-controller="MainController">
  <h2>Overview</h2> {{message}}
  <!--Create the canvas with the correct attributes. the numbers="ctrlnumbers" makes sure the 
      directive's scope.numbers is set to the controller's $scope.ctrlnumbers.-->
  <canvas state-graph numbers="ctrlnumbers" width='550' height='500' </canvas>
                function pad(n, width, z) {
       Pad a number with zero's. Code courtesy of Pointy at
    z = z || '0';
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
  var app = angular.module('CanvasTest', []);
  app.directive('stateGraph', function() {
    /* Create a stateGraph directive for canvases. The state-graph attribute can be set on them to
       activate this behaviour. It also needs a numbers attribute which contains the object containing
       the numbers to draw.
    return {
      restrict: 'A',
      scope: {
        numbers: "="
      link: function link(scope, element, attrs) {
        //The link function draws the canvas and sets-up the interaction

        function drawNumber(x, y, fontsize, n) {
             Draw a number at middle x, y. First clear the old number by creating a circle
             where the text is, clipping the canvas to that circle, clearing the area 
             (clip makes sure only the area within the clipped path is cleared) and undoing
             the clip. Then it draws the new text.

          var r = fontsize - 6;
          ctx = scope.ctx;
          ctx.save(); //save the current canvas clipped area (not clipped at all)
          ctx.beginPath(); //draw a new circle where to clip
          ctx.arc(x, y, r, 0, 2 * Math.PI);
          ctx.clearRect(x - r, y - r, r * 2, r * 2); //Erase the area (only the clipped area will be erased)
          ctx.restore(); //Restore the old clipped area (not clipped)
          ctx.font = fontsize + "px Arial";
          ctx.fillText(pad(n, 2), x - fontsize / 2, y + fontsize / 2 - 3); //Draw the new text
        var canvas = element[0]; //get the canvas
        var ctx = canvas.getContext("2d"); //get the context
        scope.ctx = ctx;

        //Set the locations and size to draw the numbers
        var x1 = 195;
        var x2 = 292;
        var y = 165;
        var fontsize = 30;

           The watch functions watch for changes in the scope.numbers object and call
           drawNumber with the new value when it changed.
        scope.$watch("numbers.a", function(oldval, newval) {
          drawNumber(x1, y, fontsize, newval);
        scope.$watch("numbers.b", function(oldval, newval) {
          drawNumber(x2, y, fontsize, newval);

        // Draw the first numbers.
        drawNumber(x1, y, fontsize, scope.numbers.a);
        drawNumber(x2, y, fontsize, scope.numbers.b);

           Draw a png image onto the canvas. It should only be drawn on the canvas after it
           loaded fully. This is done by putting the drawImage call in a onload function for 
           the image, which only gets called when the image is loaded. Only *after* the onload
           is set, set the source url for the image so it starts loading.
        var img = new Image(); //Create a background image
        img.onload = function() {
          ctx.drawImage(img, 10, 70);
        img.src = '//www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';


  app.controller('MainController', ['$scope', '$timeout', function($scope, $timeout) {
    $scope.message = 'Hello World of Canvas!';
    $scope.ctrlnumbers = {
        'a': 1,
        'b': 1
      } //Initial values for the numbers to draw.

    $scope.changeNumbers = function() {
      //Just a function that keeps changing the numbers every 500ms.
      if ($scope.ctrlnumbers.a + 1 > 99) {
        $scope.ctrlnumbers.a = 1;
      } else {
      if ($scope.ctrlnumbers.b * 2 > 99) {
        $scope.ctrlnumbers.b = 1;
      } else {
        $scope.ctrlnumbers.b = $scope.ctrlnumbers.b * 2;
      $timeout($scope.changeNumbers, 500);
    $timeout($scope.changeNumbers, 500);
Loading ..................