mixins.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }
  63. }