javascript - 使用概率 javaScript 获取结果
问题描述
下面是我制作的旋转轮的代码。我想要发生的是使用概率来获得结果。假设没有 1 我希望它有 20% 的机会,没有 2 我希望有 10% 的机会等等。我无法使用 javascript 将此应用到旋转轮。我可以通过哪些方式做到这一点?
(function() {
const wheel = document.querySelector('.wheel');
const startButton = document.querySelector('.button');
const display = document.querySelector('.display');
let deg = 0;
let zoneSize = 45; // deg
// Counter clockwise
const symbolSegments = {
1: "Frog",
2: "Snail",
3: "Dolphin",
4: "Ladybug",
5: "Koala",
6: "Unicorn",
7: "Dragon",
8: "Snowman",
}
const handleWin = (actualDeg) => {
const winningSymbolNr = Math.ceil(actualDeg / zoneSize);
display.innerHTML = symbolSegments[winningSymbolNr];
}
startButton.addEventListener('click', () => {
startButton.style.pointerEvents = 'none';
deg = Math.floor(5000 + Math.random() * 5000);
wheel.style.transition = 'all 10s ease-out';
wheel.style.transform = `rotate(${deg}deg)`;
wheel.classList.add('blur');
});
wheel.addEventListener('transitionend', () => {
wheel.classList.remove('blur');
startButton.style.pointerEvents = 'auto';
wheel.style.transition = 'none';
const actualDeg = deg % 360;
wheel.style.transform = `rotate(${actualDeg}deg)`;
handleWin(actualDeg);
});
})();
解决方案
推荐阅读
- python - AllenNLP Reading Comprehension results are different in UI Demo and Python Library
- c - File transfer successful but won't exit while loop in C
- java - Protected class in java
- makefile - gdb compile issue yyin and lexer_init
- python - How would I manage to create this CLI python interface (it's pretty complex)
- google-api - NextPageToken gives invalid input error, 400 - Google Directory API ChomeDevices
- node.js - how can I get form data from react to node.js?
- python - Django Randomly Select List Item to Display In Template
- sql - 根据 Julian JDE 日期过滤 SYSDATE
- javascript - 使用 javascript 从 html5 详细信息摘要数据元素创建超链接