javascript - Angular.js 和 Fabric.js : Fabric canvas changes behavior once code is moved to a Angular Directive

标签 javascript angularjs angularjs-directive fabricjs

我有一个简单的 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

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

JS 应用程序和助手:

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();
}

Controller :

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')

    }
}

指令代码:

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

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>

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

重现

1) browse to an image
2) move the image (notice the image disappears in the second link)

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

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

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

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

最佳答案

从您的模板中删除 ng-controller="ImageCtrl",创建另一个 $scope 和 Controller 实例,这会破坏它。

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

关于javascript - Angular.js 和 Fabric.js : Fabric canvas changes behavior once code is moved to a Angular Directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18685453/

相关文章:

javascript - 使用 AngularJS 时,我应该在 $apply 和 $watch 之间使用哪个?

javascript - 从两个表达式设置 ng-checked 值

angularjs - Angular 在表之外渲染指令

javascript - ng-repeat 项目在父指令的链接后功能中不可用

javascript - Angular CLI "ng e2e": missing localStorage in protractor tests with ts-node

javascript - Google 图书预览在安全 https 上不显示图书图像链接,但在 Google Chrome 和 Touch 浏览器中使用 http..

javascript - Angular ngRoute 导致无限循环和堆栈溢出

javascript - 过滤函数中的一行中有多个三元运算符

javascript - 如何调试 ng-click 在模态中不起作用?

javascript - 将父函数与 & from 指令一起使用以实现自定义行为,但需要回调?