UNPKG

sigma

Version:

A JavaScript library aimed at visualizing graphs of thousands of nodes and edges.

49 lines (48 loc) 2.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var label_1 = __importDefault(require("./label")); /** * Draw an hovered node. * - if there is no label => display a shadow on the node * - if the label box is bigger than node size => display a label box that contains the node with a shadow * - else node with shadow and the label box */ function drawHover(context, data, settings) { var size = settings.labelSize, font = settings.labelFont, weight = settings.labelWeight; context.font = "".concat(weight, " ").concat(size, "px ").concat(font); // Then we draw the label background context.fillStyle = "#FFF"; context.shadowOffsetX = 0; context.shadowOffsetY = 0; context.shadowBlur = 8; context.shadowColor = "#000"; var PADDING = 2; if (typeof data.label === "string") { var textWidth = context.measureText(data.label).width, boxWidth = Math.round(textWidth + 5), boxHeight = Math.round(size + 2 * PADDING), radius = Math.max(data.size, size / 2) + PADDING; var angleRadian = Math.asin(boxHeight / 2 / radius); var xDeltaCoord = Math.sqrt(Math.abs(Math.pow(radius, 2) - Math.pow(boxHeight / 2, 2))); context.beginPath(); context.moveTo(data.x + xDeltaCoord, data.y + boxHeight / 2); context.lineTo(data.x + radius + boxWidth, data.y + boxHeight / 2); context.lineTo(data.x + radius + boxWidth, data.y - boxHeight / 2); context.lineTo(data.x + xDeltaCoord, data.y - boxHeight / 2); context.arc(data.x, data.y, radius, angleRadian, -angleRadian); context.closePath(); context.fill(); } else { context.beginPath(); context.arc(data.x, data.y, data.size + PADDING, 0, Math.PI * 2); context.closePath(); context.fill(); } context.shadowOffsetX = 0; context.shadowOffsetY = 0; context.shadowBlur = 0; // And finally we draw the label (0, label_1.default)(context, data, settings); } exports.default = drawHover;