index.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="utf-8" />
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  5. <title>Dice</title>
  6. <script>
  7. const DICE_NUM_MIN = 1;
  8. const DICE_RAND_MIN = 1;
  9. const DICE_RAND_MAX = 6;
  10. function rand_int(min, max) {
  11. return Math.floor(Math.random() * (max - min + 1)) + min;
  12. }
  13. function rand_dice() {
  14. return rand_int(DICE_RAND_MIN, DICE_RAND_MAX);
  15. }
  16. function get_dice_num() {
  17. n = parseInt(document.getElementById('dice_num').value);
  18. if(isNaN(n) || n < DICE_NUM_MIN) {
  19. return 1;
  20. }
  21. return n;
  22. }
  23. function set_dice_num(n) {
  24. document.getElementById('dice_num').value = Math.max(n, DICE_NUM_MIN);
  25. }
  26. function increment() {
  27. set_dice_num(get_dice_num() + 1);
  28. }
  29. function decrement() {
  30. set_dice_num(get_dice_num() - 1);
  31. }
  32. function prependChild(parent_tag, child_tag) {
  33. parent_tag.insertBefore(child_tag, parent_tag.firstChild);
  34. }
  35. function roll() {
  36. roll_tag = document.createElement('div');
  37. dice_num = get_dice_num();
  38. for(i=0; i<dice_num; i++) {
  39. dice_tag = document.createElement('span');
  40. dice_tag.appendChild(document.createTextNode(rand_dice() + ' '));
  41. roll_tag.appendChild(dice_tag);
  42. }
  43. prependChild(document.getElementById('rolls'), roll_tag);
  44. }
  45. </script>
  46. <style>
  47. .src {
  48. font-size: 0.64em;
  49. }
  50. .src a {
  51. color: gray;
  52. text-decoration: none;
  53. }
  54. .src a:hover {
  55. text-decoration: underline;
  56. }
  57. </style>
  58. <p>
  59. dices
  60. <input type="text" id="dice_num" align="middle"
  61. type="number" min="1" size="2" value="6">
  62. <input type="button" onclick="decrement()" value="-">
  63. <input type="button" onclick="increment()" value="+">
  64. <input type="button" onclick="roll()" value="roll">
  65. </p>
  66. <div id="rolls"></div>
  67. <p class='src'><a href='https://git.hammerle.me/fphammerle/dice'>src</a></p>
  68. </html>