test.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Enhanced Button Selection</title>
  7. <style>
  8. canvas {
  9. border: 1px solid #000;
  10. display: block;
  11. margin: 0 auto;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <canvas id="buttonCanvas" width="480" height="360"></canvas>
  17. <script>
  18. const canvas = document.getElementById('buttonCanvas');
  19. const ctx = canvas.getContext('2d');
  20. const buttonWidth = 100;
  21. const buttonHeight = 100;
  22. const padding = 10;
  23. const rows = 3;
  24. const cols = 4;
  25. const images = [
  26. 'https://via.placeholder.com/100/FF0000/FFFFFF?text=Red',
  27. 'https://via.placeholder.com/100/0000FF/FFFFFF?text=Blue',
  28. 'https://via.placeholder.com/100/00FF00/FFFFFF?text=Green',
  29. 'https://via.placeholder.com/100/FFFFFF/000000?text=White',
  30. 'https://via.placeholder.com/100/FFAA00/FFFFFF?text=Orange',
  31. 'https://via.placeholder.com/100/AAAAAA/FFFFFF?text=Gray',
  32. 'https://via.placeholder.com/100/FF00FF/FFFFFF?text=Magenta',
  33. 'https://via.placeholder.com/100/00FFFF/FFFFFF?text=Cyan',
  34. 'https://via.placeholder.com/100/FFFF00/FFFFFF?text=Yellow',
  35. 'https://via.placeholder.com/100/000000/FFFFFF?text=Black',
  36. 'https://via.placeholder.com/100/FF7777/FFFFFF?text=Pink',
  37. 'https://via.placeholder.com/100/7777FF/FFFFFF?text=Purple',
  38. ];
  39. let buttons = [];
  40. let selectedButtons = new Set();
  41. function loadImages(imageUrls, callback) {
  42. const loadedImages = [];
  43. let imagesToLoad = imageUrls.length;
  44. imageUrls.forEach((url, index) => {
  45. const img = new Image();
  46. img.src = url;
  47. img.onload = () => {
  48. loadedImages[index] = img;
  49. imagesToLoad--;
  50. if (imagesToLoad === 0) callback(loadedImages);
  51. };
  52. });
  53. }
  54. function createButtons(images) {
  55. let x, y;
  56. for (let row = 0; row < rows; row++) {
  57. for (let col = 0; col < cols; col++) {
  58. const index = row * cols + col;
  59. x = col * (buttonWidth + padding);
  60. y = row * (buttonHeight + padding);
  61. buttons.push({ x, y, width: buttonWidth, height: buttonHeight, image: images[index], id: index });
  62. }
  63. }
  64. }
  65. function drawButtons() {
  66. ctx.clearRect(0, 0, canvas.width, canvas.height);
  67. buttons.forEach(button => {
  68. // 绘制按钮图片
  69. ctx.drawImage(button.image, button.x, button.y, button.width, button.height);
  70. if (selectedButtons.has(button.id)) {
  71. // 绘制半透明灰色遮罩
  72. ctx.fillStyle = 'rgba(50, 50, 50, 0.5)';
  73. ctx.fillRect(button.x, button.y, button.width, button.height);
  74. // 绘制白色对勾图标
  75. ctx.fillStyle = 'white';
  76. ctx.font = '20px Arial';
  77. ctx.textAlign = 'center';
  78. ctx.textBaseline = 'middle';
  79. ctx.fillText('✔', button.x + button.width / 2, button.y + button.height / 2);
  80. // 添加边框
  81. ctx.strokeStyle = 'black';
  82. ctx.lineWidth = 5;
  83. ctx.strokeRect(button.x, button.y, button.width, button.height);
  84. }
  85. });
  86. }
  87. function handleCanvasClick(event) {
  88. const rect = canvas.getBoundingClientRect();
  89. const mouseX = event.clientX - rect.left;
  90. const mouseY = event.clientY - rect.top;
  91. buttons.forEach(button => {
  92. if (
  93. mouseX >= button.x &&
  94. mouseX <= button.x + button.width &&
  95. mouseY >= button.y &&
  96. mouseY <= button.y + button.height
  97. ) {
  98. if (selectedButtons.has(button.id)) {
  99. selectedButtons.delete(button.id); // 取消选中
  100. } else {
  101. selectedButtons.add(button.id); // 选中按钮
  102. }
  103. drawButtons();
  104. }
  105. });
  106. }
  107. canvas.addEventListener('click', handleCanvasClick);
  108. loadImages(images, loadedImages => {
  109. createButtons(loadedImages);
  110. drawButtons();
  111. });
  112. </script>
  113. </body>
  114. </html>