sigma.canvas.labels.def.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. ;(function(undefined) {
  2. 'use strict';
  3. if (typeof sigma === 'undefined')
  4. throw 'sigma is not declared';
  5. // Initialize packages:
  6. sigma.utils.pkg('sigma.canvas.labels');
  7. /**
  8. * This label renderer will just display the label on the right of the node.
  9. *
  10. * @param {object} node The node object.
  11. * @param {CanvasRenderingContext2D} context The canvas context.
  12. * @param {configurable} settings The settings function.
  13. */
  14. sigma.canvas.labels.def = function(node, context, settings) {
  15. var fontSize,
  16. prefix = settings('prefix') || '',
  17. size = node[prefix + 'size'];
  18. if (size < settings('labelThreshold'))
  19. return;
  20. if (!node.label || typeof node.label !== 'string')
  21. return;
  22. fontSize = (settings('labelSize') === 'fixed') ?
  23. settings('defaultLabelSize') :
  24. settings('labelSizeRatio') * size;
  25. context.font = (settings('fontStyle') ? settings('fontStyle') + ' ' : '') +
  26. fontSize + 'px ' + settings('font');
  27. context.fillStyle = (settings('labelColor') === 'node') ?
  28. (node.color || settings('defaultNodeColor')) :
  29. settings('defaultLabelColor');
  30. context.fillText(
  31. node.label,
  32. Math.round(node[prefix + 'x'] + size + 3),
  33. Math.round(node[prefix + 'y'] + fontSize / 3)
  34. );
  35. };
  36. }).call(this);