Unit.ts 18.1 KB
Newer Older
limingzhe's avatar
limingzhe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14

import TWEEN from '@tweenjs/tween.js';


class Sprite {
  x = 0;
  y = 0;
  color = '';
  radius = 0;
  alive = false;
  margin = 0;
  angle = 0;
  ctx;

15 16
  constructor(ctx) {
    this.ctx = ctx;
limingzhe's avatar
limingzhe committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  }
  update($event) {
    this.draw();
  }
  draw() {

  }

}





export class MySprite extends Sprite {

33 34
  width = 0;
  height = 0;
limingzhe's avatar
limingzhe committed
35 36 37 38 39 40
  _anchorX = 0;
  _anchorY = 0;
  _offX = 0;
  _offY = 0;
  scaleX = 1;
  scaleY = 1;
41
  alpha = 1;
limingzhe's avatar
limingzhe committed
42 43 44 45 46 47 48 49 50
  rotation = 0;
  visible = true;

  children = [this];

  img;
  _z = 0;


51
  init(imgObj = null, anchorX:number = 0.5, anchorY:number = 0.5) {
limingzhe's avatar
limingzhe committed
52 53 54 55 56 57 58 59 60 61
    if (imgObj) {
      this.img = imgObj;
      this.width = this.img.width;
      this.height = this.img.height;
    }
    this.anchorX = anchorX;
    this.anchorY = anchorY;
  }

  update($event = null) {
62 63
    if (this.visible) {
      this.draw();
limingzhe's avatar
limingzhe committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    }
  }
  draw() {
    this.ctx.save();
    this.drawInit();
    this.updateChildren();
    this.ctx.restore();
  }

  drawInit() {
    this.ctx.translate(this.x, this.y);
    this.ctx.rotate(this.rotation * Math.PI / 180);
    this.ctx.scale(this.scaleX, this.scaleY);
    this.ctx.globalAlpha = this.alpha;
  }

  drawSelf() {
    if (this.img) {
82
      this.ctx.drawImage(this.img, this._offX, this._offY);
limingzhe's avatar
limingzhe committed
83 84 85 86 87
    }
  }

  updateChildren() {
    if (this.children.length <= 0) { return; }
88 89 90
    for (let i = 0; i < this.children.length; i++) {
      if (this.children[i] === this) {
        this.drawSelf();
limingzhe's avatar
limingzhe committed
91
      } else {
92
        this.children[i].update();
limingzhe's avatar
limingzhe committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
      }
    }
  }

  load(url, anchorX = 0.5, anchorY = 0.5) {
    return new Promise((resolve, reject) => {
      const img = new Image();
      img.onload = () => resolve(img);
      img.onerror = reject;
      img.src = url;
    }).then(img => {
      this.init(img, anchorX, anchorY);
      return img;
    });
  }

  addChild(child, z = 1) {
    if (this.children.indexOf(child) === -1) {
      this.children.push(child);
      child._z = z;
      child.parent = this;
    }
    this.children.sort((a, b) => {
      return a._z - b._z;
    });
  }
119

limingzhe's avatar
limingzhe committed
120 121 122 123 124 125 126
  removeChild(child) {
    const index = this.children.indexOf(child);
    if (index !== -1) {
      this.children.splice(index, 1);
    }
  }

limingzhe's avatar
limingzhe committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  set anchorX(value) {
    this._anchorX = value;
    this.refreshAnchorOff();
  }
  get anchorX() {
    return this._anchorX;
  }
  set anchorY(value) {
    this._anchorY = value;
    this.refreshAnchorOff();
  }
  get anchorY() {
    return this._anchorY;
  }
  refreshAnchorOff() {
142 143
    this._offX = -this.width * this.anchorX;
    this._offY = -this.height * this.anchorY;
limingzhe's avatar
limingzhe committed
144 145 146 147 148 149 150
  }

  setScaleXY(value) {
    this.scaleX = this.scaleY = value;
  }

  getBoundingBox() {
151 152 153 154
    const x = this.x + this._offX * this.scaleX;
    const y = this.y + this._offY * this.scaleY;
    const width = this.width * this.scaleX;
    const height = this.height * this.scaleY;
limingzhe's avatar
limingzhe committed
155 156 157 158 159
    return {x, y, width, height};
  }
}


160 161 162 163 164 165 166 167 168 169 170 171 172 173
export class Item extends MySprite {
  baseX;
  move(targetY, callBack) {
    const self = this;
    const tween = new TWEEN.Tween(this)
      .to({ y: targetY }, 2500)
      .easing(TWEEN.Easing.Quintic.Out)
      .onComplete(function() {
        self.hide(callBack);
        // if (callBack) {
        //   callBack();
        // }
      })
      .start();
limingzhe's avatar
limingzhe committed
174
  }
175 176 177 178 179 180 181 182 183 184
  show(callBack = null) {
    const tween = new TWEEN.Tween(this)
      .to({ alpha: 1 }, 800)
      // .easing(TWEEN.Easing.Quadratic.Out) // Use an easing function to make the animation smooth.
      .onComplete(function() {
        if (callBack) {
          callBack();
        }
      })
      .start(); // Start the tween immediately.
limingzhe's avatar
limingzhe committed
185 186
  }

187 188 189 190 191 192 193 194 195 196
  hide(callBack = null) {
    const tween = new TWEEN.Tween(this)
      .to({ alpha: 0 }, 800)
      // .easing(TWEEN.Easing.Quadratic.Out) // Use an easing function to make the animation smooth.
      .onComplete(function() {
        if (callBack) {
          callBack();
        }
      })
      .start(); // Start the tween immediately.
limingzhe's avatar
limingzhe committed
197 198 199
  }


200 201 202
  shake(id) {
    if (!this.baseX) {
      this.baseX = this.x;
limingzhe's avatar
limingzhe committed
203 204
    }

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    const baseX = this.baseX;
    const baseTime = 50;
    const sequence = [
      {target: {x: baseX + 40 * id}, time: baseTime - 25},
      {target: {x: baseX - 20 * id}, time: baseTime},
      {target: {x: baseX + 10 * id}, time: baseTime},
      {target: {x: baseX - 5 * id}, time: baseTime},
      {target: {x: baseX + 2 * id}, time: baseTime},
      {target: {x: baseX - 1 * id}, time: baseTime},
      {target: {x: baseX}, time: baseTime},
    ];
    const self = this;
    function runSequence() {
      if (self['shakeTween']) {
        self['shakeTween'].stop();
      }
      const tween = new TWEEN.Tween(self);
      if (sequence.length > 0) {
        const action = sequence.shift();
        tween.to(action['target'], action['time']);
        tween.onComplete( () => {
          runSequence();
        });
        tween.start();
        self['shakeTween'] = tween;
limingzhe's avatar
limingzhe committed
230 231
      }
    }
232
    runSequence();
limingzhe's avatar
limingzhe committed
233 234
  }

235 236 237 238 239 240 241 242 243 244 245 246 247 248
  drop(targetY, callBack = null) {
    const self = this;
    const time = Math.abs(targetY - this.y) * 2.4;
    this.alpha = 1;
    const tween = new TWEEN.Tween(this)
      .to({ y: targetY }, time)
      .easing(TWEEN.Easing.Cubic.In)
      .onComplete(function() {
        // self.hideItem(callBack);
        if (callBack) {
          callBack();
        }
      })
      .start();
limingzhe's avatar
limingzhe committed
249 250 251 252
  }
}


253 254 255 256 257 258 259 260
export class EndSpr extends MySprite {
  show(s) {
    this.scaleX = this.scaleY = 0;
    this.alpha = 0;
    const tween = new TWEEN.Tween(this)
      .to({ alpha: 1, scaleX: s, scaleY: s }, 800)
      .easing(TWEEN.Easing.Elastic.Out) // Use an easing function to make the animation smooth.
      .onComplete(function() {
limingzhe's avatar
limingzhe committed
261

262 263
      })
      .start(); // Start the tween immediately.
limingzhe's avatar
limingzhe committed
264 265 266
  }
}

267 268 269 270 271
export class ShapeRect extends MySprite {
  fillColor = '#FF0000';
  setSize(w, h) {
    this.width = w;
    this.height = h;
limingzhe's avatar
limingzhe committed
272 273
  }

274 275 276
  drawShape() {
    this.ctx.fillStyle = this.fillColor;
    this.ctx.fillRect(this._offX, this._offY, this.width, this.height);
limingzhe's avatar
limingzhe committed
277 278
  }

279 280 281
  drawSelf() {
    super.drawSelf();
    this.drawShape();
limingzhe's avatar
limingzhe committed
282
  }
283
}
limingzhe's avatar
limingzhe committed
284

limingzhe's avatar
limingzhe committed
285 286 287 288 289 290

export class HotZoneItem extends MySprite {

  lineDashFlag = false;
  arrow: MySprite;
  label: Label;
291
  text;
limingzhe's avatar
limingzhe committed
292 293 294 295 296 297 298 299 300 301 302 303

  arrowTop;
  arrowRight;

  setSize(w, h) {
    this.width = w;
    this.height = h;

    const rect = new ShapeRect(this.ctx);
    rect.x = -w / 2;
    rect.y = -h / 2;
    rect.setSize(w, h);
304
    rect.fillColor = '#FFFFFF';
limingzhe's avatar
limingzhe committed
305 306 307 308 309 310 311
    rect.alpha = 0.2;
    this.addChild(rect);
  }

  showLabel(text = null) {
    if (!this.label) {
      this.label = new Label(this.ctx);
312 313
      // this.label.anchorY = 0;
      this.label.fontSize = '40px';
limingzhe's avatar
limingzhe committed
314
      this.label.textAlign = 'center';
315
      this.label.color = "#7a4250"
limingzhe's avatar
limingzhe committed
316 317 318 319 320
      this.addChild(this.label);
      this.refreshLabelScale();
    }
    if (text) {
      this.label.text = text;
321 322
    } else if (this.text) {
      this.label.text = this.text;
limingzhe's avatar
limingzhe committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    }
    this.label.visible = true;
  }

  hideLabel() {
    if (!this.label) { return; }
    this.label.visible = false;
  }

  refreshLabelScale() {
    if (this.scaleX == this.scaleY) {
      this.label.setScaleXY(1);
    }

    if (this.scaleX > this.scaleY) {
      this.label.scaleX = this.scaleY / this.scaleX;
    } else {
      this.label.scaleY = this.scaleX / this.scaleY;
    }
  }

  showLineDash() {
    this.lineDashFlag = true;

    if (this.arrow) {
      this.arrow.visible = true;
    } else {
      this.arrow = new MySprite(this.ctx);
      this.arrow.load('assets/common/arrow.png', 1, 0);
      this.arrow.setScaleXY(0.06);

      this.arrowTop = new MySprite(this.ctx);
      this.arrowTop.load('assets/common/arrow_top.png', 0.5, 0);
      this.arrowTop.setScaleXY(0.06);

      this.arrowRight = new MySprite(this.ctx);
      this.arrowRight.load('assets/common/arrow_right.png', 1, 0.5);
      this.arrowRight.setScaleXY(0.06);
    }
    this.showLabel();
  }

  hideLineDash() {
    this.lineDashFlag = false;
    if (this.arrow) {
      this.arrow.visible = false;
    }
    this.hideLabel();
  }



  drawArrow() {
    if (!this.arrow) { return; }
    const rect = this.getBoundingBox();
    this.arrow.x = rect.x + rect.width;
    this.arrow.y = rect.y;
    this.arrow.update();

    this.arrowTop.x = rect.x + rect.width / 2;
    this.arrowTop.y = rect.y;
    this.arrowTop.update();

    this.arrowRight.x = rect.x + rect.width;
    this.arrowRight.y = rect.y + rect.height / 2;
    this.arrowRight.update();
  }

  drawFrame() {
    this.ctx.save();
    const rect = this.getBoundingBox();
    const w = rect.width;
    const h = rect.height;
    const x = rect.x + w / 2;
    const y = rect.y + h / 2;
    this.ctx.setLineDash([5, 5]);
    this.ctx.lineWidth = 2;
    this.ctx.strokeStyle = '#1bfff7';
    this.ctx.beginPath();
    this.ctx.moveTo( x - w / 2, y - h / 2);
    this.ctx.lineTo(x + w / 2, y - h / 2);
    this.ctx.lineTo(x + w / 2, y + h / 2);
    this.ctx.lineTo(x - w / 2, y + h / 2);
    this.ctx.lineTo(x - w / 2, y - h / 2);
    this.ctx.stroke();
    this.ctx.restore();

  }

  draw() {
    super.draw();
    if (this.lineDashFlag) {
      this.drawFrame();
      this.drawArrow();
    }
  }
}

limingzhe's avatar
limingzhe committed
421 422


423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
export class HotZoneImageItem extends MySprite {
  index
  lineDashFlag = false;
  arrow: MySprite;
  label: Label;
  image: MySprite;
  image_url: String;
  labelBox: MySprite;
  audio_url: String;
  text = "";
  type = "text";
  card_audio_url: String
  scale
  arrowTop;
  arrowRight;
  
  init(image_url = null,  text?, type?, callback?, handleSave?){
李维's avatar
李维 committed
440 441 442 443
    // let change = true;
    // if(type == this.type){
    //   change = false
    // }
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

    if(type && type=="Text"){
      image_url = "assets/default/images/bg_50_50.png";
      this.type = "Text"
    }else if(!type){
      this.type = "Text"
    }

    if(text){
      this.text = text
    }else{
      this.text = ""
    }

    this.showImage(image_url, (img)=>{
      callback && callback(img.width, img.height)
      if(type == "Text"){
        this.scaleX = 104 / img.width
        this.scaleY = 78 / img.height
        this.setSize(img.width*this.scaleX, img.height*this.scaleY)
      }else{
        this.setSize(img.width*this.image.scaleX, img.height*this.image.scaleY)
      }
      handleSave && handleSave()
      this.lineDashFlag = true;
      if(type == "Text"){
        this.showLabel(text);
        // this.arrow.visible = false
      }else{
        if(this.label){
          this.removeChild(this.label)
        }
        this.drawArrow()
      }
李维's avatar
李维 committed
478
      // if(change){
479
        this.showLineDash()
李维's avatar
李维 committed
480
      // }
481 482
    })
  }
limingzhe's avatar
limingzhe committed
483 484


485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  initNew(callback?, handleSave?){
    if(this.type && this.type=="Text"){
      this.image_url = "assets/default/images/bg_50_50.png";
    }
    this.showImage(this.image_url, (img)=>{
      callback && callback(img.width, img.height)
      if(this.type == "Text"){
        this.scaleX = 104 / img.width
        this.scaleY = 78 / img.height      
        this.setSize(104, 78)
      }else{
        this.text = ""
        this.setSize(img.width*this.image.scaleX, img.height*this.image.scaleY)
      }
      handleSave && handleSave()
      this.lineDashFlag = true;
      if(this.type == "Text"){
        this.showLabel(this.text);
      }else{
        this.showLabel(this.index);
      }
      this.drawArrow()
      this.showLineDash()
    })
  }
  
  setSize(w, h) {
    this.width = w;
    this.height = h;
  }
limingzhe's avatar
limingzhe committed
515

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
  showLabel(text = null) {
    if (!this.label) {
      this.labelBox = new MySprite(this.ctx);
      this.labelBox.load('assets/default/images/bg_50_50.png').then(()=>{
        this.labelBox.x = this.width/2;
        this.labelBox.y = this.height/2;
      });
      this.label = new Label(this.ctx);
      this.label.fontSize = "45";
      this.label.textAlign = 'center';
      this.label.color = "#7A4250"
      if(this.type == "Text"){
        this.labelBox.visible = true
      }else{
        this.labelBox.visible = false
      }
      this.labelBox.addChild(this.label);
      this.addChild(this.labelBox);
      this.refreshLabelScale();
    }
    if (text) {
      this.label.text = text;
    } else if (this.text) {
      this.label.text = this.text;
    }
    this.label.visible = true;
  }
limingzhe's avatar
limingzhe committed
543

544 545 546 547
  hideLabel() {
    if (!this.label) { return; }
    this.label.visible = false;
  }
limingzhe's avatar
limingzhe committed
548

549 550 551 552 553 554 555 556 557 558 559 560 561 562
  showImage(image_url = null, callback) {
    this.loadImage(image_url).then((img)=>{
      if(this.image){
        this.removeChild(this.image)
      }
      this.image = new MySprite(this.ctx);
      this.image.init(img)
      this.image.x = this.image.width/2
      this.image.y = this.image.height/2
      this.image.alpha = 0.7;
      this.image_url = image_url;
      this.addChild(this.image,-1);
      callback && callback(this.image)
    })
limingzhe's avatar
limingzhe committed
563 564
  }

565 566 567 568 569 570 571 572 573 574 575 576 577
  loadImage(image_url = null){
    return new Promise((resolve, reject) => {
      const img = new Image();
      img.onload = () => resolve(img);
      img.onerror = reject;
      img.src = image_url;
    })
  }
  
  hideImage() {
    if (!this.image) { return; }
    this.image.visible = false;
  }
limingzhe's avatar
limingzhe committed
578

579 580
  refreshLabelScale() {
    this.labelBox.scaleX = 75 / (this.labelBox.width*this.image.scaleX)
limingzhe's avatar
limingzhe committed
581 582
  }

583 584 585 586 587 588 589
  showLineDash() {
    if (this.arrow) {
      this.arrow.visible = true;
    } else {
      this.arrow = new MySprite(this.ctx);
      this.arrow.load('assets/common/arrow.png', 1, 0);
      this.arrow.setScaleXY(0.06);
limingzhe's avatar
limingzhe committed
590

591 592 593
      this.arrowTop = new MySprite(this.ctx);
      this.arrowTop.load('assets/common/arrow_top.png', 0.5, 0);
      this.arrowTop.setScaleXY(0.06);
limingzhe's avatar
limingzhe committed
594

595 596 597 598 599 600 601 602 603 604 605 606 607
      this.arrowRight = new MySprite(this.ctx);
      this.arrowRight.load('assets/common/arrow_right.png', 1, 0.5);
      this.arrowRight.setScaleXY(0.06);
    }
  }

  hideLineDash() {
    this.lineDashFlag = false;
    if (this.arrow) {
      this.arrow.visible = false;
    }
    this.hideLabel();
  }
limingzhe's avatar
limingzhe committed
608

609 610
  drawArrow() {
    if (!this.arrow) { return; }
limingzhe's avatar
limingzhe committed
611
    const rect = this.getBoundingBox();
612 613 614
    this.arrow.x = rect.x + rect.width;
    this.arrow.y = rect.y;
    this.arrow.update();
limingzhe's avatar
limingzhe committed
615

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    this.arrowTop.x = rect.x + rect.width / 2;
    this.arrowTop.y = rect.y;
    this.arrowTop.update();

    this.arrowRight.x = rect.x + rect.width;
    this.arrowRight.y = rect.y + rect.height / 2;
    this.arrowRight.update();
  }

  drawFrame() {
    this.ctx.save();
    const rect = this.getBoundingBox();
    const w = rect.width;
    const h = rect.height;
    const x = rect.x + w / 2;
    const y = rect.y + h / 2;
limingzhe's avatar
limingzhe committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    this.ctx.setLineDash([5, 5]);
    this.ctx.lineWidth = 2;
    this.ctx.strokeStyle = '#1bfff7';
    this.ctx.beginPath();
    this.ctx.moveTo( x - w / 2, y - h / 2);
    this.ctx.lineTo(x + w / 2, y - h / 2);
    this.ctx.lineTo(x + w / 2, y + h / 2);
    this.ctx.lineTo(x - w / 2, y + h / 2);
    this.ctx.lineTo(x - w / 2, y - h / 2);
    this.ctx.stroke();
    this.ctx.restore();
  }

  draw() {
    super.draw();
647 648 649 650
    if (this.lineDashFlag) {
      this.drawFrame();
      this.drawArrow();
    }
limingzhe's avatar
limingzhe committed
651 652 653 654
  }
}


limingzhe's avatar
limingzhe committed
655 656 657 658
export class EditorItem extends MySprite {

  lineDashFlag = false;
  arrow: MySprite;
659
  label:Label;
limingzhe's avatar
limingzhe committed
660 661 662 663 664 665
  text;

  showLabel(text = null) {
    if (!this.label) {
      this.label = new Label(this.ctx);
      this.label.anchorY = 0;
666
      this.label.fontSize = '50px';
limingzhe's avatar
limingzhe committed
667 668 669 670
      this.label.textAlign = 'center';
      this.addChild(this.label);
      this.label.setScaleXY(1 / this.scaleX);
    }
671
    this.label.text = this.text;
limingzhe's avatar
limingzhe committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
    this.label.visible = true;
  }

  hideLabel() {
    if (!this.label) { return; }
    this.label.visible = false;
  }

  showLineDash() {
    this.lineDashFlag = true;
    if (this.arrow) {
      this.arrow.visible = true;
    } else {
      this.arrow = new MySprite(this.ctx);
      this.arrow.load('assets/common/arrow.png', 1, 0);
      this.arrow.setScaleXY(0.06);
    }
    this.showLabel();
  }

  hideLineDash() {

    this.lineDashFlag = false;

    if (this.arrow) {
      this.arrow.visible = false;
    }

    this.hideLabel();
  }



  drawArrow() {
    if (!this.arrow) { return; }

    const rect = this.getBoundingBox();
    this.arrow.x = rect.x + rect.width;
    this.arrow.y = rect.y;

    this.arrow.update();
  }

  drawFrame() {


    this.ctx.save();


    const rect = this.getBoundingBox();

    const w = rect.width;
    const h = rect.height;
    const x = rect.x + w / 2;
    const y = rect.y + h / 2;

    this.ctx.setLineDash([5, 5]);
    this.ctx.lineWidth = 2;
    this.ctx.strokeStyle = '#1bfff7';
    // this.ctx.fillStyle = '#ffffff';

    this.ctx.beginPath();

    this.ctx.moveTo( x - w / 2, y - h / 2);

    this.ctx.lineTo(x + w / 2, y - h / 2);

    this.ctx.lineTo(x + w / 2, y + h / 2);

    this.ctx.lineTo(x - w / 2, y + h / 2);

    this.ctx.lineTo(x - w / 2, y - h / 2);

    // this.ctx.fill();
    this.ctx.stroke();




    this.ctx.restore();

  }

  draw() {
    super.draw();

    if (this.lineDashFlag) {
      this.drawFrame();
      this.drawArrow();
    }
  }
}



767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
export class Label extends MySprite {

  text:String;
  fontSize:String = '40px';
  fontName:String = 'Verdana';
  textAlign:String = 'left';


  constructor(ctx) {
    super(ctx);
    this.init();
  }

  drawText() {

    if (!this.text) { return; }

    this.ctx.font = `${this.fontSize} ${this.fontName}`;
    this.ctx.textAlign = this.textAlign;
    this.ctx.textBaseline = 'middle';
    this.ctx.fontWeight = 900;

    // this.ctx.lineWidth = 5;
    // this.ctx.strokeStyle = '#ffffff';
    // this.ctx.strokeText(this.text, 0, 0);

    this.ctx.fillStyle = '#7a4250';
    this.ctx.fillText(this.text, 0, 0);

  }


  drawSelf() {
    super.drawSelf();
    this.drawText();
  }

}



export function getPosByAngle(angle, len) {

  const radian = angle * Math.PI / 180;
  const x = Math.sin(radian) * len;
  const y = Math.cos(radian) * len;

  return {x, y};

}

export function getAngleByPos(px, py, mx, my) {

  const x = Math.abs(px - mx);
  const y = Math.abs(py - my);
  const z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  const cos = y / z;
  const radina = Math.acos(cos); // 用反三角函数求弧度
  let angle = Math.floor(180 / (Math.PI / radina) * 100) / 100; // 将弧度转换成角度

  if(mx > px && my > py) {// 鼠标在第四象限
    angle = 180 - angle;
  }

  if(mx === px && my > py) {// 鼠标在y轴负方向上
    angle = 180;
  }

  if(mx > px && my === py) {// 鼠标在x轴正方向上
    angle = 90;
  }

  if(mx < px && my > py) {// 鼠标在第三象限
    angle = 180 + angle;
  }

  if(mx < px && my === py) {// 鼠标在x轴负方向
    angle = 270;
  }

  if(mx < px && my < py) {// 鼠标在第二象限
    angle = 360 - angle;
  }

  return angle;
}