UNPKG

awayjs-display

Version:
189 lines (188 loc) 8.06 kB
"use strict"; var GraphicsPathWinding_1 = require("../draw/GraphicsPathWinding"); var GraphicsPathCommand_1 = require("../draw/GraphicsPathCommand"); var Point_1 = require("awayjs-core/lib/geom/Point"); /** * Defines the values to use for specifying path-drawing commands. * The values in this class are used by the Graphics.drawPath() method, *or stored in the commands vector of a GraphicsPath object. */ var GraphicsPath = (function () { function GraphicsPath(commands, data, winding) { if (commands === void 0) { commands = null; } if (data === void 0) { data = null; } if (winding === void 0) { winding = GraphicsPathWinding_1.default.EVEN_ODD; } this._data = []; this._commands = []; this._draw_directions = [0]; this._contours_closed = [false]; if (commands != null && data != null) { this._data[0] = data; this._commands[0] = commands; } else { this._data[0] = []; this._commands[0] = []; } this._direction = new Point_1.default(0, -1); this._startPoint = new Point_1.default(); this._cur_point = new Point_1.default(); this._tmp_point = new Point_1.default(); this.isFill = false; this._winding = winding; } Object.defineProperty(GraphicsPath.prototype, "draw_directions", { get: function () { return this._draw_directions; }, enumerable: true, configurable: true }); Object.defineProperty(GraphicsPath.prototype, "contours_closed", { get: function () { return this._contours_closed; }, enumerable: true, configurable: true }); Object.defineProperty(GraphicsPath.prototype, "isFill", { get: function () { return this._isFill; }, set: function (value) { this._isFill = value; }, enumerable: true, configurable: true }); Object.defineProperty(GraphicsPath.prototype, "commands", { get: function () { return this._commands; }, enumerable: true, configurable: true }); Object.defineProperty(GraphicsPath.prototype, "data", { get: function () { return this._data; }, enumerable: true, configurable: true }); GraphicsPath.prototype.curveTo = function (controlX, controlY, anchorX, anchorY) { if (this._commands[this._commands.length - 1].length == 0) { // every contour must start with a moveTo command, so we make sure we have correct startpoint this._commands[this._commands.length - 1].push(GraphicsPathCommand_1.default.MOVE_TO); this._data[this._data.length - 1].push(this._cur_point.x); this._data[this._data.length - 1].push(this._cur_point.y); } this._commands[this._commands.length - 1].push(GraphicsPathCommand_1.default.CURVE_TO); if (this.isFill) { this._tmp_point.x = anchorX - this._cur_point.x; this._tmp_point.y = anchorY - this._cur_point.y; this._tmp_point.normalize(); var testpoint = new Point_1.default(this._tmp_point.x, this._tmp_point.y); testpoint.normalize(); var degree_anchor = Math.acos(this._tmp_point.x * this._direction.x + this._tmp_point.y * this._direction.y) * 180 / Math.PI; if (degree_anchor > 180) degree_anchor -= 360; //var degree_anchor:number=Math.atan2(this._tmp_point.x, this._tmp_point.y) * 180 / Math.PI; this._draw_directions[this._draw_directions.length - 1] += degree_anchor; this._tmp_point.x = controlX - this._cur_point.x; this._tmp_point.y = controlY - this._cur_point.y; this._tmp_point.normalize(); //angle = atan2( a.x*b.y - a.y*b.x, a.x*b.x + a.y*b.y ); var degree_control = (Math.atan2(this._tmp_point.x * testpoint.y - this._tmp_point.y * testpoint.x, this._tmp_point.x * testpoint.x + this._tmp_point.y * testpoint.y)); if (degree_control > 180) degree_control -= 360; //var degree_control:number=(Math.atan2(this._tmp_point.x, this._tmp_point.y) * 180 / Math.PI); console.log("degree_control " + degree_control); console.log("degree_anchor " + degree_anchor); console.log("this._draw_directions[this._draw_directions.length-1] " + this._draw_directions[this._draw_directions.length - 1]); this._direction.x = testpoint.x; this._direction.y = testpoint.y; if ((degree_control) < 0) this._data[this._data.length - 1].push(1); else this._data[this._data.length - 1].push(2); } else { this._data[this._data.length - 1].push(1); } this._cur_point.x = anchorX; this._cur_point.y = anchorY; this._data[this._data.length - 1].push(controlX); this._data[this._data.length - 1].push(controlY); this._data[this._data.length - 1].push(anchorX); this._data[this._data.length - 1].push(anchorY); }; GraphicsPath.prototype.lineTo = function (x, y) { if (this._commands[this._commands.length - 1].length == 0) { // every contour must start with a moveTo command, so we make sure we have correct startpoint this._commands[this._commands.length - 1].push(GraphicsPathCommand_1.default.MOVE_TO); this._data[this._data.length - 1].push(this._cur_point.x); this._data[this._data.length - 1].push(this._cur_point.y); } this._commands[this._commands.length - 1].push(GraphicsPathCommand_1.default.LINE_TO); this._data[this._data.length - 1].push(x); this._data[this._data.length - 1].push(y); if (this.isFill) { this._tmp_point.x = x - this._cur_point.x; this._tmp_point.y = y - this._cur_point.y; this._tmp_point.normalize(); this._direction.x = this._tmp_point.x; this._direction.y = this._tmp_point.y; var degree_anchor = Math.atan2(this._tmp_point.x, this._tmp_point.y) * 180 / Math.PI; this._draw_directions[this._draw_directions.length - 1] += degree_anchor; } this._cur_point.x = x; this._cur_point.y = y; }; GraphicsPath.prototype.moveTo = function (x, y) { if (this._commands[this._commands.length - 1].length > 0) { this.finalizeContour(); this._draw_directions.push(0); this._contours_closed.push(false); this._commands.push([]); this._data.push([]); } this._startPoint.x = x; this._startPoint.y = y; this._cur_point.x = x; this._cur_point.y = y; }; GraphicsPath.prototype.finalizeContour = function () { if ((this._startPoint.x != this._cur_point.x) || (this._startPoint.y != this._cur_point.y)) { if (this.isFill) { this.lineTo(this._startPoint.x, this._startPoint.y); } } else { this._contours_closed[this._contours_closed.length - 1] = true; } }; GraphicsPath.prototype.wideLineTo = function (x, y) { // not used /* this._commands.push(GraphicsPathCommand.WIDE_LINE_TO); this._data.push(0); this._data.push(0); this._data.push(x); this._data.push(y); */ }; GraphicsPath.prototype.wideMoveTo = function (x, y) { // not used /* this._commands.push(GraphicsPathCommand.WIDE_MOVE_TO); this._data.push(0); this._data.push(0); this._data.push(x); this._data.push(y); */ }; return GraphicsPath; }()); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = GraphicsPath; //# sourceMappingURL=GraphicsPath.js.map