Spaces:
Running
Running
File size: 8,649 Bytes
8bc6196 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
document.addEventListener('DOMContentLoaded', () => {
// Matter.js setup
const Engine = Matter.Engine,
Render = Matter.Render,
World = Matter.World,
Bodies = Matter.Bodies,
Mouse = Matter.Mouse,
MouseConstraint = Matter.MouseConstraint;
const canvas = document.getElementById('sandbox-canvas');
const engine = Engine.create();
const render = Render.create({
canvas: canvas,
engine: engine,
options: {
width: canvas.offsetWidth,
height: canvas.offsetHeight,
wireframes: false,
background: 'transparent'
}
});
// Create boundaries
const wallOptions = { isStatic: true, render: { visible: false } };
const ground = Bodies.rectangle(canvas.offsetWidth/2, canvas.offsetHeight, canvas.offsetWidth, 10, wallOptions);
const leftWall = Bodies.rectangle(0, canvas.offsetHeight/2, 10, canvas.offsetHeight, wallOptions);
const rightWall = Bodies.rectangle(canvas.offsetWidth, canvas.offsetHeight/2, 10, canvas.offsetHeight, wallOptions);
World.add(engine.world, [ground, leftWall, rightWall]);
// Mouse control
const mouse = Mouse.create(render.canvas);
const mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
});
World.add(engine.world, mouseConstraint);
// Material properties
const materials = {
sand: {
friction: 0.8,
restitution: 0.1,
density: 1.6,
frictionAir: 0.01,
render: {
fillStyle: '#c2b280',
strokeStyle: '#a5976e',
lineWidth: 1
}
},
water: {
friction: 0.01,
restitution: 0.2,
density: 1.0,
frictionAir: 0.001,
render: {
lineWidth: 1
}
},
oil: {
friction: 0.005,
restitution: 0.1,
density: 0.9,
frictionAir: 0.001,
render: {
fillStyle: '#FFD700',
strokeStyle: '#FFC000',
lineWidth: 1
}
},
snow: {
friction: 0.3,
restitution: 0.05,
density: 0.3,
frictionAir: 0.05,
render: {
fillStyle: '#ffffff',
strokeStyle: '#f0f9ff',
lineWidth: 1
}
}
};
let currentMaterial = 'sand';
let gravityEnabled = true;
// Handle material selection
document.addEventListener('click', (e) => {
if (e.target.classList.contains('material-btn')) {
currentMaterial = e.target.dataset.material;
document.querySelectorAll('.material-btn').forEach(btn => {
btn.classList.toggle('active', btn === e.target);
});
}
});
// Toggle gravity button
const gravityBtn = document.createElement('button');
gravityBtn.className = 'sandbox-btn bg-gray-700 hover:bg-gray-800 mt-4 w-full';
gravityBtn.textContent = 'Toggle Gravity';
gravityBtn.addEventListener('click', () => {
gravityEnabled = !gravityEnabled;
engine.world.gravity.y = gravityEnabled ? 1 : 0;
gravityBtn.textContent = gravityEnabled ? 'Disable Gravity' : 'Enable Gravity';
});
document.querySelector('#sandbox').appendChild(gravityBtn);
// Create particles on click
canvas.addEventListener('mousedown', (e) => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
let count = 10;
let sizeMin = 4;
let sizeMax = 8;
if (currentMaterial === 'snow') {
count = 15;
sizeMin = 2;
sizeMax = 6;
engine.world.gravity.y = 3.5;
} else {
engine.world.gravity.y = 9.8;
}
for (let i = 0; i < count; i++) {
const particle = Bodies.circle(
x + Math.random() * 40 - 20,
y + Math.random() * 40 - 20,
Math.random() * (sizeMax - sizeMin) + sizeMin,
materials[currentMaterial]
);
World.add(engine.world, particle);
}
});
// Also work with touch events
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
const touch = e.touches[0];
const rect = canvas.getBoundingClientRect();
const x = touch.clientX - rect.left;
const y = touch.clientY - rect.top;
let count = 15;
let sizeMin = 5;
let sizeMax = 10;
if (currentMaterial === 'snow') {
count = 25;
sizeMin = 3;
sizeMax = 7;
engine.world.gravity.y = 3.5;
} else {
engine.world.gravity.y = 9.8;
}
for (let i = 0; i < count; i++) {
const particle = Bodies.circle(
x + Math.random() * 60 - 30,
y + Math.random() * 60 - 30,
Math.random() * (sizeMax - sizeMin) + sizeMin,
materials[currentMaterial]
);
World.add(engine.world, particle);
}
}, { passive: false });
// AI Tools functionality
document.addEventListener('DOMContentLoaded', () => {
// Tab switching
document.addEventListener('click', (e) => {
if (e.target.classList.contains('tab')) {
const tabId = e.target.dataset.tab;
document.querySelectorAll('.tab, .tab-content').forEach(el => {
el.classList.remove('active');
});
e.target.classList.add('active');
document.getElementById(`${tabId}-tab`).classList.add('active');
}
});
// Image preview
const imageUpload = document.getElementById('image-upload');
const imagePreview = document.getElementById('image-preview');
imageUpload.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
imagePreview.src = event.target.result;
imagePreview.style.display = 'block';
document.querySelector('#image-tab .upload-label span').textContent = 'Change Image';
};
reader.readAsDataURL(file);
}
});
// Video preview
const videoUpload = document.getElementById('video-upload');
const videoPreview = document.getElementById('video-preview');
videoUpload.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
videoPreview.src = URL.createObjectURL(file);
videoPreview.style.display = 'block';
document.querySelector('#video-tab .upload-label span').textContent = 'Change Video';
}
});
// Generate/Transform buttons (mock functionality)
document.getElementById('generate-btn').addEventListener('click', () => {
const prompt = document.querySelector('#image-tab .prompt-input').value;
if (prompt) {
alert(`Generating image based on: "${prompt}"\n(Note: This is a mock implementation. Connect to an actual AI API for real functionality.)`);
}
});
document.getElementById('transform-btn').addEventListener('click', () => {
const prompt = document.querySelector('#image-tab .prompt-input').value;
if (imagePreview.src && prompt) {
alert(`Transforming image based on: "${prompt}"\n(Note: This is a mock implementation. Connect to an actual AI API for real functionality.)`);
}
});
});
// Run the engine with initial settings
engine.world.gravity.scale = 0.001; // Matter.js uses scale factor
engine.world.gravity.y = 9.8;
Engine.run(engine);
Render.run(render);
// Handle window resize
window.addEventListener('resize', () => {
render.options.width = canvas.offsetWidth;
render.options.height = canvas.offsetHeight;
render.canvas.width = canvas.offsetWidth;
render.canvas.height = canvas.offsetHeight;
});
}); |