2010-10-31 01:47:15 +00:00
|
|
|
/**
|
|
|
|
|
* Augments canvas by assigning to `onObjectMove` and `onAfterRender`.
|
|
|
|
|
* This kind of sucks because other code using those methods will stop functioning.
|
|
|
|
|
* Need to fix it by replacing callbacks with pub/sub kind of subscription model.
|
2011-06-27 22:25:45 +00:00
|
|
|
* (or maybe use existing fabric.util.fire/observe (if it won't be too slow))
|
2010-10-31 01:47:15 +00:00
|
|
|
*/
|
|
|
|
|
function initCenteringGuidelines(canvas) {
|
|
|
|
|
|
|
|
|
|
var canvasWidth = canvas.getWidth(),
|
|
|
|
|
canvasHeight = canvas.getHeight(),
|
|
|
|
|
canvasWidthCenter = canvasWidth / 2,
|
|
|
|
|
canvasHeightCenter = canvasHeight / 2,
|
|
|
|
|
canvasWidthCenterMap = { },
|
|
|
|
|
canvasHeightCenterMap = { },
|
|
|
|
|
centerLineMargin = 4,
|
|
|
|
|
centerLineColor = 'rgba(255,0,241,0.5)',
|
|
|
|
|
centerLineWidth = 1,
|
|
|
|
|
ctx = canvas.getContext();
|
|
|
|
|
|
|
|
|
|
for (var i = canvasWidthCenter - centerLineMargin, len = canvasWidthCenter + centerLineMargin; i <= len; i++) {
|
|
|
|
|
canvasWidthCenterMap[i] = true;
|
|
|
|
|
}
|
|
|
|
|
for (var i = canvasHeightCenter - centerLineMargin, len = canvasHeightCenter + centerLineMargin; i <= len; i++) {
|
|
|
|
|
canvasHeightCenterMap[i] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showVerticalCenterLine() {
|
|
|
|
|
showCenterLine(canvasWidthCenter + 0.5, 0, canvasWidthCenter + 0.5, canvasHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showHorizontalCenterLine() {
|
|
|
|
|
showCenterLine(0, canvasHeightCenter + 0.5, canvasWidth, canvasHeightCenter + 0.5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showCenterLine(x1, y1, x2, y2) {
|
|
|
|
|
ctx.save();
|
|
|
|
|
ctx.strokeStyle = centerLineColor;
|
|
|
|
|
ctx.lineWidth = centerLineWidth;
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.moveTo(x1, y1);
|
|
|
|
|
ctx.lineTo(x2, y2);
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
ctx.restore();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-27 22:25:45 +00:00
|
|
|
var afterRenderActions = [ ],
|
2010-11-01 02:42:20 +00:00
|
|
|
isInVerticalCenter,
|
|
|
|
|
isInHorizontalCenter;
|
|
|
|
|
|
2011-07-06 19:12:44 +00:00
|
|
|
canvas.observe('object:moving', function(e) {
|
2010-11-01 02:42:20 +00:00
|
|
|
object = e.memo.target;
|
|
|
|
|
|
|
|
|
|
isInVerticalCenter = object.get('left') in canvasWidthCenterMap,
|
|
|
|
|
isInHorizontalCenter = object.get('top') in canvasHeightCenterMap;
|
2010-10-31 01:47:15 +00:00
|
|
|
|
2010-11-01 02:42:20 +00:00
|
|
|
if (isInHorizontalCenter) {
|
|
|
|
|
object.set('top', canvasHeightCenter);
|
|
|
|
|
}
|
|
|
|
|
if (isInVerticalCenter) {
|
|
|
|
|
object.set('left', canvasWidthCenter);
|
2010-10-31 01:47:15 +00:00
|
|
|
}
|
2010-11-01 02:42:20 +00:00
|
|
|
});
|
|
|
|
|
|
2011-06-27 22:25:45 +00:00
|
|
|
canvas.observe('after:render', function() {
|
2010-11-01 02:42:20 +00:00
|
|
|
if (isInVerticalCenter) {
|
|
|
|
|
showVerticalCenterLine();
|
|
|
|
|
}
|
|
|
|
|
if (isInHorizontalCenter) {
|
|
|
|
|
showHorizontalCenterLine();
|
2010-10-31 01:47:15 +00:00
|
|
|
}
|
2010-11-01 02:42:20 +00:00
|
|
|
});
|
|
|
|
|
|
2011-06-27 22:25:45 +00:00
|
|
|
canvas.observe('mouse:up', function() {
|
2010-11-01 02:42:20 +00:00
|
|
|
// clear these values, to stop drawing guidelines once mouse is up
|
|
|
|
|
isInVerticalCenter = isInHorizontalCenter = null;
|
2010-10-31 01:47:15 +00:00
|
|
|
canvas.renderAll();
|
2010-11-01 02:42:20 +00:00
|
|
|
});
|
2010-10-31 01:47:15 +00:00
|
|
|
}
|