mixins.js 3.9 KB

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