@jsmlt/jsmlt
Version:
JavaScript Machine Learning
109 lines (85 loc) • 4.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
/**
* Data point element to be drawn on the canvas
*/
var Datapoint =
/*#__PURE__*/
function () {
/**
* Constructor. Attach the datapoint to the canvas and its model.
*
* @param {jsml.UI.Canvas} canvas - Canvas to which this datapoint element is bound
* @param {jsml.Dataset.Datapoint} datapoint - Datapoint model
*/
function Datapoint(canvas, datapoint) {
_classCallCheck(this, Datapoint);
this.canvas = canvas;
this.model = datapoint;
this.radius = 0;
this.steps = 0;
}
/**
* Update information about this element from the model.
*/
_createClass(Datapoint, [{
key: "updateFromModel",
value: function updateFromModel() {
var _this$model$features = _slicedToArray(this.model.features, 2);
this.x = _this$model$features[0];
this.y = _this$model$features[1];
this.color = this.canvas.getClassColor(this.model.classIndex);
this.marked = this.model.isMarked();
}
/**
* Update drawing properties of the model.
*/
}, {
key: "update",
value: function update() {
this.updateFromModel(); // Update radius
var progress = Math.min(this.steps / 20, 0.75);
this.radius = Math.sin(progress * Math.PI) * 6; // Increase current step
this.steps += 1;
}
/**
* Draw the element on the canvas.
*/
}, {
key: "draw",
value: function draw() {
var context = this.canvas.canvas.context; // Calculate position of point
var _this$canvas$convertF = this.canvas.convertFeaturesToCanvasCoordinates(this.x, this.y),
_this$canvas$convertF2 = _slicedToArray(_this$canvas$convertF, 2),
pointCx = _this$canvas$convertF2[0],
pointCy = _this$canvas$convertF2[1]; // Draw main point filled, stroked circle
context.beginPath();
context.arc(pointCx, pointCy, this.radius, 0, 2 * Math.PI, false);
context.fillStyle = this.color;
context.fill();
context.lineWidth = 1;
context.strokeStyle = '#555';
context.stroke(); // Mark point (e.g. for Support Vectors)
if (this.marked) {
context.beginPath();
context.arc(pointCx, pointCy, this.radius + 3, 0, 2 * Math.PI, false);
context.lineWidth = 1;
context.strokeStyle = '#555'; // this.color;
context.stroke();
}
}
}]);
return Datapoint;
}();
exports["default"] = Datapoint;
module.exports = exports.default;