createBillboardPointCallback.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Creates a {@link createBillboardPointCallback~CanvasFunction} that will create a canvas with a point.
  3. *
  4. * @param {Number} centerAlpha The alpha of the center of the point. The value must be in the range [0.0, 1.0].
  5. * @param {String} cssColor The CSS color string.
  6. * @param {String} cssOutlineColor The CSS color of the point outline.
  7. * @param {Number} cssOutlineWidth The width of the outline in pixels.
  8. * @param {Number} pixelSize The size of the point in pixels.
  9. * @return {createBillboardPointCallback~CanvasFunction} The function that will return a canvas with the point drawn on it.
  10. *
  11. * @private
  12. */
  13. function createBillboardPointCallback(centerAlpha, cssColor, cssOutlineColor, cssOutlineWidth, pixelSize) {
  14. return function() {
  15. var canvas = document.createElement('canvas');
  16. var length = pixelSize + (2 * cssOutlineWidth);
  17. canvas.height = canvas.width = length;
  18. var context2D = canvas.getContext('2d');
  19. context2D.clearRect(0, 0, length, length);
  20. if (cssOutlineWidth !== 0) {
  21. context2D.beginPath();
  22. context2D.arc(length / 2, length / 2, length / 2, 0, 2 * Math.PI, true);
  23. context2D.closePath();
  24. context2D.fillStyle = cssOutlineColor;
  25. context2D.fill();
  26. // Punch a hole in the center if needed.
  27. if (centerAlpha < 1.0) {
  28. context2D.save();
  29. context2D.globalCompositeOperation = 'destination-out';
  30. context2D.beginPath();
  31. context2D.arc(length / 2, length / 2, pixelSize / 2, 0, 2 * Math.PI, true);
  32. context2D.closePath();
  33. context2D.fillStyle = 'black';
  34. context2D.fill();
  35. context2D.restore();
  36. }
  37. }
  38. context2D.beginPath();
  39. context2D.arc(length / 2, length / 2, pixelSize / 2, 0, 2 * Math.PI, true);
  40. context2D.closePath();
  41. context2D.fillStyle = cssColor;
  42. context2D.fill();
  43. return canvas;
  44. };
  45. }
  46. /**
  47. * A function that returns a canvas containing an image of a point.
  48. * @callback createBillboardPointCallback~CanvasFunction
  49. * @returns {HTMLCanvasElement} The result of the calculation.
  50. */
  51. export default createBillboardPointCallback;