Заставить Колесо Фортуны остановиться на определенном месте в Angular 4 - PullRequest
0 голосов
/ 15 мая 2018

Я пытаюсь сделать приложение Wheel of fortune для приложения Ionic 3 с angular 4. Все работает нормально, но просто останавливается случайным образом.Проблема, с которой я сталкиваюсь, заключается в том, что я хочу остановить его в определенном месте в соответствии с моими данными, так что если я получу свой предмет один раз. Он не должен прийти снова, пока не будут использованы все предметы.

  import { Component, ViewChild, ElementRef } from '@angular/core';
  import { NavController, NavParams } from 'ionic-angular';

  @Component({
    selector: 'page-wheel',
    templateUrl: 'wheel.html',
  })
  export class WheelPage {
    @ViewChild('myCanvas') myCanvas: ElementRef;
    @ViewChild('spin') spin: ElementRef;
    public ctx: CanvasRenderingContext2D;
    color = ['#fbc', '#f88', '#fbc', '#f88', '#fbc', '#f88', "#fbc", "#f67"];
    label = ['Beer', 'Peg', 'Shot', 'Bottle', 'Food', 'Beer1', 'Bomb', "Fire"];
    slices = this.color.length;
    sliceDeg = 360 / this.slices;
    deg: any;
    public speed: any;
    slowDownRand: any = 0;
    isStopped:any = false;
    width: any;
    center: any;
    lock = false;
    constructor(public navCtrl: NavController, public navParams: NavParams) {
      this.speed = 0;
      this.deg = this.rand(0, 360);
      let canvas = document.createElement('canvas');
      this.width = canvas.width; // size
      this.center = this.width / 2;      // center


    }

    ngAfterViewInit(): void {
      this.ctx = (<HTMLCanvasElement>this.myCanvas.nativeElement).getContext('2d');
      this.anim();
    }

    rand(min, max) {
      return Math.random() * (max - min) + min;
    }
    deg2rad(deg) {
      return deg * Math.PI / 180;
    }
    drawSlice(deg, color) {
      this.ctx.beginPath();
      this.ctx.fillStyle = color;
      this.ctx.moveTo(this.center, this.center);
      this.ctx.arc(this.center, this.center, this.width / 2, this.deg2rad(deg), this.deg2rad(deg + this.sliceDeg));
      this.ctx.lineTo(this.center, this.center);
      this.ctx.fill();
    }

    drawText(deg, text) {
      this.ctx.save();
      this.ctx.translate(this.center, this.center);
      this.ctx.rotate(this.deg2rad(deg));
      this.ctx.textAlign = "right";
      this.ctx.fillStyle = "#fff";
      this.ctx.font = 'bold 30px sans-serif';
      this.ctx.fillText(text, 130, 10);
      this.ctx.restore();
    }

    drawImg() {
      this.ctx.clearRect(0, 0, this.width, this.width);
      for (var i = 0; i < this.slices; i++) {
        this.drawSlice(this.deg, this.color[i]);
        this.drawText(this.deg + this.sliceDeg / 2, this.label[i]);
        this.deg += this.sliceDeg;
      }
    }
    anim() {
      this.deg += this.speed;
      this.deg %= 360;

      // Increment speed
      if (!this.isStopped && this.speed < 3) {
        this.speed = this.speed + 1 * 0.1;
      }
      // Decrement Speed
      if (this.isStopped) {
        if (!this.lock) {
          this.lock = true;
          this.slowDownRand = this.rand(0.994, 0.998);
        }
        this.speed = this.speed > 0.2 ? this.speed *= this.slowDownRand : 0;
      }
      // Stopped!
      if (this.lock && !this.speed) {
        var ai = Math.floor(((360 - this.deg - 90) % 360) / this.sliceDeg); // deg 2 Array Index
        ai = (this.slices + ai) % this.slices; // Fix negative index
        return alert("You got:\n" + this.label[ai]); // Get Array Item from end Degree
      }

      this.drawImg();
      window.requestAnimationFrame(this.anim.bind(this));
    };

    onStopClick(){
        this.isStopped = true;
    }

  }

HTML-файл

  <ion-header>

    <ion-navbar>
      <ion-title text-center class="fontMont-Regular">Spin Wheel</ion-title>
    </ion-navbar>

  </ion-header>


  <ion-content no-padding class="setBackground">
    <ion-row style="margin-top:15%">
      <ion-col col-12 text-center>
        <div id="wheel">
          <canvas #myCanvas width="auto" height="300"></canvas>
        </div>
      </ion-col>
      <ion-col offset-1 col-10>
          <button #spin class="stopButton" (click)="onStopClick()">Stop!</button>
      </ion-col>
    </ion-row>
  </ion-content>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...