code an html app with different shapes that i can pull out of a dropdown menu and into the main screen area. the shapes can be edited by right clicking, including changing size and color, also with a delete button. the main area has gravity physics applied, so shapes fall to rest at the bottom edge of the screen
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is one complete example. In this demo we use Matter.js to provide gravity and dragging physics. A dropdown and button let you “pull out” a shape (a rectangle or a circle) that then falls under gravity. You can right‑click a shape to pop up a custom editing panel where you can change its “size” (width/height for rectangles, radius for circles), its color, or choose to delete it.
You can save the code as, for example, “index.html” and open it in your browser.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Draggable Shapes with Physics and Editor</title>
<!-- Matter.js CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.19.0/matter.min.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
user-select: none;
}
#controls {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
background: rgba(255,255,255,0.85);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
#editor {
position: absolute;
display: none;
z-index: 20;
background: white;
border: 1px solid #aaa;
padding: 10px;
border-radius: 4px;
}
#editor label {
display: block;
margin-top: 5px;
}
canvas {
/* the matter.js renderer will create its own canvas element */
display: block;
}
</style>
</head>
<body>
<div id="controls">
<label for="shapeSelect">Select a shape:</label>
<select id="shapeSelect">
<option value="rectangle">Rectangle</option>
<option value="circle">Circle</option>
</select>
<button id="addShapeBtn">Add Shape</button>
</div>
<!-- Custom Editor Menu (hidden by default) -->
<div id="editor">
<form id="editForm">
<div id="sizeFields"></div>
<label for="colorInput">Color:</label>
<input type="color" id="colorInput" value="#FF0000">
<br>
<button type="submit">Apply Changes</button>
<button type="button" id="deleteBtn" style="margin-left:10px;">Delete</button>
</form>
</div>
<script>
// Module aliases from Matter.js
const Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
Bodies = Matter.Bodies,
Composite = Matter.Composite,
Body = Matter.Body,
Mouse = Matter.Mouse,
MouseConstraint = Matter.MouseConstraint,
Query = Matter.Query;
// Create an engine
const engine = Engine.create();
const world = engine.world;
// Create a renderer and add it to document.body
const render = Render.create({
element: document.body,
engine: engine,
options: {
width: window.innerWidth,
height: window.innerHeight,
wireframes: false,
background: "#f0f0f0"
}
});
Render.run(render);
// Create runner
const runner = Runner.create();
Runner.run(runner, engine);
// Add the ground (a static body)
const ground = Bodies.rectangle(
window.innerWidth / 2,
window.innerHeight - 10,
window.innerWidth,
20,
{ isStatic: true, render: { fillStyle: "#060a19" } }
);
Composite.add(world, ground);
// Enable mouse drag from Matter.js
const mouse = Mouse.create(render.canvas);
const mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: { visible: false }
}
});
Composite.add(world, mouseConstraint);
render.mouse = mouse; // keep the mouse in sync with rendering
// Keep track of shapes that we add. (We will add a custom property "isShape" to exclude ground.)
// Also, store original dimensions for scaling.
function createShape(type) {
let body, custom = {};
// place at the top center
const x = window.innerWidth / 2;
const y = 50;
if (type === "rectangle") {
// default rectangle: width = 80, height = 50, color red.
const width = 80, height = 50;
body = Bodies.rectangle(x, y, width, height, {
restitution: 0.5,
render: { fillStyle: "#FF0000" }
});
custom.width = width;
custom.height = height;
} else if (type === "circle") {
const radius = 30;
body = Bodies.circle(x, y, radius, {
restitution: 0.5,
render: { fillStyle: "#FF0000" }
});
custom.radius = radius;
}
// Mark as our shape so we can skip ground etc.
body.isShape = true;
// Save the type and custom info
body.custom = custom;
body.shapeType = type;
Composite.add(world, body);
}
// Handle Add Shape button
document.getElementById("addShapeBtn").addEventListener("click", function() {
const type = document.getElementById("shapeSelect").value;
createShape(type);
});
// --- Context Menu / Editing Panel Handling ---
let selectedBody = null; // The Matter body selected for editing
const editor = document.getElementById("editor");
const editForm = document.getElementById("editForm");
const sizeFieldsDiv = document.getElementById("sizeFields");
const colorInput = document.getElementById("colorInput");
const deleteBtn = document.getElementById("deleteBtn");
// Prevent the browser’s default right-click on the renderer’s canvas.
render.canvas.addEventListener("contextmenu", function(e) {
e.preventDefault();
});
// When right-clicking on the canvas, see if a shape was clicked.
render.canvas.addEventListener("mousedown", function(e) {
// Only consider right-click (button==2)
if (e.button !== 2) return;
// Get mouse position relative to canvas
const rect = render.canvas.getBoundingClientRect();
const mousePos = { x: e.clientX - rect.left, y: e.clientY - rect.top };
// Query all bodies at this point (filtering for ones that are our shapes)
const bodies = Composite.allBodies(world);
const hits = Query.point(bodies, mousePos).filter(b => b.isShape);
if (hits.length > 0) {
selectedBody = hits[0];
showEditor(e.clientX, e.clientY, selectedBody);
} else {
// If no shape found, hide editor.
hideEditor();
}
});
// Function to fill editor fields based on the selected shape
function showEditor(pageX, pageY, body) {
// Position the editor panel near the mouse
editor.style.left = pageX + "px";
editor.style.top = pageY + "px";
// Clear previous size fields.
sizeFieldsDiv.innerHTML = "";
// Depending on type, add appropriate input fields
if (body.shapeType === "rectangle") {
const widthLabel = document.createElement("label");
widthLabel.textContent = "Width:";
const widthInput = document.createElement("input");
widthInput.type = "number";
widthInput.id = "widthInput";
widthInput.value = body.custom.width;
const heightLabel = document.createElement("label");
heightLabel.textContent = "Height:";
const heightInput = document.createElement("input");
heightInput.type = "number";
heightInput.id = "heightInput";
heightInput.value = body.custom.height;
sizeFieldsDiv.appendChild(widthLabel);
sizeFieldsDiv.appendChild(widthInput);
sizeFieldsDiv.appendChild(heightLabel);
sizeFieldsDiv.appendChild(heightInput);
} else if (body.shapeType === "circle") {
const radiusLabel = document.createElement("label");
radiusLabel.textContent = "Radius:";
const radiusInput = document.createElement("input");
radiusInput.type = "number";
radiusInput.id = "radiusInput";
radiusInput.value = body.custom.radius;
sizeFieldsDiv.appendChild(radiusLabel);
sizeFieldsDiv.appendChild(radiusInput);
}
// Set the current color.
// We assume the body.render.fillStyle is a hex color.
colorInput.value = body.render.fillStyle || "#FF0000";
// Show the editor div.
editor.style.display = "block";
}
// Hide the editor panel
function hideEditor() {
editor.style.display = "none";
selectedBody = null;
}
// Handle the form submission: update shape properties.
editForm.addEventListener("submit", function(e) {
e.preventDefault();
if (!selectedBody) return;
// Update size based on type.
if (selectedBody.shapeType === "rectangle") {
const newWidth = Number(document.getElementById("widthInput").value);
const newHeight = Number(document.getElementById("heightInput").value);
// Determine current dimensions from our custom data.
const curW = selectedBody.custom.width;
const curH = selectedBody.custom.height;
// Compute scale factors.
const scaleX = newWidth / curW;
const scaleY = newHeight / curH;
Body.scale(selectedBody, scaleX, scaleY);
// Update our stored dimensions.
selectedBody.custom.width = newWidth;
selectedBody.custom.height = newHeight;
} else if (selectedBody.shapeType === "circle") {
const newRadius = Number(document.getElementById("radiusInput").value);
const curR = selectedBody.custom.radius;
const scaleFactor = newRadius / curR;
Body.scale(selectedBody, scaleFactor, scaleFactor);
selectedBody.custom.radius = newRadius;
}
// Update color.
selectedBody.render.fillStyle = colorInput.value;
hideEditor();
});
// Handle deletion
deleteBtn.addEventListener("click", function() {
if (selectedBody) {
Composite.remove(world, selectedBody);
hideEditor();
}
});
// Hide the editor if clicking anywhere else
document.addEventListener("mousedown", function(e) {
// If the click isn’t on the editor panel and isn't a right-click triggered on a shape, hide editor.
if (!editor.contains(e.target)) {
// For left-click only.
if (e.button === 0) hideEditor();
}
});
// Adjust canvas size on window resize.
window.addEventListener("resize", function() {
render.canvas.width = window.innerWidth;
render.canvas.height = window.innerHeight;
// Also reposition the ground.
Body.setPosition(ground, { x: window.innerWidth / 2, y: window.innerHeight - 10 });
Body.setVertices(ground, [
{ x: 0, y: window.innerHeight },
{ x: window.innerWidth, y: window.innerHeight },
{ x: window.innerWidth, y: window.innerHeight - 20 },
{ x: 0, y: window.innerHeight - 20 }
]);
});
</script>
</body>
</html>
Explanation
Matter.js Physics:
We create a Matter.js engine, renderer, and runner. A static “ground” (a wide rectangle at the bottom) is added so when shapes are added their gravity will make them fall and come to rest on this floor.
Adding Shapes:
The dropdown and “Add Shape” button call a function that creates a new Matter body. For rectangles a default width (80) and height (50) is used; for circles a default radius (30) is used. A custom property is attached to each body (the custom object) so we can later compute scaling.
Dragging:
A Matter.MouseConstraint is added so you can drag shapes around with the mouse.
Editing via Right‑Click:
We listen for right‑mouse clicks on the canvas. If a shape is found underneath the click, a custom floating editor panel appears. Depending on the shape type, you can change width/height (or radius) and the color. When you click “Apply Changes,” the code uses Body.scale to adjust the body’s size and updates its stored custom data. You may also delete the shape by clicking the “Delete” button.
Responsive Canvas:
The canvas resizes when the window size changes.
This example is meant to illustrate one way to achieve the requirements. You can extend it further by adding more shapes or additional editing options as needed.