我有一个简单的 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
当我将代码从直接嵌入的表单移动到角度指令后面时,就会出现问题。一旦我将表单移动到指令中,就会弹出一个问题,图像被加载并添加到画布中,没有(任何明显的)问题。但是,一旦您单击画布上的任意位置(例如,试图移动图像),您添加的图像将不再出现在画布上(尽管检查 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();
}
控制器:
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)
工作(图像可以移动)(没有指令):
损坏/问题(移动时图像消失)(带有指令):