mixins.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // https://forum.vuejs.org/t/how-to-use-helper-functions-for-imported-modules-in-vuejs-vue-template/6266/5
  2. export default {
  3. methods: {
  4. deg2rad (deg) {
  5. return deg * (Math.PI / 180)
  6. },
  7. rad2deg (rad) {
  8. return rad * (180 / Math.PI)
  9. },
  10. createBuildingTitleCanvas (text, fontsize, sizefactore, txtcolor, bgcolor) {
  11. console.log('createLabelCanvas', text)
  12. const size = fontsize
  13. const borderSize = 5
  14. let lines = text.split('\n')
  15. const ctx = document.createElement('canvas').getContext('2d')
  16. const font = `${size}px bold noto_sans,sans-serif`
  17. ctx.font = font
  18. // measure how long the name will be
  19. const doubleBorderSize = borderSize * 2
  20. let width = 0
  21. for (var i = 0; i < lines.length; i++) {
  22. width = Math.max(width, ctx.measureText(lines[i]).width + doubleBorderSize)
  23. }
  24. const height = size * lines.length + doubleBorderSize
  25. this.label_size = { x: width / sizefactore, y: height / sizefactore }
  26. ctx.canvas.width = width
  27. ctx.canvas.height = height
  28. // need to set font again after resizing canvas
  29. ctx.font = font
  30. ctx.textBaseline = 'top'
  31. if (bgcolor) {
  32. ctx.fillStyle = bgcolor
  33. ctx.fillRect(0, 0, width, height)
  34. }
  35. ctx.fillStyle = txtcolor
  36. for (var j = 0; j < lines.length; j++) {
  37. ctx.fillText(lines[j], borderSize, borderSize + j * size)
  38. }
  39. // console.log('createLabelCanvas', ctx)
  40. return ctx.canvas
  41. },
  42. createLabelCanvas (text, fontsize, sizefactore, txtcolor, bgcolor) {
  43. console.log('createLabelCanvas', text)
  44. const size = fontsize
  45. const borderSize = 5
  46. let lines = text.split(':')
  47. const ctx = document.createElement('canvas').getContext('2d')
  48. const font = `${size}px bold noto_sans,sans-serif`
  49. ctx.font = font
  50. // measure how long the name will be
  51. const doubleBorderSize = borderSize * 2
  52. let width = 0
  53. for (var i = 0; i < lines.length; i++) {
  54. width = Math.max(width, ctx.measureText(lines[i]).width + doubleBorderSize)
  55. }
  56. const height = size * lines.length + doubleBorderSize
  57. this.label_size = { x: width / sizefactore, y: height / sizefactore }
  58. ctx.canvas.width = width
  59. ctx.canvas.height = height
  60. // need to set font again after resizing canvas
  61. ctx.font = font
  62. ctx.textBaseline = 'top'
  63. if (bgcolor) {
  64. ctx.fillStyle = bgcolor
  65. ctx.fillRect(0, 0, width, height)
  66. }
  67. ctx.fillStyle = txtcolor
  68. for (var j = 0; j < lines.length; j++) {
  69. ctx.fillText(lines[j], borderSize, borderSize + j * size)
  70. }
  71. // console.log('createLabelCanvas', ctx)
  72. return ctx.canvas
  73. },
  74. createImgCanvas (src) {
  75. // let vnode = this
  76. return new Promise((resolve, reject) => {
  77. var img = new Image()
  78. // XSS resolution
  79. // https://www.html5gamedevs.com/topic/31184-solved-cors-problem-with-dynamictexture-on-some-servers-tainted-canvas/
  80. img.crossOrigin = 'Anonymous'
  81. img.src = src
  82. img.onload = function () {
  83. // console.log('img loaded', this)
  84. const ctx = document.createElement('canvas').getContext('2d')
  85. ctx.canvas.width = this.width
  86. ctx.canvas.height = this.height
  87. ctx.fillStyle = 'red'
  88. // ctx.fillRect(0, 0, this.width, this.height)
  89. ctx.drawImage(this, 0, 0, this.width, this.height)
  90. resolve({ img: this, canvas: ctx.canvas })
  91. }
  92. img.onerror = function () {
  93. reject(src)
  94. }
  95. })
  96. }
  97. // createGradientCanvas (c1, c2) {
  98. // var ctx = document.createElement('canvas').getContext('2d')
  99. // ctx.canvas.width = 1024
  100. // ctx.canvas.height = 1024
  101. // var lingrad = ctx.createLinearGradient(0, 0, 0, 1024)
  102. // lingrad.addColorStop(0, c1)
  103. // lingrad.addColorStop(1, c2)
  104. // ctx.fillStyle = lingrad
  105. // ctx.fillRect(0, 0, 1024, 1024)
  106. // return ctx.canvas
  107. // }
  108. // ,
  109. // /**
  110. // * Converts an HSL color value to RGB. Conversion formula
  111. // * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
  112. // * Assumes h, s, and l are contained in the set [0, 1] and
  113. // * returns r, g, and b in the set [0, 255].
  114. // *
  115. // * @param {number} h The hue
  116. // * @param {number} s The saturation
  117. // * @param {number} l The lightness
  118. // * @return {Array} The RGB representation
  119. // */
  120. // hslToRgb (h, s, l) {
  121. // var r, g, b
  122. //
  123. // if (s === 0) {
  124. // r = g = b = l // achromatic
  125. // } else {
  126. // var hue2rgb = function hue2rgb (p, q, t) {
  127. // if (t < 0) t += 1
  128. // if (t > 1) t -= 1
  129. // if (t < 1 / 6) return p + (q - p) * 6 * t
  130. // if (t < 1 / 2) return q
  131. // if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
  132. // return p
  133. // }
  134. //
  135. // var q = l < 0.5 ? l * (1 + s) : l + s - l * s
  136. // var p = 2 * l - q
  137. // r = hue2rgb(p, q, h + 1 / 3)
  138. // g = hue2rgb(p, q, h)
  139. // b = hue2rgb(p, q, h - 1 / 3)
  140. // }
  141. //
  142. // return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]
  143. // }
  144. }
  145. }