body-model.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // pages/home/body-model/body-model.ts
  2. const Size = [542, 568];
  3. const Scale = 0.78
  4. interface Rect {
  5. width: number;
  6. height: number;
  7. }
  8. Component({
  9. lifetimes: {
  10. attached() { setTimeout(() => this._getContainerRef(), 300); }
  11. },
  12. properties: {
  13. dataset: { type: Object },
  14. },
  15. data: {
  16. container: { width: 0, height: 0 },
  17. model: { width: 0, height: 0 },
  18. },
  19. observers: {
  20. 'container.**'(container) { this._positioning(container); }
  21. },
  22. methods: {
  23. onBodyModel(event: WechatMiniprogram.TouchEvent) {
  24. const position = event.mark?.position
  25. if (position) this.triggerEvent('position', { position });
  26. },
  27. _getContainerRef() {
  28. this.createSelectorQuery().select('.body-model-wrapper').boundingClientRect().exec(res => {
  29. const { width: cw } = res[0];
  30. const mw = Math.round(cw * Scale);
  31. const ch = Math.round(mw * Size[1] / Size[0]);
  32. this.setData({
  33. 'container.width': cw,
  34. 'container.height': ch,
  35. 'model.width': mw,
  36. 'model.height': ch,
  37. })
  38. })
  39. },
  40. _positioning(container: Rect) {
  41. const halfW = container.width / 2;
  42. const halfH = container.height / 2;
  43. this.setData({
  44. LT: { left: `${halfW - 60 * Scale}px`, top: `${32 * Scale}px` },
  45. LB: { left: `${halfW - 90 * Scale}px`, top: `${halfH + 30 * Scale}px` },
  46. RT: { left: `${halfW + 40 * Scale}px`, top: `${30 * Scale}px` },
  47. RB: { left: `${halfW + 30 * Scale}px`, top: `${halfH + 5 * Scale}px` },
  48. })
  49. }
  50. }
  51. })