Spaces:
Running
Running
Create slotmachine.js
Browse files- slotmachine.js +143 -0
slotmachine.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Import necessary packages
|
| 2 |
+
import { Scene, WebGLRenderer, PerspectiveCamera } from 'three';
|
| 3 |
+
import { OrbitControls } from 'three/examples/webgl2/controls';
|
| 4 |
+
|
| 5 |
+
// Initialize the scene
|
| 6 |
+
const scene = new Scene();
|
| 7 |
+
scene.setSize(600, 600);
|
| 8 |
+
|
| 9 |
+
// Create a camera
|
| 10 |
+
const camera = new PerspectiveCamera(45, 1, 0.1, 1000);
|
| 11 |
+
camera.position.z = 3;
|
| 12 |
+
|
| 13 |
+
// Add a light source
|
| 14 |
+
const light = new PointLight(0xffffff, 1, 1000, 0.5);
|
| 15 |
+
light.position.set(-20, 20, -20);
|
| 16 |
+
scene.add(light);
|
| 17 |
+
|
| 18 |
+
// Create a renderer
|
| 19 |
+
const renderer = new WebGLRenderer({
|
| 20 |
+
canvas: document.getElementById('canvas'),
|
| 21 |
+
antialias: true,
|
| 22 |
+
preserveDrawingBuffer: false,
|
| 23 |
+
});
|
| 24 |
+
renderer.setSize(600, 600);
|
| 25 |
+
|
| 26 |
+
// Add controls
|
| 27 |
+
const controls = new OrbitControls(camera, renderer.domElement);
|
| 28 |
+
controls.enablePan();
|
| 29 |
+
controls.enableZoom();
|
| 30 |
+
|
| 31 |
+
// Create a group to hold the slot machine elements
|
| 32 |
+
const slotMachineGroup = new Group();
|
| 33 |
+
|
| 34 |
+
// Create four stacked windows with different backgrounds and text
|
| 35 |
+
const window1 = new Group().addChild(new Cylinder(5, 5, 20, 0.5)).setMatrixAutoScale();
|
| 36 |
+
window1.material.color = new Color('#0000ff');
|
| 37 |
+
window1.material.opacity = 0.7;
|
| 38 |
+
window1.material.transparent = true;
|
| 39 |
+
window1.material.wireframe = false;
|
| 40 |
+
|
| 41 |
+
const window2 = new Cylinder(5, 5, 20, 0.5).setMatrixAutoScale();
|
| 42 |
+
window2.material.color = new Color('#ff0000');
|
| 43 |
+
window2.material.opacity = 0.7;
|
| 44 |
+
window2.material.transparent = true;
|
| 45 |
+
window2.material.wireframe = false;
|
| 46 |
+
|
| 47 |
+
const window3 = new Cylinder(5, 5, 20, 0.5).setMatrixAutoScale();
|
| 48 |
+
window3.material.color = new Color('#00ff00');
|
| 49 |
+
window3.material.opacity = 0.7;
|
| 50 |
+
window3.material.transparent = true;
|
| 51 |
+
window3.material.wireframe = false;
|
| 52 |
+
|
| 53 |
+
const window4 = new Cylinder(5, 5, 20, 0.5).setMatrixAutoScale();
|
| 54 |
+
window4.material.color = new Color('#ff00ff');
|
| 55 |
+
window4.material.opacity = 0.7;
|
| 56 |
+
window4.material.transparent = true;
|
| 57 |
+
window4.material.wireframe = false;
|
| 58 |
+
|
| 59 |
+
// Add the windows to the slot machine group
|
| 60 |
+
slotMachineGroup.addChild(window1);
|
| 61 |
+
slotMachineGroup.addChild(window2);
|
| 62 |
+
slotMachineGroup.addChild(window3);
|
| 63 |
+
slotMachineGroup.addChild(window4);
|
| 64 |
+
|
| 65 |
+
// Add buttons for copy, result, and restart
|
| 66 |
+
const copyButton = new Cylinder(5, 5, 10, 0.5).setMatrixAutoScale();
|
| 67 |
+
copyButton.material.color = new Color('#ff0000');
|
| 68 |
+
copyButton.material.opacity = 0.7;
|
| 69 |
+
copyButton.material.transparent = true;
|
| 70 |
+
copyButton.material.wireframe = false;
|
| 71 |
+
copyButton.position.set(10, 25, 0);
|
| 72 |
+
|
| 73 |
+
const resultButton = new Cylinder(5, 5, 10, 0.5).setMatrixAutoScale();
|
| 74 |
+
resultButton.material.color = new Color('#ff0000');
|
| 75 |
+
resultButton.material.opacity = 0.7;
|
| 76 |
+
resultButton.material.transparent = true;
|
| 77 |
+
resultButton.material.wireframe = false;
|
| 78 |
+
resultButton.position.set(10, 50, 0);
|
| 79 |
+
|
| 80 |
+
const restartButton = new Cylinder(5, 5, 10, 0.5).setMatrixAutoScale();
|
| 81 |
+
restartButton.material.color = new Color('#ff0000');
|
| 82 |
+
restartButton.material.opacity = 0.7;
|
| 83 |
+
restartButton.material.transparent = true;
|
| 84 |
+
restartButton.material.wireframe = false;
|
| 85 |
+
restartButton.position.set(10, 85, 0);
|
| 86 |
+
|
| 87 |
+
// Add the buttons to the slot machine group
|
| 88 |
+
slotMachineGroup.addChild(copyButton);
|
| 89 |
+
slotMachineGroup.addChild(resultButton);
|
| 90 |
+
slotMachineGroup.addChild(restartButton);
|
| 91 |
+
|
| 92 |
+
// Add the slot machine group to the scene
|
| 93 |
+
scene.add(slotMachineGroup);
|
| 94 |
+
|
| 95 |
+
// Add controls to the scene
|
| 96 |
+
controls.update();
|
| 97 |
+
|
| 98 |
+
// Add event listeners for button clicks
|
| 99 |
+
window1.addEventListener('click', function() {
|
| 100 |
+
console.log('Copy button clicked');
|
| 101 |
+
});
|
| 102 |
+
|
| 103 |
+
window2.addEventListener('click', function() {
|
| 104 |
+
console.log('Result button clicked');
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
window3.addEventListener('click', function() {
|
| 108 |
+
console.log('Restart button clicked');
|
| 109 |
+
});
|
| 110 |
+
|
| 111 |
+
// Add the scene to the document
|
| 112 |
+
document.body.appendChild(renderer.domElement);
|
| 113 |
+
|
| 114 |
+
// Start the animation loop
|
| 115 |
+
const frameRate = 10; // update every frame
|
| 116 |
+
requestAnimationFrame(animate);
|
| 117 |
+
|
| 118 |
+
function animate() {
|
| 119 |
+
// Update the slot machine elements
|
| 120 |
+
window1.material.color.setHex(Math.floor(Math.random() * 256));
|
| 121 |
+
window2.material.color.setHex(Math.floor(Math.random() * 256));
|
| 122 |
+
window3.material.color.setHex(Math.floor(Math.random() * 256));
|
| 123 |
+
window4.material.color.setHex(Math.floor(Math.random() * 256));
|
| 124 |
+
|
| 125 |
+
// Update the buttons' text based on the current state of the slot machine
|
| 126 |
+
copyButton.material.color.setHex(Math.floor(Math.random() * 256));
|
| 127 |
+
resultButton.material.color.setHex(Math.floor(Math.random() * 256));
|
| 128 |
+
restartButton.material.color.setHex(Math.floor(Math.random() * 256));
|
| 129 |
+
|
| 130 |
+
// Update the buttons' position based on the current state of the slot machine
|
| 131 |
+
copyButton.position.x = Math.random() * 100 - 50;
|
| 132 |
+
resultButton.position.x = Math.random() * 100 - 50;
|
| 133 |
+
restartButton.position.x = Math.random() * 100 - 50;
|
| 134 |
+
|
| 135 |
+
// Update the slot machine group's position based on the current state of the slot machine
|
| 136 |
+
slotMachineGroup.position.set(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50);
|
| 137 |
+
|
| 138 |
+
// Update the scene
|
| 139 |
+
renderer.render(scene, camera);
|
| 140 |
+
|
| 141 |
+
// Request another frame
|
| 142 |
+
requestAnimationFrame(animate);
|
| 143 |
+
}
|