Angular.js和Fabric.js:一旦代码移动到Angular Directive,Fabric canvas就会改变行为

aka*_*nom 8 javascript fabricjs angularjs angularjs-directive

我有一个简单的AngularJS/FabricJs应用程序,目的是允许在上载之前移动/重新调整图像大小.基本上有四个步骤:

1) I present a form with a canvas, and a rectangle inside of form to represent a clip area 
2) browse for a local file 
3) add it to the canvas 
4) and have a button to capture the clip area inside of the canvas
Run Code Online (Sandbox Code Playgroud)

当我将代码从直接嵌入的表单移动到坐在angular指令后面时,会出现问题.一旦我将表单移动到指令中弹出一个问题,图像就被加载并添加到画布而没有(任何明显的)问题.但是,一旦您单击画布上的任何位置(例如,尝试移动图像),您添加的图像就不再出现在画布上(尽管检查fabricJs Canvas对象仍然将其显示为其对象数组内部).

JS App和助手:

var myApp = angular.module('myApp', [])

// helper function to bind tot he on change of a input/file object (angularJs workaround)
var invokeImageChange = function(that) {
    angular.element(that).scope().imageChange(that)
    angular.element(that).scope().$digest();
}
Run Code Online (Sandbox Code Playgroud)

控制器:

var ImageCtrl = function($scope) {

    var desiredHeight = 300
    var desiredWidth = 770

    // I understand this docucment.gelElementById is not the angular way - and needs to be changed
    var myImageData = document.getElementById('fileData')
    var myImage = document.getElementById('myImage')
    var canvas = new fabric.Canvas('myCanvas')

    var rect = new fabric.Rect({
        fill: 'lightgray',
        left: canvas.width / 2,
        top: canvas.height / 2,
        width: desiredWidth,
        height: desiredHeight,
        stroke: 'darkgray',
        strokeDashArray: [5, 5],
        selectable: false
    });
    canvas.add(rect)
    var clipZone = {
        y: rect.top - (.5 * rect.height),
        x: rect.left - (.5 * rect.width),
        width: desiredWidth,
        height: desiredHeight,
        quality: 1
    }

    $scope.caption = "" ;
    $scope.fileUrl = "" ;

    $scope.imageChange = function(inp)  {
        console.log('imageChange')
        file = inp.files[0];
        fr = new FileReader();
        fr.onload = createImage;
        fr.readAsDataURL(file);

        var img = null

        function createImage() {
            console.log('createImage')
            img = new Image();
            img.onload = imageLoaded;
            img.src = fr.result;
        }

        function imageLoaded() {
            console.log('imageLoaded')
            var fabImg = new fabric.Image(img)

            fabImg.scale(1.0).set({
                left: canvas.width / 2,
                top: canvas.height / 2
            });

            canvas.add(fabImg)
            canvas.setActiveObject(fabImg)
        }
    }

    $scope.submit = function(event) {

    }

    $scope.capture = function() {
        console.log('capture')

    }
}
Run Code Online (Sandbox Code Playgroud)

指令代码:

myApp.directive('ngImageEditor', function() {
  return {
    restrict: 'E',
    transclude: true,
    replace: true,
    controller: 'ImageCtrl',
    templateUrl: '\blah\pathToForm'
  };
});
Run Code Online (Sandbox Code Playgroud)

TemplateUrl引用此表单的位置

<form name="uploadForm" ng-controller="ImageCtrl" method="post" enctype="multipart/form-data"
      action="/main/update" ng-submit="submit()">
    <div class="control-group">
        <label class="control-label" for="file">Image File</label>
        <div class="controls">
            <input type="file" name="file" ng-model="file" onchange="invokeImageChange(this)"/>
            <input type="text" id="fileData" name="fileData" ng-model="fileUrl"/>
        </div>
    </div>
    <div class="control-group">
        <div class="controls">
            <input type="button" value="Upload"  ng-click="capture()"/>
        </div>
    </div>
    <div>
        <canvas id="myCanvas" width="800" height="400" style="border:1px solid #000000;"></canvas>
    </div>
    <img id="myImage" style="border: 1px solid burlywood">
</form>
Run Code Online (Sandbox Code Playgroud)

希望下面的JsFiddle帮助人们理解我在说什么.提前致谢!

重现

1) browse to an image
2) move the image (notice the image disappears in the second link)
Run Code Online (Sandbox Code Playgroud)

工作(图像可以移动)(无指令):

http://jsfiddle.net/PNwbp/1/

损坏/问题(图像在移动时消失)(带指令):

http://jsfiddle.net/faFVW/23/

小智 6

从你的模板中删除ng-controller ="ImageCtrl",正在创建另一个$ scope和Controller的实例,并打破它.

<form name="uploadForm" /*ng-controller="ImageCtrl"*/ method="post" enctype="multipart/form-data"
      action="/main/update" ng-submit="submit()">
Run Code Online (Sandbox Code Playgroud)