<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GUI Drawing Component</title>
<style>
body { margin: 0; }
#palette { position: absolute; top: 10px; left: 10px; }
#editPanel { position: absolute; top: 10px; right: 10px; background: #f0f0f0; padding: 10px; border: 1px solid #ccc; }
canvas { border: 1px solid black; }
</style>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
<div id="palette"></div>
<div id="editPanel" style="display: none;"></div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const palette = document.getElementById('palette');
const editPanel = document.getElementById('editPanel');
let shapes = [];
let particles = [];
let instruments = [
{name: 'Line', type: 'line', props: {}},
{name: 'Rectangle', type: 'rect', props: {}},
{name: 'Circle', type: 'circle', props: {}}
];
let mode = 'normal'; // 'normal', 'drawing', 'editing'
let drawClickCount = 0;
let tempShape = null;
let selected = null;
let hovered = null;
let moveMode = false;
let movingIndex = -1;
let lastTime = performance.now();
let mouse = {x: 0, y: 0};
class Shape {
constructor() {
this.color = '#000000';
this.force = {x: 0, y: 0};
this.createParticles = false;
this.particlesPerSec = 1;
this.particleColor1 = '#ff0000';
this.particleColor2 = '#0000ff';
this.randomColor = false;
this.addSpeed = false;
this.setSpeed = false;
this.affectIndirect = false;
this.vxMultiplier = 1;
this.vyMultiplier = 1;
this.particleAccumulator = 0;
this.effectNormal = false; // for line only
this.type = 'shape';
}
setProps(props) {
Object.assign(this, props);
}
draw(ctx) {}
getControlPoints() { return []; }
moveControl(index, p) {}
getCenter() { return {x: 0, y: 0}; }
isMouseOver(pos) { return false; }
isPointInArea(pos) { return false; }
generateRandomPos() { return null; }
drawForceVector(ctx) {
const center = this.getCenter();
const end = {x: center.x + this.force.x, y: center.y + this.force.y};
const dx = end.x - center.x;
const dy = end.y - center.y;
let len = Math.sqrt(dx * dx + dy * dy);
if (len === 0) return;
const unitX = dx / len;
const unitY = dy / len;
const perpX = -unitY;
const perpY = unitX;
const triSize = 4;
const numTris = Math.floor(len / triSize);
for (let i = 1; i <= numTris; i++) {
const t = i * triSize / len;
const base = {x: center.x + t * dx, y: center.y + t * dy};
const tip = {x: base.x + unitX * (triSize / 2), y: base.y + unitY * (triSize / 2)};
const base1 = {x: base.x + perpX * 2, y: base.y + perpY * 2};
const base2 = {x: base.x - perpX * 2, y: base.y - perpY * 2};
ctx.beginPath();
ctx.moveTo(tip.x, tip.y);
ctx.lineTo(base1.x, base1.y);
ctx.lineTo(base2.x, base2.y);
ctx.closePath();
ctx.fillStyle = 'red';
ctx.fill();
}
}
}
class Line extends Shape {
constructor() {
super();
this.type = 'line';
this.start = {x: 0, y: 0};
this.end = {x: 0, y: 0};
}
setPoint1(p) { this.start = {...p}; }
setPoint2(p) { this.end = {...p}; }
getControlPoints() { return [this.start, this.end]; }
moveControl(index, p) {
if (index === 0) this.start = {...p};
else this.end = {...p};
}
getCenter() { return {x: (this.start.x + this.end.x) / 2, y: (this.start.y + this.end.y) / 2}; }
draw(ctx) {
ctx.beginPath();
ctx.moveTo(this.start.x, this.start.y);
ctx.lineTo(this.end.x, this.end.y);
ctx.strokeStyle = this.color;
ctx.lineWidth = 2;
ctx.stroke();
}
isMouseOver(pos) {
return distToSegment(pos, this.start, this.end) < 5;
}
isPointInArea(pos) {
if (!this.effectNormal) {
return distToSegment(pos, this.start, this.end) < 5;
} else {
const a = this.start, b = this.end;
const cross = (b.x - a.x) * (pos.y - a.y) - (b.y - a.y) * (pos.x - a.x);
return cross < 0;
}
}
generateRandomPos() {
if (!this.effectNormal) {
const t = Math.random();
return {x: this.start.x + t * (this.end.x - this.start.x), y: this.start.y + t * (this.end.y - this.start.y)};
} else {
for (let tries = 0; tries < 100; tries++) {
const rx = Math.random() * width;
const ry = Math.random() * height;
if (this.isPointInArea({x: rx, y: ry})) return {x: rx, y: ry};
}
return null;
}
}
}
class Rect extends Shape {
constructor() {
super();
this.type = 'rect';
this.p1 = {x: 0, y: 0};
this.p2 = {x: 0, y: 0};
}
setPoint1(p) { this.p1 = {...p}; }
setPoint2(p) { this.p2 = {...p}; }
getControlPoints() { return [this.p1, this.p2]; }
moveControl(index, p) {
if (index === 0) this.p1 = {...p};
else this.p2 = {...p};
}
getCenter() {
const minx = Math.min(this.p1.x, this.p2.x);
const miny = Math.min(this.p1.y, this.p2.y);
const maxx = Math.max(this.p1.x, this.p2.x);
const maxy = Math.max(this.p1.y, this.p2.y);
return {x: (minx + maxx) / 2, y: (miny + maxy) / 2};
}
draw(ctx) {
const minx = Math.min(this.p1.x, this.p2.x);
const miny = Math.min(this.p1.y, this.p2.y);
const w = Math.max(this.p1.x, this.p2.x) - minx;
const h = Math.max(this.p1.y, this.p2.y) - miny;
ctx.strokeStyle = this.color;
ctx.lineWidth = 2;
ctx.strokeRect(minx, miny, w, h);
}
isMouseOver(pos) {
const minx = Math.min(this.p1.x, this.p2.x);
const miny = Math.min(this.p1.y, this.p2.y);
const maxx = Math.max(this.p1.x, this.p2.x);
const maxy = Math.max(this.p1.y, this.p2.y);
return pos.x >= minx && pos.x <= maxx && pos.y >= miny && pos.y <= maxy;
}
isPointInArea(pos) {
return this.isMouseOver(pos);
}
generateRandomPos() {
const minx = Math.min(this.p1.x, this.p2.x);
const miny = Math.min(this.p1.y, this.p2.y);
const maxx = Math.max(this.p1.x, this.p2.x);
const maxy = Math.max(this.p1.y, this.p2.y);
return {x: minx + Math.random() * (maxx - minx), y: miny + Math.random() * (maxy - miny)};
}
}
class Circle extends Shape {
constructor() {
super();
this.type = 'circle';
this.center = {x: 0, y: 0};
this.edgePoint = {x: 0, y: 0};
}
setPoint1(p) { this.center = {...p}; }
setPoint2(p) { this.edgePoint = {...p}; }
get radius() { return dist(this.center, this.edgePoint); }
getControlPoints() { return [this.center, this.edgePoint]; }
moveControl(index, p) {
if (index === 0) {
const vec = {x: this.edgePoint.x - this.center.x, y: this.edgePoint.y - this.center.y};
this.center = {...p};
this.edgePoint = {x: p.x + vec.x, y: p.y + vec.y};
} else {
this.edgePoint = {...p};
}
}
getCenter() { return {...this.center}; }
draw(ctx) {
ctx.beginPath();
ctx.arc(this.center.x, this.center.y, this.radius, 0, Math.PI * 2);
ctx.strokeStyle = this.color;
ctx.lineWidth = 2;
ctx.stroke();
}
isMouseOver(pos) {
return dist(pos, this.center) <= this.radius;
}
isPointInArea(pos) {
return dist(pos, this.center) <= this.radius;
}
generateRandomPos() {
const theta = Math.random() * Math.PI * 2;
const r = Math.sqrt(Math.random()) * this.radius;
return {x: this.center.x + r * Math.cos(theta), y: this.center.y + r * Math.sin(theta)};
}
}
function dist(p1, p2) {
const dx = p1.x - p2.x;
const dy = p1.y - p2.y;
return Math.sqrt(dx * dx + dy * dy);
}
function distToSegment(p, a, b) {
const dx = b.x - a.x;
const dy = b.y - a.y;
const len2 = dx * dx + dy * dy;
if (len2 === 0) return dist(p, a);
let t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2;
t = Math.max(0, Math.min(1, t));
const proj = {x: a.x + t * dx, y: a.y + t * dy};
return dist(p, proj);
}
function drawControlPoint(p) {
ctx.beginPath();
ctx.arc(p.x, p.y, 5, 0, Math.PI * 2);
ctx.fillStyle = 'blue';
ctx.fill();
}
function redraw() {
ctx.clearRect(0, 0, width, height);
shapes.forEach(s => s.draw(ctx));
particles.forEach(p => {
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(p.pos.x, p.pos.y, 2, 0, Math.PI * 2);
ctx.fill();
});
if (selected) {
selected.getControlPoints().forEach(drawControlPoint);
selected.drawForceVector(ctx);
}
if (hovered && (mode !== 'editing' || hovered !== selected)) {
hovered.getControlPoints().forEach(drawControlPoint);
}
if (mode === 'drawing' && drawClickCount >= 1) {
tempShape.draw(ctx);
}
}
function updateEditPanel() {
if (!selected) {
editPanel.style.display = 'none';
return;
}
editPanel.style.display = 'block';
let html = `
<label>Force X: <input type="number" value="${selected.force.x}" oninput="selected.force.x = parseFloat(this.value) || 0; redraw()"></label><br>
<label>Force Y: <input type="number" value="${selected.force.y}" oninput="selected.force.y = parseFloat(this.value) || 0; redraw()"></label><br>
<label>Color: <input type="color" value="${selected.color}" oninput="selected.color = this.value; redraw()"></label><br>
<label>Create Particles: <input type="checkbox" ${selected.createParticles ? 'checked' : ''} oninput="selected.createParticles = this.checked;"></label><br>
<label>Particles/sec: <input type="number" value="${selected.particlesPerSec}" oninput="selected.particlesPerSec = parseFloat(this.value) || 1;"></label><br>
<label>Particle Color 1: <input type="color" value="${selected.particleColor1}" oninput="selected.particleColor1 = this.value;"></label><br>
<label>Particle Color 2: <input type="color" value="${selected.particleColor2}" oninput="selected.particleColor2 = this.value;"></label><br>
<label>Random Color: <input type="checkbox" ${selected.randomColor ? 'checked' : ''} oninput="selected.randomColor = this.checked;"></label><br>
<label>Add Speed: <input type="checkbox" ${selected.addSpeed ? 'checked' : ''} oninput="selected.addSpeed = this.checked;"></label><br>
<label>Set Speed: <input type="checkbox" ${selected.setSpeed ? 'checked' : ''} oninput="selected.setSpeed = this.checked;"></label><br>
<label>Affect Indirect: <input type="checkbox" ${selected.affectIndirect ? 'checked' : ''} oninput="selected.affectIndirect = this.checked;"></label><br>
<label>VX Multiplier: <input type="number" value="${selected.vxMultiplier}" oninput="selected.vxMultiplier = parseFloat(this.value) || 1;"></label><br>
<label>VY Multiplier: <input type="number" value="${selected.vyMultiplier}" oninput="selected.vyMultiplier = parseFloat(this.value) || 1;"></label><br>
`;
if (selected.type === 'line') {
html += `<label>Effect Normal: <input type="checkbox" ${selected.effectNormal ? 'checked' : ''} oninput="selected.effectNormal = this.checked; redraw()"></label><br>`;
}
html += `
<button onclick="saveToPalette()">Save to Palette</button>
<button onclick="deleteSelected()">Delete</button>
`;
editPanel.innerHTML = html;
}
function saveToPalette() {
const props = {
color: selected.color,
force: {...selected.force},
createParticles: selected.createParticles,
particlesPerSec: selected.particlesPerSec,
particleColor1: selected.particleColor1,
particleColor2: selected.particleColor2,
randomColor: selected.randomColor,
addSpeed: selected.addSpeed,
setSpeed: selected.setSpeed,
affectIndirect: selected.affectIndirect,
vxMultiplier: selected.vxMultiplier,
vyMultiplier: selected.vyMultiplier
};
if (selected.type === 'line') props.effectNormal = selected.effectNormal;
const count = instruments.length - 2;
const name = `Saved ${count}`;
const config = {name, type: selected.type, props};
instruments.push(config);
addPaletteButton(config);
}
function deleteSelected() {
shapes = shapes.filter(s => s !== selected);
selected = null;
mode = 'normal';
moveMode = false;
updateEditPanel();
redraw();
}
function addPaletteButton(config) {
const btn = document.createElement('button');
btn.textContent = config.name;
btn.onclick = () => startDraw(config);
palette.appendChild(btn);
}
function startDraw(instrument) {
if (instrument.type === 'line') tempShape = new Line();
else if (instrument.type === 'rect') tempShape = new Rect();
else if (instrument.type === 'circle') tempShape = new Circle();
tempShape.setProps(instrument.props);
mode = 'drawing';
drawClickCount = 0;
}
function getMousePos(evt) {
const rect = canvas.getBoundingClientRect();
return {x: evt.clientX - rect.left, y: evt.clientY - rect.top};
}
canvas.addEventListener('mousemove', evt => {
mouse = getMousePos(evt);
hovered = shapes.find(s => s.isMouseOver(mouse)) || null;
if (mode === 'drawing' && drawClickCount === 1) {
tempShape.setPoint2(mouse);
redraw();
} else {
redraw();
}
});
canvas.addEventListener('mousedown', evt => {
const pos = getMousePos(evt);
if (mode === 'drawing') {
if (drawClickCount === 0) {
tempShape.setPoint1(pos);
drawClickCount = 1;
} else {
tempShape.setPoint2(pos);
shapes.push(tempShape);
selected = tempShape;
mode = 'editing';
moveMode = false;
drawClickCount = 0;
tempShape = null;
updateEditPanel();
}
redraw();
return;
}
if (mode === 'editing' && moveMode) {
selected.moveControl(movingIndex, pos);
moveMode = false;
redraw();
return;
}
let clickedShape = null;
let clickedIndex = -1;
for (let s of shapes) {
const points = s.getControlPoints();
for (let i = 0; i < points.length; i++) {
if (dist(pos, points[i]) < 10) {
clickedShape = s;
clickedIndex = i;
break;
}
}
if (clickedShape) break;
}
if (clickedShape) {
selected = clickedShape;
mode = 'editing';
moveMode = true;
movingIndex = clickedIndex;
updateEditPanel();
} else {
selected = null;
mode = 'normal';
moveMode = false;
updateEditPanel();
}
redraw();
});
function animate(now) {
const dt = (now - lastTime) / 1000;
lastTime = now;
shapes.forEach(s => {
if (s.createParticles) {
s.particleAccumulator += s.particlesPerSec * dt;
while (s.particleAccumulator >= 1) {
const pos = s.generateRandomPos();
if (pos) {
const color = s.randomColor ? (Math.random() < 0.5 ? s.particleColor1 : s.particleColor2) : s.particleColor1;
particles.push({pos: {...pos}, vel: {x: 0, y: 0}, color});
}
s.particleAccumulator -= 1;
}
}
});
particles.forEach(p => {
p.pos.x += p.vel.x * dt;
p.pos.y += p.vel.y * dt;
shapes.forEach(s => {
if (s.isPointInArea(p.pos)) {
if (s.setSpeed) {
p.vel.x = s.force.x;
p.vel.y = s.force.y;
}
if (s.addSpeed) {
p.vel.x += s.force.x * dt;
p.vel.y += s.force.y * dt;
}
if (s.affectIndirect) {
p.pos.x += p.vel.x * s.vxMultiplier * dt;
p.pos.y += p.vel.y * s.vyMultiplier * dt;
}
}
});
});
particles = particles.filter(p => p.pos.x >= 0 && p.pos.x <= width && p.pos.y >= 0 && p.pos.y <= height);
redraw();
requestAnimationFrame(animate);
}
// Init
instruments.forEach(addPaletteButton);
requestAnimationFrame(animate);
</script>
</body>
</html>