about summary refs log tree commit diff stats
path: root/js/peep/app.js
blob: 777d78f9ff9b4cc5faa55537a16df0c90728143e (plain) (blame)
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
const canvas = document.getElementById('peep');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext('2d');

let draggedShape = null;

const shapes = [
    { type: 'circle', x: 100, y: canvas.height - 32, radius: 30 },
    { type: 'rectangle', x: 200, y: canvas.height - 32, width: 60, height: 60 },
    { type: 'triangle', x: 300, y: canvas.height - 32, sideLength: 50 },
    { type: 'octagon', x: 400, y: canvas.height - 32, sideLength: 40 },
    { type: 'pentagon', x: 500, y: canvas.height - 32, sideLength: 45 },
];

const drawCircle = (shape) => {
    ctx.arc(shape.x, shape.y, shape.radius, 0, Math.PI * 2);
};

const drawRectangle = (shape) => {
    ctx.rect(shape.x - shape.width / 2, shape.y - shape.height / 2, shape.width, shape.height);
};

const drawTriangle = (shape) => {
    const sideLength = shape.sideLength;
    const height = (Math.sqrt(3) / 2) * sideLength;
    ctx.moveTo(shape.x, shape.y - height / 2);
    ctx.lineTo(shape.x - sideLength / 2, shape.y + height / 2);
    ctx.lineTo(shape.x + sideLength / 2, shape.y + height / 2);
    ctx.closePath();
};

const drawOctagon = (shape) => {
    const sideLength = shape.sideLength;
    const angle = (2 * Math.PI) / 8;
    ctx.moveTo(shape.x + sideLength * Math.cos(0), shape.y + sideLength * Math.sin(0));
    for (let i = 1; i < 8; i++) {
        ctx.lineTo(shape.x + sideLength * Math.cos(angle * i), shape.y + sideLength * Math.sin(angle * i));
    }
    ctx.closePath();
};

const drawPentagon = (shape) => {
    const sideLength = shape.sideLength;
    const angle = (2 * Math.PI) / 5;
    ctx.moveTo(shape.x + sideLength * Math.cos(0), shape.y + sideLength * Math.sin(0));
    for (let i = 1; i < 5; i++) {
        ctx.lineTo(shape.x + sideLength * Math.cos(angle * i), shape.y + sideLength * Math.sin(angle * i));
    }
    ctx.closePath();
};

const draw = () => {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = '#ddd';
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    const rectHeight = 4 * 16;
    ctx.fillStyle = '#adadad';
    ctx.fillRect(0, canvas.height - rectHeight, canvas.width, rectHeight);

    ctx.strokeStyle = 'white';
    for (const pulse of pulses) {
        ctx.beginPath();
        ctx.lineWidth = 5;
        ctx.shadowColor = 'rgba(255, 255, 255, 0.5)';
        ctx.shadowBlur = 20;
        ctx.arc(pulse.x, pulse.y, pulse.radius, 0, Math.PI * 2);
        ctx.stroke();
        // Increase the radius of the pulse
        pulse.radius += 5;

        // Naive collision detection
        for (const shape of shapes) {
            if (isPointInsideShape(pulse.x, pulse.y, shape)) {
                console.log('Collision detected with shape:', shape.type);
            }
        }
    }

    ctx.shadowColor = 'transparent'; // reset shadow color
    ctx.shadowBlur = 0; // reset shadow blur

    ctx.fillStyle = 'black';

    shapes.forEach((shape) => {
        ctx.beginPath();
        if (shape.type === 'circle') {
            drawCircle(shape);
        } else if (shape.type === 'rectangle') {
            drawRectangle(shape);
        } else if (shape.type === 'triangle') {
            drawTriangle(shape);
        } else if (shape.type === 'octagon') {
            drawOctagon(shape);
        } else if (shape.type === 'pentagon') {
            drawPentagon(shape);
        }
        ctx.fill();
    });
};

const isPointInsideShape = (x, y, shape) => {
    if (shape.type === 'circle') {
        return Math.hypot(shape.x - x, shape.y - y) < shape.radius;
    } else if (shape.type === 'rectangle') {
        return x > shape.x - shape.width / 2 && x < shape.x + shape.width / 2 && y > shape.y - shape.height / 2 && y < shape.y + shape.height / 2;
    } else if (shape.type === 'triangle') {
        return isPointInsideTriangle(x, y, shape);
    } else if (shape.type === 'octagon') {
        return isPointInsideOctagon(x, y, shape);
    } else if (shape.type === 'pentagon') {
        return isPointInsidePentagon(x, y, shape);
    }
    return false;
};

const isPointInsideTriangle = (x, y, shape) => {
    const sideLength = shape.sideLength;
    const height = (Math.sqrt(3) / 2) * sideLength;
    const topY = shape.y - height / 2;
    const bottomY = shape.y + height / 2;
    const leftX = shape.x - sideLength / 2;
    const rightX = shape.x + sideLength / 2;
    const topSlope = (y - topY) / (x - shape.x);
    const bottomSlope = (y - bottomY) / (x - shape.x);
    return x > leftX && x < rightX && y > topY && y < bottomY && topSlope < bottomSlope;
};

const isPointInsideOctagon = (x, y, shape) => {
    const sideLength = shape.sideLength;
    const topY = shape.y - sideLength;
    const bottomY = shape.y + sideLength;
    const leftX = shape.x - sideLength;
    const rightX = shape.x + sideLength;
    const topSlope = (y - topY) / (x - shape.x);
    const bottomSlope = (y - bottomY) / (x - shape.x);
    return x > leftX && x < rightX && y > topY && y < bottomY && topSlope < bottomSlope;
};

const isPointInsidePentagon = (x, y, shape) => {
    const sideLength = shape.sideLength;
    const topY = shape.y - sideLength;
    const bottomY = shape.y + sideLength;
    const leftX = shape.x - sideLength;
    const rightX = shape.x + sideLength;
    const topSlope = (y - topY) / (x - shape.x);
    const bottomSlope = (y - bottomY) / (x - shape.x);
    return x > leftX && x < rightX && y > topY && y < bottomY && topSlope < bottomSlope;
};

canvas.addEventListener('mousedown', (event) => {
    const x = event.clientX;
    const y = event.clientY;
    shapes.forEach((shape) => {
        if (isPointInsideShape(x, y, shape)) {
            draggedShape = shape;
            return;
        }
    });
});

canvas.addEventListener('mousemove', (event) => {
    if (draggedShape) {
        draggedShape.x = event.clientX;
        draggedShape.y = event.clientY;
    }
});

let pulses = [];

canvas.addEventListener('mouseup', (event) => {
    if (draggedShape && draggedShape.type === 'circle') {
        // Add a new pulse for the circle
        pulses.push({ x: draggedShape.x, y: draggedShape.y, radius: 0 });
    }
    draggedShape = null;
});


const setup = () => {};

const update = () => {
    draw();
    pulses = pulses.filter(pulse => pulse.radius < canvas.width / 2);
    requestAnimationFrame(update);
};

setup();
update();