Refactor properties into methods for canvas
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
<input type="text" name="w" value="100" />
|
<input type="text" name="w" value="100" />
|
||||||
<input type="text" name="h" value="100" />
|
<input type="text" name="h" value="100" />
|
||||||
Mirror: <input type="checkbox" class="image-control" name="mirror" />
|
Mirror: <input type="checkbox" class="image-control" name="mirror" />
|
||||||
|
No crop: <input type="checkbox" class="image-control" name="no-crop" />
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<br /><br />
|
<br /><br />
|
||||||
|
|||||||
@@ -17,20 +17,24 @@ var Canvas = fabric.util.createClass(fabric.Canvas, {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.on('mouse:up', function () {
|
this.on('mouse:up', function () {
|
||||||
if (this.image.__moved) {
|
if (this.getImage().__moved) {
|
||||||
this.aperture.setTransparent(false);
|
this.getApertures().setTransparent(false);
|
||||||
}
|
}
|
||||||
$(this.upperCanvasEl).css('cursor', 'default');
|
$(this.upperCanvasEl).css('cursor', 'default');
|
||||||
this.renderAll();
|
this.renderAll();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
get_apertures: function () {
|
getApertures: function () {
|
||||||
return this.__aperturehandler ||
|
return this.__aperturehandler ||
|
||||||
(this.__aperturehandler = new ApertureHandler(this));
|
(this.__aperturehandler = new ApertureHandler(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
get_dragbars: function () {
|
/**
|
||||||
|
*
|
||||||
|
* @returns {DragbarHandler}
|
||||||
|
*/
|
||||||
|
getDragbars: function () {
|
||||||
return this.__dragbars ||
|
return this.__dragbars ||
|
||||||
(this.__dragbars = new DragbarHandler(this));
|
(this.__dragbars = new DragbarHandler(this));
|
||||||
},
|
},
|
||||||
@@ -38,18 +42,29 @@ var Canvas = fabric.util.createClass(fabric.Canvas, {
|
|||||||
/**
|
/**
|
||||||
* @return {fabric.Image}
|
* @return {fabric.Image}
|
||||||
*/
|
*/
|
||||||
get_image: function () {
|
getImage: function () {
|
||||||
return this.__image;
|
return this.__image;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {Viewport}
|
* @return {Viewport}
|
||||||
*/
|
*/
|
||||||
get_viewport: function () {
|
getViewport: function () {
|
||||||
return this.__viewport ||
|
return this.__viewport ||
|
||||||
(this.__viewport = new Viewport(this));
|
(this.__viewport = new Viewport(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the menu for this instance
|
||||||
|
* @returns {ButtonMenu}
|
||||||
|
*/
|
||||||
|
getButtonMenu: function () {
|
||||||
|
if (!this.__buttonMenu) {
|
||||||
|
this.__buttonMenu = new ButtonMenu(this);
|
||||||
|
}
|
||||||
|
return this.__buttonMenu;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {Canvas}
|
* @return {Canvas}
|
||||||
*/
|
*/
|
||||||
@@ -68,35 +83,11 @@ var Canvas = fabric.util.createClass(fabric.Canvas, {
|
|||||||
this.removeImage();
|
this.removeImage();
|
||||||
this.__image = img;
|
this.__image = img;
|
||||||
this.__image.addTo(this).center().on('mousedown', function () {
|
this.__image.addTo(this).center().on('mousedown', function () {
|
||||||
img.canvas.aperture.setTransparent(true);
|
img.canvas.getApertures().setTransparent(true);
|
||||||
}).setCoords();
|
}).setCoords();
|
||||||
this.viewport.reset();
|
this.getViewport().reset();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Define properties
|
|
||||||
util.setProperties(Canvas.prototype, {
|
|
||||||
aperture: {
|
|
||||||
get: Canvas.prototype.get_apertures
|
|
||||||
},
|
|
||||||
dragbars: {
|
|
||||||
get: Canvas.prototype.get_dragbars
|
|
||||||
},
|
|
||||||
image: {
|
|
||||||
get: Canvas.prototype.get_image
|
|
||||||
},
|
|
||||||
buttonMenu: {
|
|
||||||
get: function () {
|
|
||||||
if (!this.__buttonMenu) {
|
|
||||||
this.__buttonMenu = new ButtonMenu(this);
|
|
||||||
}
|
|
||||||
return this.__buttonMenu;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
viewport: {
|
|
||||||
get: Canvas.prototype.get_viewport
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = Canvas;
|
module.exports = Canvas;
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ var DragHandle = function (handler) {
|
|||||||
mouseDown: function () {
|
mouseDown: function () {
|
||||||
_dragging = true;
|
_dragging = true;
|
||||||
_currentAxis = this.axis;
|
_currentAxis = this.axis;
|
||||||
handler.canvas.image.trigger('mousedown');
|
handler.canvas.getImage().trigger('mousedown');
|
||||||
},
|
},
|
||||||
mouseUp: function () {
|
mouseUp: function () {
|
||||||
_dragging = false;
|
_dragging = false;
|
||||||
_currentAxis = null;
|
_currentAxis = null;
|
||||||
handler.canvas.image.trigger('mouseup');
|
handler.canvas.getImage().trigger('mouseup');
|
||||||
},
|
},
|
||||||
mouseMove: function (e) {
|
mouseMove: function (e) {
|
||||||
if (!_dragging) {
|
if (!_dragging) {
|
||||||
@@ -24,12 +24,12 @@ var DragHandle = function (handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (_currentAxis === 'h') {
|
if (_currentAxis === 'h') {
|
||||||
handler.canvas.image.left -= e.e.movementX;
|
handler.canvas.getImage().left -= e.e.movementX;
|
||||||
} else {
|
} else {
|
||||||
handler.canvas.image.top -= e.e.movementY;
|
handler.canvas.getImage().top -= e.e.movementY;
|
||||||
}
|
}
|
||||||
// Trigger moving for image, to stay within bounds
|
// Trigger moving for image, to stay within bounds
|
||||||
handler.canvas.image.setCoords().trigger('moving');
|
handler.canvas.getImage().setCoords().trigger('moving');
|
||||||
handler.canvas.renderAll();
|
handler.canvas.renderAll();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -58,7 +58,7 @@ var DragHandle = function (handler) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.init = function () {
|
this.init = function () {
|
||||||
var viewportData = this.getCanvas().viewport.data;
|
var viewportData = this.getCanvas().getViewport().data;
|
||||||
var tracks = handler.getTracks();
|
var tracks = handler.getTracks();
|
||||||
|
|
||||||
if (this.__handles) {
|
if (this.__handles) {
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ function DragbarHandler(canvas) {
|
|||||||
|
|
||||||
_dragbars.h = new fabric.Rect($.extend({}, config.dragBar, {
|
_dragbars.h = new fabric.Rect($.extend({}, config.dragBar, {
|
||||||
axis: 'h',
|
axis: 'h',
|
||||||
width: canvas.image.width,
|
width: canvas.getImage().width,
|
||||||
height: config.dragBar.size,
|
height: config.dragBar.size,
|
||||||
}))
|
}))
|
||||||
.addTo(canvas)
|
.addTo(canvas)
|
||||||
@@ -71,12 +71,12 @@ function DragbarHandler(canvas) {
|
|||||||
_dragbars.v = new fabric.Rect($.extend({}, config.dragBar, {
|
_dragbars.v = new fabric.Rect($.extend({}, config.dragBar, {
|
||||||
axis: 'v',
|
axis: 'v',
|
||||||
width: config.dragBar.size,
|
width: config.dragBar.size,
|
||||||
height: canvas.image.height
|
height: canvas.getImage().height
|
||||||
}))
|
}))
|
||||||
.addTo(canvas)
|
.addTo(canvas)
|
||||||
.set({ left: canvas.width - 40 }).setCoords();
|
.set({ left: canvas.width - 40 }).setCoords();
|
||||||
|
|
||||||
canvas.image.on('moving', this.sync);
|
canvas.getImage().on('moving', this.sync);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
@@ -85,8 +85,8 @@ function DragbarHandler(canvas) {
|
|||||||
* @return {DragbarHandler}
|
* @return {DragbarHandler}
|
||||||
*/
|
*/
|
||||||
this.sync = function () {
|
this.sync = function () {
|
||||||
_dragbars.h.set({left: canvas.image.left}).setCoords();
|
_dragbars.h.set({left: canvas.getImage().left}).setCoords();
|
||||||
_dragbars.v.set({top: canvas.image.top}).setCoords();
|
_dragbars.v.set({top: canvas.getImage().top}).setCoords();
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ function ImageEditor(args) {
|
|||||||
// Initialize canvas
|
// Initialize canvas
|
||||||
_canvas = new Canvas('canvas');
|
_canvas = new Canvas('canvas');
|
||||||
|
|
||||||
_canvas.buttonMenu
|
_canvas.getButtonMenu()
|
||||||
.addItem(['Show Murals Panel', 'Hide Murals Panel'], function () {
|
.addItem(['Show Murals Panel', 'Hide Murals Panel'], function () {
|
||||||
_canvas.viewport.gores.enable(this.isActive());
|
_canvas.getViewport().gores.enable(this.isActive());
|
||||||
})
|
})
|
||||||
.addItem(['Show Ruler', 'Hide Ruler'], function () {
|
.addItem(['Show Ruler', 'Hide Ruler'], function () {
|
||||||
_canvas.viewport.rulers.enable(this.isActive());
|
_canvas.getViewport().rulers.enable(this.isActive());
|
||||||
})
|
})
|
||||||
.addItem(['Fullscreen', 'Exit Fullscreen'], function () {
|
.addItem(['Fullscreen', 'Exit Fullscreen'], function () {
|
||||||
util.toggleFullscreen(_canvas.getSelectionElement().parentNode);
|
util.toggleFullscreen(_canvas.getSelectionElement().parentNode);
|
||||||
@@ -32,7 +32,7 @@ function ImageEditor(args) {
|
|||||||
* @return {ImageEditor}
|
* @return {ImageEditor}
|
||||||
*/
|
*/
|
||||||
this.crop = function (width, height) {
|
this.crop = function (width, height) {
|
||||||
_canvas.viewport.set(width, height);
|
_canvas.getViewport().set(width, height);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -29,13 +29,13 @@ function Viewport(canvas) {
|
|||||||
_rect.height += _borderWidth;
|
_rect.height += _borderWidth;
|
||||||
_rect.addTo(canvas).center().setCoords();
|
_rect.addTo(canvas).center().setCoords();
|
||||||
|
|
||||||
canvas.image
|
canvas.getImage()
|
||||||
.center()
|
.center()
|
||||||
.drag.enable(calcMinMaxBoundsForRect(_rect.getInsideBoundingRect(), canvas.image));
|
.drag.enable(calcMinMaxBoundsForRect(_rect.getInsideBoundingRect(), canvas.getImage()));
|
||||||
canvas.aperture.apply(_data.axis, _rect.getBoundingRect());
|
canvas.getApertures().apply(_data.axis, _rect.getBoundingRect());
|
||||||
|
|
||||||
drawBeams();
|
drawBeams();
|
||||||
canvas.dragbars.apply();
|
canvas.getDragbars().apply();
|
||||||
|
|
||||||
// Apertures sometimes overlaps the viewports bounding rect.
|
// Apertures sometimes overlaps the viewports bounding rect.
|
||||||
// Solve this by bringing it to the front after apertures are applied.
|
// Solve this by bringing it to the front after apertures are applied.
|
||||||
@@ -115,13 +115,13 @@ function Viewport(canvas) {
|
|||||||
* @return {Viewport}
|
* @return {Viewport}
|
||||||
*/
|
*/
|
||||||
this.set = function (width, height) {
|
this.set = function (width, height) {
|
||||||
_data = util.calcCrop(canvas.image, width, height);
|
_data = util.calcCrop(canvas.getImage(), width, height);
|
||||||
apply();
|
apply();
|
||||||
this.gores.reset();
|
this.gores.reset();
|
||||||
this.rulers.reset();
|
this.rulers.reset();
|
||||||
canvas.aperture.setTransparent(true);
|
canvas.getApertures().setTransparent(true);
|
||||||
canvas.image.__moved = false;
|
canvas.getImage().__moved = false;
|
||||||
canvas.buttonMenu.bringToFront();
|
canvas.getButtonMenu().bringToFront();
|
||||||
};
|
};
|
||||||
|
|
||||||
util.setProperties(this, {
|
util.setProperties(this, {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var Draggable = require('../Draggable');
|
var Draggable = require('./ImageEditor/Draggable');
|
||||||
var ImageTransformationHandler = require('../ImageTransformationHandler');
|
var ImageTransformationHandler = require('./ImageEditor/ImageTransformationHandler');
|
||||||
var util = require('../../util');
|
var util = require('./util');
|
||||||
|
|
||||||
function ExtendFabricFunctionality() {
|
function ExtendFabricFunctionality() {
|
||||||
|
|
||||||
|
|||||||
+8
-4
@@ -6,7 +6,7 @@
|
|||||||
var ImageEditor = require('./components/ImageEditor/ImageEditor');
|
var ImageEditor = require('./components/ImageEditor/ImageEditor');
|
||||||
|
|
||||||
ExtendFabricFunctionality();
|
ExtendFabricFunctionality();
|
||||||
ImageEditor();
|
window.ImageEditor = ImageEditor;
|
||||||
|
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -31,11 +31,15 @@ $(function () {
|
|||||||
editor.crop($input.w.val(), $input.h.val());
|
editor.crop($input.w.val(), $input.h.val());
|
||||||
},
|
},
|
||||||
Mirror: function () {
|
Mirror: function () {
|
||||||
editor.canvas.image.transformation.set('mirror', this.checked);
|
editor.canvas.getImage().transformation.set('mirror', this.checked);
|
||||||
}
|
},
|
||||||
|
NoCrop: function () {
|
||||||
|
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
$input.w.keyup(Event.DimensionChange);
|
$input.w.keyup(Event.DimensionChange);
|
||||||
$input.h.keyup(Event.DimensionChange);
|
$input.h.keyup(Event.DimensionChange);
|
||||||
$input.mirror.click(Event.Mirror);
|
$input.mirror.change(Event.Mirror);
|
||||||
|
$input.mirror.change(Event.NoCrop);
|
||||||
}());
|
}());
|
||||||
|
|||||||
Reference in New Issue
Block a user