UNPKG

medsurf-draw

Version:

Draw annotations on jpg/zoomify images, based on PIXI.js

451 lines 27 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import * as PIXI from "pixi.js-legacy"; import * as MedsurfDraw from "../../public-api"; import { Design } from "../../config/design"; import { BaseContainer, BaseContainerImageModel } from "../../bases/elements/BaseContainer"; import { debounce } from "debounce"; export var ScaleElementType; (function (ScaleElementType) { ScaleElementType[ScaleElementType["NOT_SCALEABLE"] = 0] = "NOT_SCALEABLE"; ScaleElementType[ScaleElementType["SCALEABLE"] = 1] = "SCALEABLE"; ScaleElementType[ScaleElementType["TEXT"] = 2] = "TEXT"; })(ScaleElementType || (ScaleElementType = {})); var ScaleElementModel = (function (_super) { __extends(ScaleElementModel, _super); function ScaleElementModel() { return _super !== null && _super.apply(this, arguments) || this; } return ScaleElementModel; }(BaseContainerImageModel)); export { ScaleElementModel }; var ScaleElement = (function (_super) { __extends(ScaleElement, _super); function ScaleElement(model) { var _this = _super.call(this, model) || this; _this.zIndex = Design.scale.zIndex; _this._debounceTypeChangedMethod = debounce(_this._typeChanged.bind(_this), 20).bind(_this); _this.on("added", function () { _this.on("debounceTypeChanged", _this._debounceTypeChangedMethod); }); _this.on("removed", function () { _this.off("debounceTypeChanged", _this._debounceTypeChangedMethod); }); return _this; } ScaleElement.prototype.init = function () { this._rectangleElement = new MedsurfDraw.Rectangle({ rectangle: new PIXI.Rectangle(0, 0, 0, 0), options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: false }, lineWidth: Design.scale.lineWidth }); this.addChild(this._rectangleElement); this._topLeftCircleElement = new MedsurfDraw.Circle({ circle: new PIXI.Circle(0, 0, Design.scale.cornerRadius), options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: true, fillColor: Design.scale.fillColorNormal, fillAlpha: Design.scale.fillAlpha }, lineWidth: Design.scale.lineWidth }); this._topLeftCircleElement.interactive = true; this._topLeftCircleElement.cursor = "scale-nwse"; this._topLeftCircleElement.on("pointerover", this.onPointerOver, this); this._topLeftCircleElement.on("mousedown", this.onPointerDownXY, this); this._topLeftCircleElement.on("pointermove", this.onScale, this); this._topLeftCircleElement.on("mouseup", this.endScale, this); this._topLeftCircleElement.on("pointerupoutside", this.endScale, this); this.addChild(this._topLeftCircleElement); this._topRightCircleElement = new MedsurfDraw.Circle({ circle: new PIXI.Circle(0, 0, Design.scale.cornerRadius), options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: true, fillColor: Design.scale.fillColorNormal, fillAlpha: Design.scale.fillAlpha }, lineWidth: Design.scale.lineWidth }); this._topRightCircleElement.interactive = true; this._topRightCircleElement.cursor = "scale-nesw"; this._topRightCircleElement.on("pointerover", this.onPointerOver, this); this._topRightCircleElement.on("mousedown", this.onPointerDownWidthY, this); this._topRightCircleElement.on("pointermove", this.onScale, this); this._topRightCircleElement.on("mouseup", this.endScale, this); this._topRightCircleElement.on("pointerupoutside", this.endScale, this); this.addChild(this._topRightCircleElement); this._bottomRightCircleElement = new MedsurfDraw.Circle({ circle: new PIXI.Circle(0, 0, Design.scale.cornerRadius), options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: true, fillColor: Design.scale.fillColorNormal, fillAlpha: Design.scale.fillAlpha }, lineWidth: Design.scale.lineWidth }); this._bottomRightCircleElement.interactive = true; this._bottomRightCircleElement.cursor = "scale-nwse"; this._bottomRightCircleElement.on("pointerover", this.onPointerOver, this); this._bottomRightCircleElement.on("mousedown", this.onPointerDownWidthHeight, this); this._bottomRightCircleElement.on("pointermove", this.onScale, this); this._bottomRightCircleElement.on("mouseup", this.endScale, this); this._bottomRightCircleElement.on("pointerupoutside", this.endScale, this); this.addChild(this._bottomRightCircleElement); this._bottomLeftCircleElement = new MedsurfDraw.Circle({ circle: new PIXI.Circle(0, 0, Design.scale.cornerRadius), options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: true, fillColor: Design.scale.fillColorNormal, fillAlpha: Design.scale.fillAlpha }, lineWidth: Design.scale.lineWidth }); this._bottomLeftCircleElement.interactive = true; this._bottomLeftCircleElement.cursor = "scale-nesw"; this._bottomLeftCircleElement.on("pointerover", this.onPointerOver, this); this._bottomLeftCircleElement.on("mousedown", this.onPointerDownXHeight, this); this._bottomLeftCircleElement.on("pointermove", this.onScale, this); this._bottomLeftCircleElement.on("mouseup", this.endScale, this); this._bottomLeftCircleElement.on("pointerupoutside", this.endScale, this); this.addChild(this._bottomLeftCircleElement); this._topLineElement = new MedsurfDraw.StraightLine({ points: { start: new PIXI.Point(0, 0), end: new PIXI.Point(0, 0), }, options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: true, fillColor: Design.scale.fillColorNormal, fillAlpha: Design.scale.fillAlpha }, lineWidth: Design.scale.lineWidth, }); this._topLineElement.interactive = true; this._topLineElement.cursor = "scale-v"; this._topLineElement.on("pointerover", this.onPointerOver, this); this._topLineElement.on("mousedown", this.onPointerDownY, this); this._topLineElement.on("pointermove", this.onScale, this); this._topLineElement.on("mouseup", this.endScale, this); this._topLineElement.on("pointerupoutside", this.endScale, this); this.addChild(this._topLineElement); this._rightLineElement = new MedsurfDraw.StraightLine({ points: { start: new PIXI.Point(0, 0), end: new PIXI.Point(0, 0), }, options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: true, fillColor: Design.scale.fillColorNormal, fillAlpha: Design.scale.fillAlpha }, lineWidth: Design.scale.lineWidth, }); this._rightLineElement.interactive = true; this._rightLineElement.cursor = "scale-h"; this._rightLineElement.on("pointerover", this.onPointerOver, this); this._rightLineElement.on("mousedown", this.onPointerDownWidth, this); this._rightLineElement.on("pointermove", this.onScale, this); this._rightLineElement.on("mouseup", this.endScale, this); this._rightLineElement.on("pointerupoutside", this.endScale, this); this.addChild(this._rightLineElement); this._bottomLineElement = new MedsurfDraw.StraightLine({ points: { start: new PIXI.Point(0, 0), end: new PIXI.Point(0, 0), }, options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: true, fillColor: Design.scale.fillColorNormal, fillAlpha: Design.scale.fillAlpha }, lineWidth: Design.scale.lineWidth, }); this._bottomLineElement.interactive = true; this._bottomLineElement.cursor = "scale-v"; this._bottomLineElement.on("pointerover", this.onPointerOver, this); this._bottomLineElement.on("mousedown", this.onPointerDownHeight, this); this._bottomLineElement.on("pointermove", this.onScale, this); this._bottomLineElement.on("mouseup", this.endScale, this); this._bottomLineElement.on("pointerupoutside", this.endScale, this); this.addChild(this._bottomLineElement); this._leftLineElement = new MedsurfDraw.StraightLine({ points: { start: new PIXI.Point(0, 0), end: new PIXI.Point(0, 0), }, options: { hasLine: true, lineColor: Design.scale.lineColorNormal, lineAlpha: Design.scale.lineAlpha, hasFill: true, fillColor: Design.scale.fillColorNormal, fillAlpha: Design.scale.fillAlpha }, lineWidth: Design.scale.lineWidth, }); this._leftLineElement.interactive = true; this._leftLineElement.cursor = "scale-h"; this._leftLineElement.on("pointerover", this.onPointerOver, this); this._leftLineElement.on("mousedown", this.onPointerDownX, this); this._leftLineElement.on("pointermove", this.onScale, this); this._leftLineElement.on("mouseup", this.endScale, this); this._leftLineElement.on("pointerupoutside", this.endScale, this); this.addChild(this._leftLineElement); this.sortChildren(); this.emit("debounceDraw"); }; ScaleElement.prototype.draw = function () { if (this._rectangleElement) { this._rectangleElement.position.set(this.rectangle.x, this.rectangle.y); this._rectangleElement.rectangle = new PIXI.Rectangle(0, 0, this.rectangle.width, this.rectangle.height); this._rectangleElement.draw(); } if (this._topLeftCircleElement) { this._topLeftCircleElement.position.set(this.rectangle.x, this.rectangle.y); this._topLeftCircleElement.draw(); } if (this._topRightCircleElement) { this._topRightCircleElement.position.set(this.rectangle.x + this.rectangle.width, this.rectangle.y); this._topRightCircleElement.draw(); } if (this._bottomRightCircleElement) { this._bottomRightCircleElement.position.set(this.rectangle.x + this.rectangle.width, this.rectangle.y + this.rectangle.height); this._bottomRightCircleElement.draw(); } if (this._bottomLeftCircleElement) { this._bottomLeftCircleElement.position.set(this.rectangle.x, this.rectangle.y + this.rectangle.height); this._bottomLeftCircleElement.draw(); } if (this._topLineElement) { this._topLineElement.points = { start: new PIXI.Point(this._topLeftCircleElement.position.x + Design.scale.cornerRadius, this._topLeftCircleElement.position.y), end: new PIXI.Point(this._topRightCircleElement.position.x - Design.scale.cornerRadius, this._topRightCircleElement.position.y), }; this._topLineElement.draw(); } if (this._rightLineElement) { this._rightLineElement.points = { start: new PIXI.Point(this._topRightCircleElement.position.x, this._topRightCircleElement.position.y + Design.scale.cornerRadius), end: new PIXI.Point(this._bottomRightCircleElement.position.x, this._bottomRightCircleElement.position.y - Design.scale.cornerRadius), }; this._rightLineElement.draw(); } if (this._bottomLineElement) { this._bottomLineElement.points = { start: new PIXI.Point(this._bottomRightCircleElement.position.x - Design.scale.cornerRadius, this._bottomRightCircleElement.position.y), end: new PIXI.Point(this._bottomLeftCircleElement.position.x + Design.scale.cornerRadius, this._bottomLeftCircleElement.position.y), }; this._bottomLineElement.draw(); } if (this._leftLineElement) { this._leftLineElement.points = { start: new PIXI.Point(this._bottomLeftCircleElement.position.x, this._bottomLeftCircleElement.position.y - Design.scale.cornerRadius), end: new PIXI.Point(this._topLeftCircleElement.position.x, this._topLeftCircleElement.position.y + Design.scale.cornerRadius), }; this._leftLineElement.draw(); } this.pivot.set(this.rectangle.x + this.rectangle.width / 2, this.rectangle.y + this.rectangle.height / 2); }; ScaleElement.prototype.destroy = function (options) { if (this._rectangleElement) { this._rectangleElement.destroy(options); } if (this._topLeftCircleElement) { this._topLeftCircleElement.destroy(options); } if (this._topRightCircleElement) { this._topRightCircleElement.destroy(options); } if (this._bottomRightCircleElement) { this._bottomRightCircleElement.destroy(options); } if (this._bottomLeftCircleElement) { this._bottomLeftCircleElement.destroy(options); } if (this._topLineElement) { this._topLineElement.destroy(options); } if (this._rightLineElement) { this._rightLineElement.destroy(options); } if (this._bottomLineElement) { this._bottomLineElement.destroy(options); } if (this._leftLineElement) { this._leftLineElement.destroy(options); } _super.prototype.destroy.call(this, options); }; ScaleElement.prototype._setLockAspectRatio = function () { this._rectangleElement.options.lineColor = Design.scale.lineColorLockAspect; this._rectangleElement.options.fillColor = Design.scale.fillColorLockAspect; this._topLeftCircleElement.options.lineColor = Design.scale.lineColorLockAspect; this._topLeftCircleElement.options.fillColor = Design.scale.fillColorLockAspect; this._topRightCircleElement.options.lineColor = Design.scale.lineColorLockAspect; this._topRightCircleElement.options.fillColor = Design.scale.fillColorLockAspect; this._bottomRightCircleElement.options.lineColor = Design.scale.lineColorLockAspect; this._bottomRightCircleElement.options.fillColor = Design.scale.fillColorLockAspect; this._bottomLeftCircleElement.options.lineColor = Design.scale.lineColorLockAspect; this._bottomLeftCircleElement.options.fillColor = Design.scale.fillColorLockAspect; this._topLineElement.options.lineColor = Design.scale.lineColorLockAspect; this._topLineElement.options.fillColor = Design.scale.fillColorLockAspect; this._rightLineElement.options.lineColor = Design.scale.lineColorLockAspect; this._rightLineElement.options.fillColor = Design.scale.fillColorLockAspect; this._bottomLineElement.options.lineColor = Design.scale.lineColorLockAspect; this._bottomLineElement.options.fillColor = Design.scale.fillColorLockAspect; this._leftLineElement.options.lineColor = Design.scale.lineColorLockAspect; this._leftLineElement.options.fillColor = Design.scale.fillColorLockAspect; this.emit("debounceDraw"); }; ScaleElement.prototype._unsetLockAspectRatio = function () { this._rectangleElement.options.lineColor = Design.scale.lineColorNormal; this._rectangleElement.options.fillColor = Design.scale.fillColorNormal; this._topLeftCircleElement.options.lineColor = Design.scale.lineColorNormal; this._topLeftCircleElement.options.fillColor = Design.scale.fillColorNormal; this._topRightCircleElement.options.lineColor = Design.scale.lineColorNormal; this._topRightCircleElement.options.fillColor = Design.scale.fillColorNormal; this._bottomRightCircleElement.options.lineColor = Design.scale.lineColorNormal; this._bottomRightCircleElement.options.fillColor = Design.scale.fillColorNormal; this._bottomLeftCircleElement.options.lineColor = Design.scale.lineColorNormal; this._bottomLeftCircleElement.options.fillColor = Design.scale.fillColorNormal; this._topLineElement.options.lineColor = Design.scale.lineColorNormal; this._topLineElement.options.fillColor = Design.scale.fillColorNormal; this._rightLineElement.options.lineColor = Design.scale.lineColorNormal; this._rightLineElement.options.fillColor = Design.scale.fillColorNormal; this._bottomLineElement.options.lineColor = Design.scale.lineColorNormal; this._bottomLineElement.options.fillColor = Design.scale.fillColorNormal; this._leftLineElement.options.lineColor = Design.scale.lineColorNormal; this._leftLineElement.options.fillColor = Design.scale.fillColorNormal; this.emit("debounceDraw"); }; ScaleElement.prototype._setMoveRelative = function () { this._rectangleElement.options.lineColor = Design.scale.lineColorRelative; this._rectangleElement.options.fillColor = Design.scale.fillColorRelative; this._topLeftCircleElement.options.lineColor = Design.scale.lineColorRelative; this._topLeftCircleElement.options.fillColor = Design.scale.fillColorRelative; this._topRightCircleElement.options.lineColor = Design.scale.lineColorRelative; this._topRightCircleElement.options.fillColor = Design.scale.fillColorRelative; this._bottomRightCircleElement.options.lineColor = Design.scale.lineColorRelative; this._bottomRightCircleElement.options.fillColor = Design.scale.fillColorRelative; this._bottomLeftCircleElement.options.lineColor = Design.scale.lineColorRelative; this._bottomLeftCircleElement.options.fillColor = Design.scale.fillColorRelative; this._topLineElement.options.lineColor = Design.scale.lineColorRelative; this._topLineElement.options.fillColor = Design.scale.fillColorRelative; this._rightLineElement.options.lineColor = Design.scale.lineColorRelative; this._rightLineElement.options.fillColor = Design.scale.fillColorRelative; this._bottomLineElement.options.lineColor = Design.scale.lineColorRelative; this._bottomLineElement.options.fillColor = Design.scale.fillColorRelative; this._leftLineElement.options.lineColor = Design.scale.lineColorRelative; this._leftLineElement.options.fillColor = Design.scale.fillColorRelative; this.emit("debounceDraw"); }; ScaleElement.prototype._unsetMoveRelative = function () { this._rectangleElement.options.lineColor = Design.scale.lineColorNormal; this._rectangleElement.options.fillColor = Design.scale.fillColorNormal; this._topLeftCircleElement.options.lineColor = Design.scale.lineColorNormal; this._topLeftCircleElement.options.fillColor = Design.scale.fillColorNormal; this._topRightCircleElement.options.lineColor = Design.scale.lineColorNormal; this._topRightCircleElement.options.fillColor = Design.scale.fillColorNormal; this._bottomRightCircleElement.options.lineColor = Design.scale.lineColorNormal; this._bottomRightCircleElement.options.fillColor = Design.scale.fillColorNormal; this._bottomLeftCircleElement.options.lineColor = Design.scale.lineColorNormal; this._bottomLeftCircleElement.options.fillColor = Design.scale.fillColorNormal; this._topLineElement.options.lineColor = Design.scale.lineColorNormal; this._topLineElement.options.fillColor = Design.scale.fillColorNormal; this._rightLineElement.options.lineColor = Design.scale.lineColorNormal; this._rightLineElement.options.fillColor = Design.scale.fillColorNormal; this._bottomLineElement.options.lineColor = Design.scale.lineColorNormal; this._bottomLineElement.options.fillColor = Design.scale.fillColorNormal; this._leftLineElement.options.lineColor = Design.scale.lineColorNormal; this._leftLineElement.options.fillColor = Design.scale.fillColorNormal; this.emit("debounceDraw"); }; ScaleElement.prototype._typeChanged = function () { MedsurfDraw.Keyboard.events.remove("pressed_ShiftLeft", "onScaleElementLockAspectLeft"); MedsurfDraw.Keyboard.events.remove("released_ShiftLeft", "onScaleElementUnlockAspectLeft"); MedsurfDraw.Keyboard.events.remove("pressed_ControlLeft", "onScaleElementMoveRelativeLeft"); MedsurfDraw.Keyboard.events.remove("released_ControlLeft", "onScaleElementUnmoveRelativeLeft"); MedsurfDraw.Keyboard.events.remove("pressed_ShiftRight", "onScaleElementLockAspectRight"); MedsurfDraw.Keyboard.events.remove("released_ShiftRight", "onScaleElementUnlockAspectRight"); MedsurfDraw.Keyboard.events.remove("pressed_ControlLeft", "onScaleElementMoveRelativeRight"); MedsurfDraw.Keyboard.events.remove("released_ControlLeft", "onScaleElementUnmoveRelativeRight"); if (this.type === ScaleElementType.TEXT) { this._rectangleElement.showItem(); this._topLeftCircleElement.hideItem(); this._topRightCircleElement.hideItem(); this._bottomRightCircleElement.hideItem(); this._bottomLeftCircleElement.hideItem(); this._topLineElement.hideItem(); this._rightLineElement.hideItem(); this._bottomLineElement.hideItem(); this._leftLineElement.hideItem(); } else { if (this.type === ScaleElementType.SCALEABLE) { this._rectangleElement.hideItem(); this._topLeftCircleElement.showItem(); this._topRightCircleElement.showItem(); this._bottomRightCircleElement.showItem(); this._bottomLeftCircleElement.showItem(); this._topLineElement.showItem(); this._rightLineElement.showItem(); this._bottomLineElement.showItem(); this._leftLineElement.showItem(); } else { this._rectangleElement.showItem(); this._topLeftCircleElement.hideItem(); this._topRightCircleElement.hideItem(); this._bottomRightCircleElement.hideItem(); this._bottomLeftCircleElement.hideItem(); this._topLineElement.hideItem(); this._rightLineElement.hideItem(); this._bottomLineElement.hideItem(); this._leftLineElement.hideItem(); } MedsurfDraw.Keyboard.events.on("pressed_ShiftLeft", "onScaleElementLockAspectLeft", this._setLockAspectRatio.bind(this)); MedsurfDraw.Keyboard.events.on("released_ShiftLeft", "onScaleElementUnlockAspectLeft", this._unsetLockAspectRatio.bind(this)); MedsurfDraw.Keyboard.events.on("pressed_ControlLeft", "onScaleElementMoveRelativeLeft", this._setMoveRelative.bind(this)); MedsurfDraw.Keyboard.events.on("released_ControlLeft", "onScaleElementUnmoveRelativeLeft", this._unsetMoveRelative.bind(this)); MedsurfDraw.Keyboard.events.on("pressed_ShiftRight", "onScaleElementLockAspectRight", this._setLockAspectRatio.bind(this)); MedsurfDraw.Keyboard.events.on("released_ShiftRight", "onScaleElementUnlockAspectRight", this._unsetLockAspectRatio.bind(this)); MedsurfDraw.Keyboard.events.on("pressed_ControlRight", "onScaleElementMoveRelativeRight", this._setMoveRelative.bind(this)); MedsurfDraw.Keyboard.events.on("released_ControlRight", "onScaleElementUnmoveRelativeRight", this._unsetMoveRelative.bind(this)); } }; ScaleElement.prototype.onPointerOver = function (event) { event.stopPropagation(); }; ScaleElement.prototype.onPointerDownY = function (event) { this.emit("startScale", event, MedsurfDraw.ScaleSideTypes.Y); }; ScaleElement.prototype.onPointerDownWidth = function (event) { this.emit("startScale", event, MedsurfDraw.ScaleSideTypes.WIDTH); }; ScaleElement.prototype.onPointerDownHeight = function (event) { this.emit("startScale", event, MedsurfDraw.ScaleSideTypes.HEIGHT); }; ScaleElement.prototype.onPointerDownX = function (event) { this.emit("startScale", event, MedsurfDraw.ScaleSideTypes.X); }; ScaleElement.prototype.onPointerDownXY = function (event) { this.emit("startScale", event, MedsurfDraw.ScaleSideTypes.X_Y); }; ScaleElement.prototype.onPointerDownWidthY = function (event) { this.emit("startScale", event, MedsurfDraw.ScaleSideTypes.WIDTH_Y); }; ScaleElement.prototype.onPointerDownWidthHeight = function (event) { this.emit("startScale", event, MedsurfDraw.ScaleSideTypes.WIDTH_HEIGHT); }; ScaleElement.prototype.onPointerDownXHeight = function (event) { this.emit("startScale", event, MedsurfDraw.ScaleSideTypes.X_HEIGHT); }; ScaleElement.prototype.onScale = function (event) { this.emit("onScale", event); }; ScaleElement.prototype.endScale = function (event) { this.emit("endScale", event); }; Object.defineProperty(ScaleElement.prototype, "type", { get: function () { return this.data.type; }, set: function (value) { this.data.type = value; this.emit("debounceTypeChanged"); }, enumerable: false, configurable: true }); Object.defineProperty(ScaleElement.prototype, "rectangle", { get: function () { return this.data.rectangle; }, set: function (value) { this.data.rectangle = value; }, enumerable: false, configurable: true }); return ScaleElement; }(BaseContainer)); export { ScaleElement }; //# sourceMappingURL=ScaleElement.js.map