gpt4 book ai didi

javascript - 如何在 angular2 中使用 winwheel.js 回调

转载 作者:行者123 更新时间:2023-11-30 15:17:04 26 4
gpt4 key购买 nike

我一直在进行轮盘设计,我需要一个轮子,所以我使用了 winwheel.js 库。

wheel;


wheelSpinning = false;

constructor() {
}

ngAfterViewInit() {
this.initWheel();
}

initWheel() {
this.wheel = new Winwheel({
'numSegments': 8, // Specify number of segments.
'outerRadius': 212, // Set radius to so wheel fits the background.
'innerRadius': 150, // Set inner radius to make wheel hollow.
'pointerAngle': 90,
'pointerGuide': // Turn pointer guide on.
{
'display': true,
'strokeStyle': 'red',
'lineWidth': 3
},
'segments': // Define segments including colour and text.
[
{ 'fillStyle': '#eae56f', 'text': 'Prize 1' },
{ 'fillStyle': '#89f26e', 'text': 'Prize 2' },
{ 'fillStyle': '#7de6ef', 'text': 'Prize 3' },
{ 'fillStyle': '#e7706f', 'text': 'Prize 4' },
{ 'fillStyle': '#eae56f', 'text': 'Prize 5' },
{ 'fillStyle': '#89f26e', 'text': 'Prize 6' },
{ 'fillStyle': '#7de6ef', 'text': 'Prize 7' },
{ 'fillStyle': '#e7706f', 'text': 'Prize 8' }
],
'animation': // Define spin to stop animation.
{
'type': 'spinToStop',
'duration': 5,
'spins': 8,
'callbackFinished': 'alertPrize()'
}
});
}

public alertPrize() {
console.log('wheel');
}

// -------------------------------------------------------
// Click handler for spin button.
// -------------------------------------------------------
startSpin() {
// Ensure that spinning can't be clicked again while already running.
if (this.wheelSpinning === false) {
this.wheel.startAnimation();
this.wheelSpinning = true;
}
}

// -------------------------------------------------------
// Function for reset button.
// -------------------------------------------------------
resetWheel() {
this.wheel.stopAnimation(false); // Stop the animation, false as param so does not call callback function.
this.wheel.rotationAngle = 0; // Re-set the wheel angle to 0 degrees.
this.wheel.draw(); // Call draw to render changes to the wheel.

this.wheelSpinning = false; // Reset to false to power buttons and spin can be clicked again.
}

一切正常,但是轮子停止旋转后,它需要一个回调函数,以便我们可以在轮子停止旋转后编写我们的逻辑,所以我这样传递,

'callbackFinished': 'alertPrize()'

但在这种情况下,alertPrize 需要在全局范围内,以便 winwheel.js 可以访问此函数。由于我的警报功能是在组件内部声明的,因此它无法访问 winwheel js。

如果我像这样在 index.html 中定义我的函数,那么它是可访问的,因为它在全局范围内

   <script>
function alertPrize() {
console.log('wheel');
}
</script>

但我想要组件内部的 alertPrize() 以便我可以在其中编写一些逻辑。

有没有办法解决这个问题。

最佳答案

我最终在 2266 行修改库 Winwheel.js 以删除将函数字符串解析为简单函数回调的 eval 函数:

eval(winwheelToDrawDuringAnimation.animation.callbackFinished);

winwheelToDrawDuringAnimation.animation.callbackFinished();

然后在你的代码中

'callbackFinished': 'alertPrize()' 变为 'callbackFinished': this.alertPrize.bind(this)

我将组件范围绑定(bind)到回调,以便它可以访问组件类属性和方法。

也许更好的方法是 fork git 存储库并在那里进行此更改,但我没有这样做,因为该存储库不在 bowernpm 上。

关于javascript - 如何在 angular2 中使用 winwheel.js 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44306571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com