143 lines
3.7 KiB
JavaScript
143 lines
3.7 KiB
JavaScript
'use strict';
|
|
|
|
var config = require('./config');
|
|
|
|
function FrameHandler(viewport) {
|
|
var _frames;
|
|
var _frameType = 'image';
|
|
|
|
function init() {
|
|
initFrames();
|
|
}
|
|
|
|
function getFramesAsArray() {
|
|
return [
|
|
_frames.top, _frames.right, _frames.bottom, _frames.left
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Sets the 4 rectangles which are used when current canvas should be framed
|
|
*/
|
|
function initFrames() {
|
|
var frame = new fabric.Rect(config.frames.default);
|
|
|
|
_frames = {
|
|
top: frame.clone(),
|
|
right: frame.clone(),
|
|
bottom: frame.clone(),
|
|
left: frame.clone()
|
|
};
|
|
|
|
getFramesAsArray().forEach(function (o) {
|
|
viewport.canvas.add(o);
|
|
});
|
|
}
|
|
|
|
init();
|
|
|
|
/**
|
|
* @return {String}
|
|
*/
|
|
this.getFrameType = function () {
|
|
return _frameType;
|
|
};
|
|
|
|
this.setVisible = function (isVisible) {
|
|
isVisible = typeof isVisible === 'undefined' ? true : !!isVisible;
|
|
|
|
getFramesAsArray().forEach(function (frame) {
|
|
frame.set({ visible: isVisible });
|
|
});
|
|
|
|
viewport.canvas.renderAll();
|
|
|
|
return this;
|
|
};
|
|
|
|
/**
|
|
* Sets passed frametype to passed viewwport
|
|
* @param {string} frameType none|image|black|white
|
|
* @return {FrameHandler}
|
|
*/
|
|
this.setFrames = function (frameType) {
|
|
var bounds = viewport.getBounds();
|
|
|
|
frameType = frameType || _frameType || 'image';
|
|
_frameType = frameType;
|
|
|
|
if (!frameType) {
|
|
return;
|
|
}
|
|
|
|
if (frameType === 'none') {
|
|
return this.setVisible(false);
|
|
}
|
|
|
|
this.setVisible(true);
|
|
|
|
getFramesAsArray().forEach(function (o) {
|
|
o.set(config.frames[frameType]);
|
|
});
|
|
|
|
var vpData = viewport.getData();
|
|
var frameSize = 29;
|
|
if (vpData.dim.units === 'inch') {
|
|
frameSize /= 2.54;
|
|
}
|
|
|
|
_frames.top.height =
|
|
_frames.bottom.height =
|
|
_frames.right.width =
|
|
_frames.left.width = vpData.ppmm * frameSize * viewport.canvas.getImage().scaleX;
|
|
|
|
_frames.top.width = _frames.bottom.width = vpData.width;
|
|
_frames.right.height = _frames.left.height = vpData.height;
|
|
|
|
if (frameType === 'image') {
|
|
_frames.top.width -= (vpData.ppmm * frameSize * viewport.canvas.getImage().scaleX) * 2;
|
|
_frames.bottom.width = _frames.top.width;
|
|
_frames.right.height -= (vpData.ppmm * frameSize * viewport.canvas.getImage().scaleX) * 2;
|
|
_frames.left.height = _frames.right.height;
|
|
|
|
_frames.top.set({
|
|
top: bounds.top,
|
|
}).centerH().setCoords();
|
|
|
|
_frames.bottom.set({
|
|
top: (bounds.top + bounds.height - _frames.bottom.height) - 1
|
|
}).centerH().setCoords();
|
|
|
|
_frames.right.set({
|
|
left: (bounds.left + bounds.width - _frames.right.width) - 1
|
|
}).centerV().setCoords();
|
|
|
|
_frames.left.set({
|
|
left: bounds.left
|
|
}).centerV().setCoords();
|
|
} else {
|
|
_frames.top.set({
|
|
top: (bounds.top - _frames.top.height) + 1,
|
|
}).centerH().setCoords();
|
|
|
|
_frames.bottom.set({
|
|
top: (bounds.top + bounds.height) - 1,
|
|
}).centerH().setCoords();
|
|
|
|
_frames.right.set({
|
|
left: (bounds.left + bounds.width) - 1
|
|
}).centerV().setCoords();
|
|
|
|
_frames.left.set({
|
|
left: (bounds.left - _frames.left.width) + 1
|
|
}).centerV().setCoords();
|
|
}
|
|
|
|
viewport.canvas.renderAll();
|
|
|
|
return this;
|
|
};
|
|
}
|
|
|
|
module.exports = FrameHandler;
|