2013-09-28 23:10:12 +04:00
|
|
|
/**
|
2012-09-15 00:20:34 +04:00
|
|
|
* @provide pskl.drawingtools.Circle
|
|
|
|
*
|
|
|
|
* @require pskl.utils
|
|
|
|
*/
|
|
|
|
(function() {
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.drawingtools");
|
2012-09-15 00:20:34 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.Circle = function() {
|
|
|
|
this.toolId = "tool-circle";
|
|
|
|
this.helpText = "Circle tool";
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
// Circle's first point coordinates (set in applyToolAt)
|
|
|
|
this.startCol = null;
|
|
|
|
this.startRow = null;
|
|
|
|
};
|
2012-09-15 00:20:34 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
pskl.utils.inherit(ns.Circle, ns.BaseTool);
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
ns.Circle.prototype.applyToolAt = function(col, row, color, frame, overlay) {
|
|
|
|
this.startCol = col;
|
|
|
|
this.startRow = row;
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
// Drawing the first point of the rectangle in the fake overlay canvas:
|
|
|
|
overlay.setPixel(col, row, color);
|
|
|
|
};
|
2012-09-15 00:20:34 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.Circle.prototype.moveToolAt = function(col, row, color, frame, overlay) {
|
|
|
|
overlay.clear();
|
|
|
|
if(color == Constants.TRANSPARENT_COLOR) {
|
|
|
|
color = Constants.SELECTION_TRANSPARENT_COLOR;
|
|
|
|
}
|
2012-09-15 00:20:34 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
// draw in overlay
|
|
|
|
this.drawCircle_(col, row, color, overlay);
|
|
|
|
};
|
2012-09-15 00:20:34 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
2013-09-29 01:52:51 +04:00
|
|
|
ns.Circle.prototype.releaseToolAt = function(col, row, color, frame, overlay) {
|
2013-08-10 14:11:16 +04:00
|
|
|
overlay.clear();
|
|
|
|
if(frame.containsPixel(col, row)) { // cancel if outside of canvas
|
|
|
|
// draw in frame to finalize
|
|
|
|
this.drawCircle_(col, row, color, frame);
|
|
|
|
}
|
|
|
|
};
|
2012-09-15 01:43:49 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.Circle.prototype.drawCircle_ = function (col, row, color, targetFrame) {
|
|
|
|
var circlePoints = this.getCirclePixels_(this.startCol, this.startRow, col, row);
|
|
|
|
for(var i = 0; i< circlePoints.length; i++) {
|
|
|
|
// Change model:
|
|
|
|
targetFrame.setPixel(circlePoints[i].col, circlePoints[i].row, color);
|
|
|
|
}
|
|
|
|
};
|
2012-09-15 01:43:49 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.Circle.prototype.getCirclePixels_ = function (x0, y0, x1, y1) {
|
|
|
|
var coords = pskl.PixelUtils.getOrderedRectangleCoordinates(x0, y0, x1, y1);
|
|
|
|
var xC = (coords.x0 + coords.x1)/2;
|
|
|
|
var yC = (coords.y0 + coords.y1)/2;
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var rX = coords.x1 - xC;
|
|
|
|
var rY = coords.y1 - yC;
|
2012-09-15 01:43:49 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var pixels = [];
|
|
|
|
var x, y, angle;
|
|
|
|
for (x = coords.x0 ; x < coords.x1 ; x++) {
|
|
|
|
angle = Math.acos((x - xC)/rX);
|
|
|
|
y = Math.round(rY * Math.sin(angle) + yC);
|
|
|
|
pixels.push({"col": x, "row": y});
|
|
|
|
pixels.push({"col": 2*xC - x, "row": 2*yC - y});
|
|
|
|
}
|
2012-09-15 01:43:49 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
for (y = coords.y0 ; y < coords.y1 ; y++) {
|
|
|
|
angle = Math.asin((y - yC)/rY);
|
|
|
|
x = Math.round(rX * Math.cos(angle) + xC);
|
|
|
|
pixels.push({"col": x, "row": y});
|
|
|
|
pixels.push({"col": 2*xC - x, "row": 2*yC - y});
|
|
|
|
}
|
|
|
|
return pixels;
|
|
|
|
};
|
2012-09-15 00:20:34 +04:00
|
|
|
})();
|