Accessing canvas context


#1

hello,
please help find out what’s wrong with this code:

    $scope.canvasElement = angular.element(document.getElementById('canvas'));
    console.log("canvasElement: "+$scope.canvasElement); // shows canvasElement: []
    $scope.canvas = $scope.canvasElement[0];
    console.log("canvas: "+$scope.canvas); // shows canvas: undefined
     $scope.context = $scope.canvas.getContext('2d'); // so this is null
    console.log("context: "+$scope.context);

thank you


#2

I also tryed this without success:

var canvas = angular.element(document.getElementById(‘canevas’));
console.log("Canvas: "+canvas);

var ctx = canvas.getContext(“2d”);
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.stroke();


#3

works now with:

    setTimeout(function() {
       
        $scope.canvas = angular.element(document.getElementById('canevas'));
        console.log("Canvas: "+$scope.canvas);

        $scope.ctx = $scope.canvas[0].getContext("2d");
        $scope.ctx.moveTo(0,0);
        $scope.ctx.lineTo(200,100);
        $scope.ctx.stroke(); 
        console.log("context: "+$scope.ctx);