container.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. Component({
  2. data: {},
  3. properties: {
  4. style: {
  5. type: String,
  6. value: '',
  7. },
  8. width: {
  9. type: Number,
  10. value: 100,
  11. },
  12. height: {
  13. type: Number,
  14. value: 100,
  15. },
  16. onInit: {
  17. type: Function,
  18. value: () => {},
  19. },
  20. onTouchEvent: {
  21. type: Function,
  22. value: () => {},
  23. },
  24. onError: {
  25. type: Function,
  26. value: () => {},
  27. },
  28. pixelRatio: {
  29. type: Number,
  30. value: 1,
  31. },
  32. },
  33. observers: {
  34. pixelRatio: function (pixelRatio) {
  35. this.setData({
  36. finalPixelRatio: pixelRatio >= 1 ? Math.ceil(pixelRatio) : 1,
  37. });
  38. },
  39. },
  40. ready() {
  41. const query = wx.createSelectorQuery().in(this);
  42. query
  43. .select('#g6-canvas')
  44. .fields({
  45. node: true,
  46. size: true,
  47. })
  48. .exec((ret) => {
  49. console.log('ret', ret);
  50. const { node: canvas } = ret[0];
  51. const finalPixelRatio = this.data.finalPixelRatio;
  52. canvas.width = this.data.width * finalPixelRatio;
  53. canvas.height = this.data.height * finalPixelRatio;
  54. this.rect = {
  55. width: this.data.width * finalPixelRatio,
  56. height: this.data.height * finalPixelRatio,
  57. left: canvas._left,
  58. top: canvas._top,
  59. };
  60. console.log('rect', this.rect);
  61. this.ctx = canvas.getContext('2d');
  62. this.data.onInit(this.ctx, this.rect, canvas, 'mini-native');
  63. });
  64. },
  65. methods: {
  66. error(e) {
  67. this.data.onError(e);
  68. },
  69. ontouch(e) {
  70. let i = 0;
  71. for (i = 0; i < e.touches.length; i++) {
  72. modifyEvent(e.touches[i]);
  73. }
  74. for (i = 0; i < e.changedTouches.length; i++) {
  75. modifyEvent(e.changedTouches[i]);
  76. }
  77. this.data.onTouchEvent(e);
  78. },
  79. },
  80. });
  81. function modifyEvent(touchEvent) {
  82. touchEvent.clientX = touchEvent.x;
  83. touchEvent.clientY = touchEvent.y;
  84. touchEvent.pageX = touchEvent.x;
  85. touchEvent.pageY = touchEvent.y;
  86. }