如何解决 self.context.anim is not a function error in ionic 2?

阿拜辛格

我正在使用 Html5 Canvas 在 ionic 2 和 angular 2 中创建命运之轮,它的动画效果很好,但我想要当我点击一个按钮时它应该开始动画。但是当我在按钮上调用函数时它会显示

类型错误:self.context.anim 不是函数

这是我的home.ts页面

import { Component, ViewChild } from '@angular/core';

import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
    @ViewChild('layout') canvasRef;
    ngAfterViewInit() {
        let canvas = this.canvasRef.nativeElement;
        let context = canvas.getContext('2d');
        canvas.width = 300;
        canvas.height = 300;
        function rand(min, max) {
            return Math.random() * (max - min) + min;
        }
        var color = ['#fbc', '#f88', '#fbc', '#f88', '#fbc', '#f88', "#fbc", "#f67"];
        var label = ['10', '200', '50', '100', '5', '500', '0', "jPOT"];
        var slices = color.length;
        var sliceDeg = 360 / slices;
        var deg = rand(0, 360);
        var speed = 0;
        var slowDownRand = 0;
        var ctx = context;
        var width = canvas.width; // size
        var center = width / 2;      // center
        var isStopped = false;
        var lock = false;

        function deg2rad(deg) {
            return deg * Math.PI / 180;
        }

        function drawSlice(deg, color) {
            ctx.beginPath();
            ctx.fillStyle = color;
            ctx.moveTo(center, center);
            ctx.arc(center, center, width / 2, deg2rad(deg), deg2rad(deg + sliceDeg));
            ctx.lineTo(center, center);
            ctx.fill();
        }

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

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



        //document.getElementById("spin").addEventListener("mousedown", function () {
        //    isStopped = true;
        //}, false);

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

                drawImg();
                window.requestAnimationFrame(anim);
            } ;
            //anim();

    }

  constructor(public navCtrl: NavController) {    
  }

  onLink(url: string) {
      window.open(url);
  }
}

这是我的home.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>    
<ion-content class="home" padding>
    <canvas #layout></canvas>
    <button (click)="anim(animation)">
        Button Item
    </button>   
</ion-content>

请帮帮我。提前致谢。

苏拉·拉奥

anim为不在里面的类设置函数ngAfterViewInit()

像这样:

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

                drawImg();
                window.requestAnimationFrame(this.anim.bind(this));
            } 
 }//end of class

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在IONIC中创建新应用程序时如何解决PROXY ERROR?

如何解决Ionic2中的CORS问题和JSONP问题

如何解决Android Error type 3?

如何解决错误TypeError:self._form.on不是函数?

如何解决 Fragment 无法转换为 Context 错误?

self.context.delete 不是 angular2 中的函数

即使指定了Path变量,如何解决“ CreateProcess error = 2,系统找不到指定的文件”的问题(跨平台)

我该如何解决-rake:aborted!Mysql2 :: Error:用户'root'@'localhost'的访问被拒绝(使用密码:NO)

如何解决设备ionic 4中的安装问题

python-如何解决KeyError:2?

如何解决AAPT2错误?

如何解决dpkg错误[2]?

如何使用烧瓶中的变量访问 self.context 属性

如何解决/修复Azure Function主机崩溃的问题?

如何解决我的 react hook TypeError: setUser is not a function 问题?

如何解决错误 app.use(store) is not a function

如何解决取决于:libwinpr2-2?

如何解决Django中的Value Error Assign错误?

如何解决这个错误 Minified React error #200; 访问

如何解决本地JS文件的BUILD ERROR角度问题

如何解决ERROR TypeError:这是未定义的[角度]

如何解决 Dataframe to_numeric Error (Python)?

如何解决@error:GEKKO中的方程式定义

如何解决 Uncaught (in promise): Error: StaticInjectorError in Angular 7?

如何解决Mysql Near Sintax Error CI

如何解决“ Error Domain = kCFErrorDomainCFNetwork代码= -1022”(null)“错误

如何解决:“ pygame.error:无法打开图片”

如何解决android app install error 110?

如何解决 Conda HTTP000error?