mixins.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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) {
  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 / 21, y: height / 21 }
  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 = 'black'
  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. }
  38. }