mixins.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. createLabelCanvas (text, fontsize, sizefactore, txtcolor, bgcolor) {
  11. // console.log('createLabelCanvas', this.data.Titre)
  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. createImgCanvas (src) {
  43. // let vnode = this
  44. return new Promise((resolve, reject) => {
  45. var img = new Image()
  46. // XSS resolution
  47. // https://www.html5gamedevs.com/topic/31184-solved-cors-problem-with-dynamictexture-on-some-servers-tainted-canvas/
  48. img.crossOrigin = 'Anonymous'
  49. img.src = src
  50. img.onload = function () {
  51. // console.log('img loaded', this)
  52. const ctx = document.createElement('canvas').getContext('2d')
  53. ctx.canvas.width = this.width
  54. ctx.canvas.height = this.height
  55. ctx.fillStyle = 'red'
  56. // ctx.fillRect(0, 0, this.width, this.height)
  57. ctx.drawImage(this, 0, 0, this.width, this.height)
  58. resolve({ img: this, canvas: ctx.canvas })
  59. }
  60. img.onerror = function () {
  61. reject(src)
  62. }
  63. })
  64. }
  65. // createGradientCanvas (c1, c2) {
  66. // var ctx = document.createElement('canvas').getContext('2d')
  67. // ctx.canvas.width = 1024
  68. // ctx.canvas.height = 1024
  69. // var lingrad = ctx.createLinearGradient(0, 0, 0, 1024)
  70. // lingrad.addColorStop(0, c1)
  71. // lingrad.addColorStop(1, c2)
  72. // ctx.fillStyle = lingrad
  73. // ctx.fillRect(0, 0, 1024, 1024)
  74. // return ctx.canvas
  75. // }
  76. // ,
  77. // /**
  78. // * Converts an HSL color value to RGB. Conversion formula
  79. // * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
  80. // * Assumes h, s, and l are contained in the set [0, 1] and
  81. // * returns r, g, and b in the set [0, 255].
  82. // *
  83. // * @param {number} h The hue
  84. // * @param {number} s The saturation
  85. // * @param {number} l The lightness
  86. // * @return {Array} The RGB representation
  87. // */
  88. // hslToRgb (h, s, l) {
  89. // var r, g, b
  90. //
  91. // if (s === 0) {
  92. // r = g = b = l // achromatic
  93. // } else {
  94. // var hue2rgb = function hue2rgb (p, q, t) {
  95. // if (t < 0) t += 1
  96. // if (t > 1) t -= 1
  97. // if (t < 1 / 6) return p + (q - p) * 6 * t
  98. // if (t < 1 / 2) return q
  99. // if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
  100. // return p
  101. // }
  102. //
  103. // var q = l < 0.5 ? l * (1 + s) : l + s - l * s
  104. // var p = 2 * l - q
  105. // r = hue2rgb(p, q, h + 1 / 3)
  106. // g = hue2rgb(p, q, h)
  107. // b = hue2rgb(p, q, h - 1 / 3)
  108. // }
  109. //
  110. // return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]
  111. // }
  112. }
  113. }