subPixelOptimize.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Sub-pixel optimize for canvas rendering, prevent from blur
  3. * when rendering a thin vertical/horizontal line.
  4. */
  5. var round = Math.round;
  6. /**
  7. * Sub pixel optimize line for canvas
  8. *
  9. * @param {Object} outputShape The modification will be performed on `outputShape`.
  10. * `outputShape` and `inputShape` can be the same object.
  11. * `outputShape` object can be used repeatly, because all of
  12. * the `x1`, `x2`, `y1`, `y2` will be assigned in this method.
  13. * @param {Object} [inputShape]
  14. * @param {number} [inputShape.x1]
  15. * @param {number} [inputShape.y1]
  16. * @param {number} [inputShape.x2]
  17. * @param {number} [inputShape.y2]
  18. * @param {Object} [style]
  19. * @param {number} [style.lineWidth]
  20. */
  21. function subPixelOptimizeLine(outputShape, inputShape, style) {
  22. var lineWidth = style && style.lineWidth;
  23. if (!inputShape || !lineWidth) {
  24. return;
  25. }
  26. var x1 = inputShape.x1;
  27. var x2 = inputShape.x2;
  28. var y1 = inputShape.y1;
  29. var y2 = inputShape.y2;
  30. if (round(x1 * 2) === round(x2 * 2)) {
  31. outputShape.x1 = outputShape.x2 = subPixelOptimize(x1, lineWidth, true);
  32. } else {
  33. outputShape.x1 = x1;
  34. outputShape.x2 = x2;
  35. }
  36. if (round(y1 * 2) === round(y2 * 2)) {
  37. outputShape.y1 = outputShape.y2 = subPixelOptimize(y1, lineWidth, true);
  38. } else {
  39. outputShape.y1 = y1;
  40. outputShape.y2 = y2;
  41. }
  42. }
  43. /**
  44. * Sub pixel optimize rect for canvas
  45. *
  46. * @param {Object} outputShape The modification will be performed on `outputShape`.
  47. * `outputShape` and `inputShape` can be the same object.
  48. * `outputShape` object can be used repeatly, because all of
  49. * the `x`, `y`, `width`, `height` will be assigned in this method.
  50. * @param {Object} [inputShape]
  51. * @param {number} [inputShape.x]
  52. * @param {number} [inputShape.y]
  53. * @param {number} [inputShape.width]
  54. * @param {number} [inputShape.height]
  55. * @param {Object} [style]
  56. * @param {number} [style.lineWidth]
  57. */
  58. function subPixelOptimizeRect(outputShape, inputShape, style) {
  59. var lineWidth = style && style.lineWidth;
  60. if (!inputShape || !lineWidth) {
  61. return;
  62. }
  63. var originX = inputShape.x;
  64. var originY = inputShape.y;
  65. var originWidth = inputShape.width;
  66. var originHeight = inputShape.height;
  67. outputShape.x = subPixelOptimize(originX, lineWidth, true);
  68. outputShape.y = subPixelOptimize(originY, lineWidth, true);
  69. outputShape.width = Math.max(subPixelOptimize(originX + originWidth, lineWidth, false) - outputShape.x, originWidth === 0 ? 0 : 1);
  70. outputShape.height = Math.max(subPixelOptimize(originY + originHeight, lineWidth, false) - outputShape.y, originHeight === 0 ? 0 : 1);
  71. }
  72. /**
  73. * Sub pixel optimize for canvas
  74. *
  75. * @param {number} position Coordinate, such as x, y
  76. * @param {number} lineWidth Should be nonnegative integer.
  77. * @param {boolean=} positiveOrNegative Default false (negative).
  78. * @return {number} Optimized position.
  79. */
  80. function subPixelOptimize(position, lineWidth, positiveOrNegative) {
  81. // Assure that (position + lineWidth / 2) is near integer edge,
  82. // otherwise line will be fuzzy in canvas.
  83. var doubledPosition = round(position * 2);
  84. return (doubledPosition + round(lineWidth)) % 2 === 0 ? doubledPosition / 2 : (doubledPosition + (positiveOrNegative ? 1 : -1)) / 2;
  85. }
  86. exports.subPixelOptimizeLine = subPixelOptimizeLine;
  87. exports.subPixelOptimizeRect = subPixelOptimizeRect;
  88. exports.subPixelOptimize = subPixelOptimize;