custom-hot-zone.component.ts 20.4 KB
Newer Older
limingzhe's avatar
limingzhe committed
1 2 3 4 5 6 7 8 9 10 11 12
import {
  Component,
  ElementRef,
  EventEmitter,
  HostListener,
  Input,
  OnChanges,
  OnDestroy,
  OnInit,
  Output,
  ViewChild
} from '@angular/core';
limingzhe's avatar
limingzhe committed
13 14
import {Subject} from 'rxjs';
import {debounceTime} from 'rxjs/operators';
limingzhe's avatar
limingzhe committed
15
import {EditorItem, HotZoneImg, HotZoneItem, HotZoneLabel, Label, MySprite, removeItemFromArr} from './Unit';
limingzhe's avatar
limingzhe committed
16
import TWEEN from '@tweenjs/tween.js';
limingzhe's avatar
limingzhe committed
17 18
import {getMinScale} from "../../play/Unit";
import {tar} from "compressing";
limingzhe's avatar
limingzhe committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36


@Component({
  selector: 'app-custom-hot-zone',
  templateUrl: './custom-hot-zone.component.html',
  styleUrls: ['./custom-hot-zone.component.scss']
})
export class CustomHotZoneComponent implements OnInit, OnDestroy, OnChanges {


  @Input()
  imgItemArr = null;
  @Input()
  hotZoneItemArr = null;
  @Input()
  hotZoneArr = null;
  @Output()
  save = new EventEmitter();
limingzhe's avatar
limingzhe committed
37 38
  @ViewChild('canvas', {static: true}) canvas: ElementRef;
  @ViewChild('wrap', {static: true}) wrap: ElementRef;
limingzhe's avatar
limingzhe committed
39

limingzhe's avatar
limingzhe committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  @Input()
  isHasRect = true;
  @Input()
  isHasPic = true;
  @Input()
  isHasText = true;
  @Input()
  hotZoneFontObj = {
    size: 50,
    name: 'BRLNSR_1',
    color: '#8f3758'
  }
  @Input()
  defaultItemType = 'text';

  @Input()
  hotZoneImgSize = 190;

  saveDisabled = true;
limingzhe's avatar
limingzhe committed
59 60 61 62

  canvasWidth = 1280;
  canvasHeight = 720;
  canvasBaseW = 1280;
limingzhe's avatar
limingzhe committed
63
  // @HostListener('window:resize', ['$event'])
limingzhe's avatar
limingzhe committed
64 65 66 67 68 69 70 71 72 73
  canvasBaseH = 720;
  mapScale = 1;
  ctx;
  mx;
  my; // 点击坐标
  // 声音
  bgAudio = new Audio();
  images = new Map();
  animationId: any;

limingzhe's avatar
limingzhe committed
74 75 76 77

  // 资源
  // rawImages = new Map(res);
  winResizeEventStream = new Subject();
limingzhe's avatar
limingzhe committed
78 79 80 81 82
  canvasLeft;
  canvasTop;
  renderArr;
  imgArr = [];
  oldPos;
limingzhe's avatar
limingzhe committed
83
  radioValue;
limingzhe's avatar
limingzhe committed
84 85 86 87 88 89
  curItem;
  bg: MySprite;
  changeSizeFlag = false;
  changeTopSizeFlag = false;
  changeRightSizeFlag = false;

limingzhe's avatar
limingzhe committed
90 91
  constructor() {
  }
limingzhe's avatar
limingzhe committed
92

limingzhe's avatar
limingzhe committed
93
  _bgItem = null;
limingzhe's avatar
limingzhe committed
94

limingzhe's avatar
limingzhe committed
95 96
  get bgItem() {
    return this._bgItem;
limingzhe's avatar
limingzhe committed
97 98
  }

limingzhe's avatar
limingzhe committed
99 100 101
  @Input()
  set bgItem(v) {
    this._bgItem = v;
limingzhe's avatar
limingzhe committed
102

limingzhe's avatar
limingzhe committed
103 104
    this.init();
  }
limingzhe's avatar
limingzhe committed
105 106

  onResize(event) {
limingzhe's avatar
limingzhe committed
107
    this.winResizeEventStream.next();
limingzhe's avatar
limingzhe committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  }


  ngOnInit() {

    this.initListener();

    // this.init();
    this.update();

  }

  ngOnDestroy() {
    window.cancelAnimationFrame(this.animationId);
  }


  ngOnChanges() {

  }


  onBackgroundUploadSuccess(e) {
    console.log('e: ', e);
    this.bgItem.url = e.url;
    this.refreshBackground();
  }

limingzhe's avatar
limingzhe committed
136 137 138 139 140 141 142 143 144 145
  onItemImgUploadSuccess(e, item) {
    item.pic_url = e.url;
    this.loadHotZonePic(item.pic, e.url);
  }

  onItemAudioUploadSuccess(e, item) {
    item.audio_url = e.url;
  }


limingzhe's avatar
limingzhe committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  refreshBackground(callBack = null) {

    if (!this.bg) {
      this.bg = new MySprite(this.ctx);
      this.renderArr.push(this.bg);
    }

    const bg = this.bg;
    if (this.bgItem.url) {

      bg.load(this.bgItem.url).then(() => {
        const rate1 = this.canvasWidth / bg.width;
        const rate2 = this.canvasHeight / bg.height;

        const rate = Math.min(rate1, rate2);
        bg.setScaleXY(rate);


        bg.x = this.canvasWidth / 2;
        bg.y = this.canvasHeight / 2;

        if (callBack) {
          callBack();
        }
      });
    }

  }


  addBtnClick() {
    // this.imgArr.push({});
    // this.hotZoneArr.push({});

    const item = this.getHotZoneItem();
    this.hotZoneArr.push(item);

limingzhe's avatar
limingzhe committed
183 184 185 186 187 188 189 190 191 192 193 194
    this.refreshItem(item);

    this.refreshHotZoneId();

  }

  deleteBtnClick(index) {

    const item = this.hotZoneArr.splice(index, 1)[0];
    removeItemFromArr(this.renderArr, item.pic);
    removeItemFromArr(this.renderArr, item.textLabel);

limingzhe's avatar
limingzhe committed
195 196 197 198 199 200
    this.refreshHotZoneId();

  }

  onImgUploadSuccessByImg(e, img) {
    img.pic_url = e.url;
limingzhe's avatar
limingzhe committed
201
    this.refreshImage(img);
limingzhe's avatar
limingzhe committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  }

  refreshImage(img) {

    this.hideAllLineDash();

    img.picItem = this.getPicItem(img);

    this.refreshImageId();

  }


  refreshHotZoneId() {
    for (let i = 0; i < this.hotZoneArr.length; i++) {
      this.hotZoneArr[i].index = i;

      if (this.hotZoneArr[i]) {
limingzhe's avatar
limingzhe committed
220
        this.hotZoneArr[i].title = 'item-' + (i + 1);
limingzhe's avatar
limingzhe committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

      }
    }
  }


  refreshImageId() {
    for (let i = 0; i < this.imgArr.length; i++) {
      this.imgArr[i].id = i;

      if (this.imgArr[i].picItem) {
        this.imgArr[i].picItem.text = 'Image-' + (i + 1);
      }
    }

  }

limingzhe's avatar
limingzhe committed
238
  getHotZoneItem(saveData = null) {
limingzhe's avatar
limingzhe committed
239 240 241 242 243 244 245 246 247 248 249 250


    const itemW = 200;
    const itemH = 200;
    const item = new HotZoneItem(this.ctx);
    item.setSize(itemW, itemH);
    item.anchorX = 0.5;
    item.anchorY = 0.5;

    item.x = this.canvasWidth / 2;
    item.y = this.canvasHeight / 2;

limingzhe's avatar
limingzhe committed
251 252
    item.itemType = this.defaultItemType;

limingzhe's avatar
limingzhe committed
253 254 255 256 257
    if (saveData) {

      const saveRect = saveData.rect;
      item.scaleX = saveRect.width / item.width;
      item.scaleY = saveRect.height / item.height;
limingzhe's avatar
limingzhe committed
258
      item.x = saveRect.x + saveRect.width / 2;
limingzhe's avatar
limingzhe committed
259 260 261 262 263 264
      item.y = saveRect.y + saveRect.height / 2;

    }

    item.showLineDash();

limingzhe's avatar
limingzhe committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    const pic = new HotZoneImg(this.ctx);
    pic.visible = false;
    item['pic'] = pic;
    if (saveData && saveData.pic_url) {
      this.loadHotZonePic(pic, saveData.pic_url);
    }
    pic.x = item.x;
    pic.y = item.y;
    this.renderArr.push(pic);

    const textLabel = new HotZoneLabel(this.ctx);
    textLabel.fontSize = this.hotZoneFontObj.size;
    textLabel.fontName = this.hotZoneFontObj.name;
    textLabel.fontColor = this.hotZoneFontObj.color;
    textLabel.textAlign = 'center';
    // textLabel.setOutline();
    // console.log('saveData:', saveData);
    item['textLabel'] = textLabel;
    textLabel.setScaleXY(this.mapScale);
    if (saveData && saveData.text) {
      textLabel.text = saveData.text;
      textLabel.refreshSize();
    }
    textLabel.x = item.x;
    textLabel.y = item.y;
    this.renderArr.push(textLabel);
limingzhe's avatar
limingzhe committed
291 292 293 294 295 296 297 298 299

    return item;
  }


  getPicItem(img, saveData = null) {


    const item = new EditorItem(this.ctx);
limingzhe's avatar
limingzhe committed
300
    item.load(img.pic_url).then(img => {
limingzhe's avatar
limingzhe committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

      let maxW, maxH;
      if (this.bg) {
        maxW = this.bg.width * this.bg.scaleX;
        maxH = this.bg.height * this.bg.scaleY;
      } else {
        maxW = this.canvasWidth;
        maxH = this.canvasHeight;
      }

      let scaleX = maxW / 3 / item.width;
      let scaleY = maxH / 3 / item.height;

      if (item.height * scaleX < this.canvasHeight) {
        item.setScaleXY(scaleX);
      } else {
        item.setScaleXY(scaleY);
      }
      item.x = this.canvasWidth / 2;
      item.y = this.canvasHeight / 2;

      if (saveData) {

        const saveRect = saveData.rect;
        item.setScaleXY(saveRect.width / item.width);
limingzhe's avatar
limingzhe committed
326
        item.x = saveRect.x + saveRect.width / 2;
limingzhe's avatar
limingzhe committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
        item.y = saveRect.y + saveRect.height / 2;

      } else {
        item.showLineDash();
      }

    });
    return item;
  }


  onAudioUploadSuccessByImg(e, img) {
    img.audio_url = e.url;
  }

  deleteItem(e, i) {
    // this.imgArr.splice(i , 1);
    // this.refreshImageId();

    this.hotZoneArr.splice(i, 1);
    this.refreshHotZoneId();
  }


limingzhe's avatar
limingzhe committed
351 352 353 354
  radioChange(e, item) {
    item.itemType = e;

    this.refreshItem(item);
limingzhe's avatar
limingzhe committed
355

limingzhe's avatar
limingzhe committed
356 357
    // console.log(' in radioChange e: ', e);
  }
limingzhe's avatar
limingzhe committed
358

limingzhe's avatar
limingzhe committed
359 360 361 362 363 364 365 366 367 368 369 370
  refreshItem(item) {
    switch (item.itemType) {
      case 'rect':
        this.setRectState(item);
        break;
      case 'pic':
        this.setPicState(item);
        break;
      case 'text':
        this.setTextState(item);
        break;
      default:
limingzhe's avatar
limingzhe committed
371

limingzhe's avatar
limingzhe committed
372 373
    }
  }
limingzhe's avatar
limingzhe committed
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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446


  init() {

    this.initData();
    this.initCtx();

    this.initItem();
  }

  initItem() {
    if (!this.bgItem) {
      this.bgItem = {};
    } else {
      this.refreshBackground(() => {

        // if (!this.imgItemArr) {
        //   this.imgItemArr = [];
        // } else {
        //   this.initImgArr();
        // }

        // console.log('aaaaa');

        if (!this.hotZoneItemArr) {
          this.hotZoneItemArr = [];
        } else {
          this.initHotZoneArr();
        }

      });
    }

  }

  initHotZoneArr() {

    // console.log('this.hotZoneArr: ', this.hotZoneArr);
    let curBgRect;
    if (this.bg) {
      curBgRect = this.bg.getBoundingBox();
    } else {
      curBgRect = {x: 0, y: 0, width: this.canvasWidth, height: this.canvasHeight};
    }

    let oldBgRect = this.bgItem.rect;
    if (!oldBgRect) {
      oldBgRect = curBgRect;
    }

    const rate = curBgRect.width / oldBgRect.width;

    console.log('rate: ', rate);

    this.hotZoneArr = [];
    const arr = this.hotZoneItemArr.concat();
    for (let i = 0; i < arr.length; i++) {

      const data = JSON.parse(JSON.stringify(arr[i]));
      // const img = {pic_url: data.pic_url};

      data.rect.x *= rate;
      data.rect.y *= rate;
      data.rect.width *= rate;
      data.rect.height *= rate;

      data.rect.x += curBgRect.x;
      data.rect.y += curBgRect.y;

      // img['picItem'] = this.getPicItem(img, data);
      // img['audio_url'] = arr[i].audio_url;
      // this.imgArr.push(img);

limingzhe's avatar
limingzhe committed
447 448 449 450 451 452 453
      const item = this.getHotZoneItem(data);
      item.audio_url = data.audio_url;
      item.pic_url = data.pic_url;
      item.text = data.text;
      item.itemType = data.itemType;
      this.refreshItem(item);

limingzhe's avatar
limingzhe committed
454 455 456 457 458 459
      console.log('item: ', item);
      this.hotZoneArr.push(item);

    }

    this.refreshHotZoneId();
limingzhe's avatar
limingzhe committed
460

limingzhe's avatar
limingzhe committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    // this.refreshImageId();

  }


  initImgArr() {

    console.log('this.imgItemArr: ', this.imgItemArr);
    let curBgRect;
    if (this.bg) {
      curBgRect = this.bg.getBoundingBox();
    } else {
      curBgRect = {x: 0, y: 0, width: this.canvasWidth, height: this.canvasHeight};
    }

    let oldBgRect = this.bgItem.rect;
    if (!oldBgRect) {
      oldBgRect = curBgRect;
    }

    const rate = curBgRect.width / oldBgRect.width;

    console.log('rate: ', rate);

    this.imgArr = [];
    const arr = this.imgItemArr.concat();
    for (let i = 0; i < arr.length; i++) {

      const data = JSON.parse(JSON.stringify(arr[i]));
      const img = {pic_url: data.pic_url};

      data.rect.x *= rate;
      data.rect.y *= rate;
      data.rect.width *= rate;
      data.rect.height *= rate;

      data.rect.x += curBgRect.x;
      data.rect.y += curBgRect.y;

      img['picItem'] = this.getPicItem(img, data);
      img['audio_url'] = arr[i].audio_url;
      this.imgArr.push(img);
    }
    this.refreshImageId();
  }


  initData() {

    this.canvasWidth = this.wrap.nativeElement.clientWidth;
    this.canvasHeight = this.wrap.nativeElement.clientHeight;
    this.mapScale = this.canvasWidth / this.canvasBaseW;
    this.renderArr = [];
    this.bg = null;

    this.imgArr = [];
    this.hotZoneArr = [];
  }

  initCtx() {
    this.ctx = this.canvas.nativeElement.getContext('2d');
    this.canvas.nativeElement.width = this.canvasWidth;
    this.canvas.nativeElement.height = this.canvasHeight;
  }


  mapDown(event) {

    this.oldPos = {x: this.mx, y: this.my};

limingzhe's avatar
limingzhe committed
531
    for (let i = 0; i < this.hotZoneArr.length; i++) {
limingzhe's avatar
limingzhe committed
532

limingzhe's avatar
limingzhe committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
      const item = this.hotZoneArr[i];
      let callback;
      let target;
      switch (item.itemType) {
        case 'rect':
          target = item;
          callback = this.clickedHotZoneRect.bind(this);
          break;
        case 'pic':
          target = item.pic;
          callback = this.clickedHotZonePic.bind(this);
          break;
        case 'text':
          target = item.textLabel;
          callback = this.clickedHotZoneText.bind(this);
          break;
      }
limingzhe's avatar
limingzhe committed
550

limingzhe's avatar
limingzhe committed
551 552 553
      if (this.checkClickTarget(target)) {
        callback(target);
        return;
limingzhe's avatar
limingzhe committed
554 555 556 557 558 559
      }
    }


  }

limingzhe's avatar
limingzhe committed
560

limingzhe's avatar
limingzhe committed
561
  mapMove(event) {
limingzhe's avatar
limingzhe committed
562 563 564
    if (!this.curItem) {
      return;
    }
limingzhe's avatar
limingzhe committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

    if (this.changeSizeFlag) {
      this.changeSize();

    } else if (this.changeTopSizeFlag) {
      this.changeTopSize();

    } else if (this.changeRightSizeFlag) {
      this.changeRightSize();

    } else {

      const addX = this.mx - this.oldPos.x;
      const addY = this.my - this.oldPos.y;
      this.curItem.x += addX;
      this.curItem.y += addY;
    }

    this.oldPos = {x: this.mx, y: this.my};
limingzhe's avatar
limingzhe committed
584 585 586

    this.saveDisabled = true;

limingzhe's avatar
limingzhe committed
587 588 589 590 591 592 593 594 595 596 597 598 599
  }

  mapUp(event) {
    this.curItem = null;
    this.changeSizeFlag = false;
    this.changeTopSizeFlag = false;
    this.changeRightSizeFlag = false;
  }


  changeSize() {
    const rect = this.curItem.getBoundingBox();

limingzhe's avatar
limingzhe committed
600 601
    let lenW = (this.mx - (rect.x + rect.width / 2)) * 2;
    let lenH = ((rect.y + rect.height / 2) - this.my) * 2;
limingzhe's avatar
limingzhe committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629


    let minLen = 20;
    let s;
    if (lenW < lenH) {
      if (lenW < minLen) {
        lenW = minLen;
      }
      s = lenW / this.curItem.width;

    } else {
      if (lenH < minLen) {
        lenH = minLen;
      }
      s = lenH / this.curItem.height;
    }


    // console.log('s: ', s);
    this.curItem.setScaleXY(s);
    this.curItem.refreshLabelScale();
  }


  changeTopSize() {
    const rect = this.curItem.getBoundingBox();

    // let lenW = ( this.mx - (rect.x + rect.width / 2) ) * 2;
limingzhe's avatar
limingzhe committed
630
    let lenH = ((rect.y + rect.height / 2) - this.my) * 2;
limingzhe's avatar
limingzhe committed
631 632 633 634 635 636 637 638 639 640 641


    let minLen = 20;
    let s;
    // if (lenW < lenH) {
    //   if (lenW < minLen) {
    //     lenW = minLen;
    //   }
    //   s = lenW / this.curItem.width;
    //
    // } else {
limingzhe's avatar
limingzhe committed
642 643 644 645
    if (lenH < minLen) {
      lenH = minLen;
    }
    s = lenH / this.curItem.height;
limingzhe's avatar
limingzhe committed
646 647 648 649 650 651 652 653 654 655 656
    // }


    // console.log('s: ', s);
    this.curItem.scaleY = s;
    this.curItem.refreshLabelScale();
  }

  changeRightSize() {
    const rect = this.curItem.getBoundingBox();

limingzhe's avatar
limingzhe committed
657
    let lenW = (this.mx - (rect.x + rect.width / 2)) * 2;
limingzhe's avatar
limingzhe committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 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
    // let lenH = ( (rect.y + rect.height / 2) - this.my ) * 2;

    let minLen = 20;
    let s;
    if (lenW < minLen) {
      lenW = minLen;
    }
    s = lenW / this.curItem.width;

    this.curItem.scaleX = s;
    this.curItem.refreshLabelScale();
  }

  changeItemSize(item) {
    this.curItem = item;
    this.changeSizeFlag = true;
  }

  changeItemTopSize(item) {
    this.curItem = item;
    this.changeTopSizeFlag = true;
  }

  changeItemRightSize(item) {
    this.curItem = item;
    this.changeRightSizeFlag = true;
  }

  changeCurItem(item) {

    this.hideAllLineDash();

    this.curItem = item;
    this.curItem.showLineDash();
  }

  hideAllLineDash() {

    for (let i = 0; i < this.imgArr.length; i++) {
      if (this.imgArr[i].picItem) {
        this.imgArr[i].picItem.hideLineDash();
      }
    }
  }


  update() {
liujiaxin's avatar
liujiaxin committed
705 706 707
    if (!this.ctx) {
      return;
    }
limingzhe's avatar
limingzhe committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725


    this.animationId = window.requestAnimationFrame(this.update.bind(this));
    // 清除画布内容
    this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);

    for (let i = 0; i < this.renderArr.length; i++) {
      this.renderArr[i].update(this);
    }

    // for (let i = 0; i < this.imgArr.length; i++) {
    //   const picItem = this.imgArr[i].picItem;
    //   if (picItem) {
    //     picItem.update(this);
    //   }
    // }

    this.updateArr(this.hotZoneArr);
limingzhe's avatar
limingzhe committed
726
    this.updatePos()
limingzhe's avatar
limingzhe committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748


    TWEEN.update();
  }

  updateArr(arr) {
    if (arr) {
      for (let i = 0; i < arr.length; i++) {
        arr[i].update();
      }
    }
  }


  renderAfterResize() {
    this.canvasWidth = this.wrap.nativeElement.clientWidth;
    this.canvasHeight = this.wrap.nativeElement.clientHeight;
    this.init();
  }

  initListener() {

limingzhe's avatar
limingzhe committed
749 750 751 752 753
    this.winResizeEventStream
      .pipe(debounceTime(500))
      .subscribe(data => {
        this.renderAfterResize();
      });
limingzhe's avatar
limingzhe committed
754 755 756 757 758 759 760 761 762 763 764 765 766 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 853 854 855 856 857 858

    if (this.IsPC()) {

      this.canvas.nativeElement.addEventListener('mousedown', (event) => {
        setMxMyByMouse(event);
        this.mapDown(event);
      });

      this.canvas.nativeElement.addEventListener('mousemove', (event) => {
        setMxMyByMouse(event);
        this.mapMove(event);
      });

      this.canvas.nativeElement.addEventListener('mouseup', (event) => {
        setMxMyByMouse(event);
        this.mapUp(event);
      });

      const setMxMyByMouse = (event) => {
        this.mx = event.offsetX;
        this.my = event.offsetY;
      };

    } else {

      this.canvas.nativeElement.addEventListener('touchstart', (event) => {
        setMxMyByTouch(event);
        this.mapDown(event);
      });

      this.canvas.nativeElement.addEventListener('touchmove', (event) => {
        setMxMyByTouch(event);
        this.mapMove(event);
      });

      this.canvas.nativeElement.addEventListener('touchend', (event) => {
        setMxMyByTouch(event);
        this.mapUp(event);
      });

      this.canvas.nativeElement.addEventListener('touchcancel', (event) => {
        setMxMyByTouch(event);
        this.mapUp(event);
      });


      const setMxMyByTouch = (event) => {
        if (event.touches.length <= 0) {
          return;
        }

        if (this.canvasLeft == null) {
          setParentOffset();
        }

        this.mx = event.touches[0].pageX - this.canvasLeft;
        this.my = event.touches[0].pageY - this.canvasTop;
      };


      const setParentOffset = () => {

        const rect = this.canvas.nativeElement.getBoundingClientRect();
        this.canvasLeft = rect.left;
        this.canvasTop = rect.top;
      };
    }
  }

  IsPC() {
    if (window['ELECTRON']) {
      return false; // 封装客户端标记
    }
    if (document.body.ontouchstart !== undefined) {
      return false;
    } else {
      return true;
    }
  }


  checkClickTarget(target) {

    const rect = target.getBoundingBox();
    if (this.checkPointInRect(this.mx, this.my, rect)) {
      return true;
    }
    return false;
  }

  checkPointInRect(x, y, rect) {
    if (x >= rect.x && x <= rect.x + rect.width) {
      if (y >= rect.y && y <= rect.y + rect.height) {
        return true;
      }
    }
    return false;
  }


  saveClick() {
    const bgItem = this.bgItem;
    if (this.bg) {
      bgItem['rect'] = this.bg.getBoundingBox();
    } else {
limingzhe's avatar
limingzhe committed
859 860 861 862 863 864
      bgItem['rect'] = {
        x: 0,
        y: 0,
        width: Math.round(this.canvasWidth * 100) / 100,
        height: Math.round(this.canvasHeight * 100) / 100
      };
limingzhe's avatar
limingzhe committed
865 866 867 868 869 870 871 872 873
    }


    const hotZoneItemArr = [];
    const hotZoneArr = this.hotZoneArr;
    for (let i = 0; i < hotZoneArr.length; i++) {

      const hotZoneItem = {
        index: hotZoneArr[i].index,
limingzhe's avatar
limingzhe committed
874 875 876 877 878 879 880 881 882 883
        pic_url: hotZoneArr[i].pic_url,
        text: hotZoneArr[i].text,
        audio_url: hotZoneArr[i].audio_url,
        itemType: hotZoneArr[i].itemType,
        fontSize: this.hotZoneFontObj.size,
        fontName: this.hotZoneFontObj.name,
        fontColor: this.hotZoneFontObj.color,
        fontScale: hotZoneArr[i].textLabel ? hotZoneArr[i].textLabel.scaleX : 1,
        imgScale: hotZoneArr[i].pic ? hotZoneArr[i].pic.scaleX : 1,
        mapScale: this.mapScale
limingzhe's avatar
limingzhe committed
884 885
      };
      hotZoneItem['rect'] = hotZoneArr[i].getBoundingBox();
limingzhe's avatar
limingzhe committed
886 887 888 889 890
      hotZoneItem['rect'].x = Math.round((hotZoneItem['rect'].x - bgItem['rect'].x) * 100) / 100;
      hotZoneItem['rect'].y = Math.round((hotZoneItem['rect'].y - bgItem['rect'].y) * 100) / 100;
      hotZoneItem['rect'].width = Math.round((hotZoneItem['rect'].width) * 100) / 100;
      hotZoneItem['rect'].height = Math.round((hotZoneItem['rect'].height) * 100) / 100;

limingzhe's avatar
limingzhe committed
891 892 893 894 895 896 897 898

      hotZoneItemArr.push(hotZoneItem);
    }

    console.log('hotZoneItemArr: ', hotZoneItemArr);

    this.save.emit({bgItem, hotZoneItemArr});
  }
limingzhe's avatar
limingzhe committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

  private updatePos() {
    this.hotZoneArr.forEach((item) => {
      let x, y;
      switch (item.itemType) {
        case 'rect':
          x = item.x;
          y = item.y;
          break;

        case 'pic':
          x = item.pic.x;
          y = item.pic.y;
          break;

        case 'text':
          x = item.textLabel.x;
          y = item.textLabel.y;
          break;
      }

      item.x = x;
      item.y = y;
      item.pic.x = x;
      item.pic.y = y;
      item.textLabel.x = x;
      item.textLabel.y = y;
    });
  }

  private setPicState(item: any) {
    item.visible = false;
    item.textLabel.visible = false;
    item.pic.visible = true;
  }

  private setRectState(item: any) {
    item.visible = true;
    item.textLabel.visible = false;
    item.pic.visible = false;
  }

  private setTextState(item: any) {
    item.visible = false;
    item.pic.visible = false;
    item.textLabel.visible = true;
  }

  private clickedHotZoneRect(item: any) {
    if (this.checkClickTarget(item)) {

      if (item.lineDashFlag && this.checkClickTarget(item.arrow)) {
        this.changeItemSize(item);
      } else if (item.lineDashFlag && this.checkClickTarget(item.arrowTop)) {
        this.changeItemTopSize(item);
      } else if (item.lineDashFlag && this.checkClickTarget(item.arrowRight)) {
        this.changeItemRightSize(item);
      } else {
        this.changeCurItem(item);
      }
      return;
    }
  }

  private clickedHotZonePic(item: any) {
    if (this.checkClickTarget(item)) {
      this.curItem = item;
    }
  }

  private clickedHotZoneText(item: any) {
    if (this.checkClickTarget(item)) {
      this.curItem = item;
    }
  }

  saveText(item) {
    item.textLabel.text = item.text;
  }

  private loadHotZonePic(pic: HotZoneImg, url) {
    const baseLen = this.hotZoneImgSize * this.mapScale;
    pic.load(url).then(() => {
      const s = getMinScale(pic, baseLen);
      pic.setScaleXY(s);
    });
  }
limingzhe's avatar
limingzhe committed
986
}