mixins.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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) {
  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. // ctx.fillStyle = 'red'
  29. // ctx.fillRect(0, 0, width, height)
  30. ctx.fillStyle = txtcolor
  31. for (var j = 0; j < lines.length; j++) {
  32. ctx.fillText(lines[j], borderSize, borderSize + j * size)
  33. }
  34. // console.log('createLabelCanvas', ctx)
  35. return ctx.canvas
  36. },
  37. createImgCanvas (src) {
  38. // let vnode = this
  39. return new Promise((resolve, reject) => {
  40. var img = new Image()
  41. // XSS resolution
  42. // https://www.html5gamedevs.com/topic/31184-solved-cors-problem-with-dynamictexture-on-some-servers-tainted-canvas/
  43. img.crossOrigin = 'Anonymous'
  44. img.src = src
  45. img.onload = function () {
  46. // console.log('img loaded', this)
  47. const ctx = document.createElement('canvas').getContext('2d')
  48. ctx.canvas.width = this.width
  49. ctx.canvas.height = this.height
  50. ctx.fillStyle = 'red'
  51. // ctx.fillRect(0, 0, this.width, this.height)
  52. ctx.drawImage(this, 0, 0, this.width, this.height)
  53. resolve({ img: this, canvas: ctx.canvas })
  54. }
  55. img.onerror = function () {
  56. reject(src)
  57. }
  58. })
  59. }
  60. }
  61. }