Guest User

Untitled

a guest
Nov 14th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. /**
  2. * Creates a renderer for a CanvasRenderingContext2D (2d canvas context).
  3. *
  4. * @param {HTMLCanvasElement} canvas The canvas to render on.
  5. * @author Martin Hövre <martin.hovre@opencubes.io>
  6. * @license MIT
  7. */
  8. function renderer2d(canvas) {
  9. // The context for the renderer
  10. const ctx = canvas.getContext('2d')
  11. return {
  12. ctx,
  13. /**
  14. * Clears the canvas.
  15. *
  16. * @author Martin Hövre <martin.hovre@opencubes.io>
  17. * @license MIT
  18. */
  19. clear() {
  20. ctx.clearRect(0, 0, canvas.width, canvas.height)
  21. },
  22. /**
  23. * Renders a string on the canvas.
  24. *
  25. * @param {string} text The text to be rendered.
  26. * @param {number} x The x position to render the start of the text.
  27. * @param {number} y The y position to render the start of the text.
  28. * @author Martin Hövre <martin.hovre@opencubes.io>
  29. * @license MIT
  30. */
  31. text(text, x, y) {
  32. const fontSize = parseInt((/(\d+)px/.exec(ctx.font) || [, 10])[1]) || 10
  33. ctx.fillText(text, x, y + fontSize)
  34. },
  35. /**
  36. * Render a circle on the canvas.
  37. *
  38. * @param {object} options The options for the circle.
  39. * @param {number} options.radius The circle radius.
  40. * @param {number} options.x The circles x position.
  41. * @param {number} options.y The circles y position.
  42. * @param {string} [options.fill] The fill color of the circle.
  43. * @param {number} [options.borderWidth] The circle border width.
  44. * @param {string} [options.borderColor] The circle border color.
  45. * @author Martin Hövre <martin.hovre@opencubes.io>
  46. * @license MIT
  47. */
  48. circle(options) {
  49. ctx.beginPath()
  50. ctx.arc(options.x || 0, options.y || 0, options.radius || 0, 0, 2 * Math.PI)
  51. if (options.fill) {
  52. const fill = ctx.fillStyle
  53. ctx.fillStyle = options.fill
  54. ctx.fill()
  55. ctx.fillStyle = fill
  56. }
  57. if (options.borderWidth)
  58. ctx.lineWidth = options.borderWidth
  59. if (options.borderColor)
  60. ctx.strokeStyle = options.borderColor
  61. if (options.borderWidth || options.borderColor)
  62. ctx.stroke()
  63. },
  64. /**
  65. * Create a line from one position to another or a sequence of points.
  66. *
  67. * @example
  68. * line(0, 0, 10, 10, 1) // Create a line from position 0,0 to 10,10 with line width of 1
  69. *
  70. * // Create a line from position 0,0 to 10,10 with line width of 1 and a color of red
  71. * line(0, 0, 10, 10, 1, 'red') // or
  72. * line(0, 0, 10, 10, 1, '#ff0000')
  73. *
  74. * @example
  75. * line(1, [ // Create a line from 0,0 -> 10,10 -> 10,20 with line width 1
  76. * {x: 0, y: 0},
  77. * {x: 10, y: 10},
  78. * {x: 10, y: 20},
  79. * ])
  80. *
  81. * // Create a line from 0,0 -> 10,10 -> 10,20 with line width 1 and color of red
  82. * line(1, 'red', [
  83. * {x: 0, y: 0},
  84. * {x: 10, y: 10},
  85. * {x: 10, y: 20},
  86. * ]) // or
  87. * line(1, '#ff0000', [
  88. * {x: 0, y: 0},
  89. * {x: 10, y: 10},
  90. * {x: 10, y: 20},
  91. * ])
  92. *
  93. * @param {number} x1 The first x position or line width.
  94. * @param {number|string|Array<{x: number, y: number}>} y1 The first y position or line color or a array of points.
  95. * @param {number|Array<{x: number, y: number}>} x2 The second x position or a array of points.
  96. * @param {number} y2 The second y position.
  97. * @param {number} width The width of the line.
  98. * @param {number} [color='#000000'] Color of the line.
  99. * @author Martin Hövre <martin.hovre@opencubes.io>
  100. * @license MIT
  101. */
  102. line(x1, y1, x2, y2, width, color='#000000') {
  103. if (typeof x1 === 'number' && typeof y1 === 'number' && typeof x2 === 'number' && typeof y2 === 'number') {
  104. ctx.lineWidth = typeof width === 'number' ? width : 1
  105. ctx.strokeStyle = color
  106. ctx.beginPath()
  107. ctx.moveTo(x1, y1)
  108. ctx.lineTo(x2, y2)
  109. ctx.stroke()
  110. } else {
  111. const width = x1
  112. let color = y1
  113. let points = x2
  114. if (Array.isArray(color)) {
  115. points = color
  116. color = '#000000'
  117. }
  118. ctx.lineWidth = typeof width === 'number' ? width : 1
  119. ctx.strokeStyle = color
  120. ctx.beginPath()
  121. const point = points.shift()
  122. ctx.moveTo(point.x, point.y)
  123. for (const point of points)
  124. ctx.lineTo(point.x, point.y)
  125. ctx.stroke()
  126. }
  127. },
  128. /**
  129. * Create a continuous renderer i.e. a function that is called on every browser frame.
  130. *
  131. * @param {Function} fn The function to be run at every frame.
  132. * @author Martin Hövre <martin.hovre@opencubes.io>
  133. * @license MIT
  134. */
  135. render(fn) {
  136. const renderer = () => {
  137. requestAnimationFrame(renderer)
  138. fn()
  139. }
  140. renderer()
  141. }
  142. }
  143. }
Add Comment
Please, Sign In to add comment