visjs-network
Version:
A dynamic, browser-based network visualization library.
106 lines (93 loc) • 2.51 kB
JavaScript
'use strict'
import NodeBase from '../util/NodeBase'
/**
* A Box Node/Cluster shape.
*
* @extends NodeBase
*/
class Box extends NodeBase {
/**
* @param {Object} options
* @param {Object} body
* @param {Label} labelModule
*/
constructor(options, body, labelModule) {
super(options, body, labelModule)
this._setMargins(labelModule)
}
/**
*
* @param {CanvasRenderingContext2D} ctx
* @param {boolean} [selected]
* @param {boolean} [hover]
*/
resize(ctx, selected = this.selected, hover = this.hover) {
if (this.needsRefresh(selected, hover)) {
var dimensions = this.getDimensionsFromLabel(ctx, selected, hover)
this.width = dimensions.width + this.margin.right + this.margin.left
this.height = dimensions.height + this.margin.top + this.margin.bottom
this.radius = this.width / 2
}
}
/**
*
* @param {CanvasRenderingContext2D} ctx
* @param {number} x width
* @param {number} y height
* @param {boolean} selected
* @param {boolean} hover
* @param {ArrowOptions} values
*/
draw(ctx, x, y, selected, hover, values) {
this.resize(ctx, selected, hover)
this.left = x - this.width / 2
this.top = y - this.height / 2
this.initContextForDraw(ctx, values)
ctx.roundRect(
this.left,
this.top,
this.width,
this.height,
values.borderRadius
)
this.performFill(ctx, values)
this.updateBoundingBox(x, y, ctx, selected, hover)
this.labelModule.draw(
ctx,
this.left + this.textSize.width / 2 + this.margin.left,
this.top + this.textSize.height / 2 + this.margin.top,
selected,
hover
)
}
/**
*
* @param {number} x width
* @param {number} y height
* @param {CanvasRenderingContext2D} ctx
* @param {boolean} selected
* @param {boolean} hover
*/
updateBoundingBox(x, y, ctx, selected, hover) {
this._updateBoundingBox(x, y, ctx, selected, hover)
let borderRadius = this.options.shapeProperties.borderRadius // only effective for box
this._addBoundingBoxMargin(borderRadius)
}
/**
*
* @param {CanvasRenderingContext2D} ctx
* @param {number} angle
* @returns {number}
*/
distanceToBorder(ctx, angle) {
this.resize(ctx)
let borderWidth = this.options.borderWidth
return (
Math.min(
Math.abs(this.width / 2 / Math.cos(angle)),
Math.abs(this.height / 2 / Math.sin(angle))
) + borderWidth
)
}
}
export default Box