-
Notifications
You must be signed in to change notification settings - Fork 314
/
canva.js
195 lines (169 loc) · 6.09 KB
/
canva.js
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
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const toolButtons = document.querySelectorAll('.toolbar button');
const clearButton = document.getElementById('clear');
const sizeSlider = document.getElementById('size-slider');
const fontSizeInput = document.getElementById('font-size');
const fontStyleSelect = document.getElementById('font-style');
const textColorInput = document.getElementById('text-color');
const textOptionsContainer = document.querySelector('.text-options');
class DrawingApp {
constructor(canvas, ctx) {
this.canvas = canvas;
this.ctx = ctx;
this.canvas.width = 1000;
this.canvas.height = 600;
// State management
this.state = {
tool: 'pencil',
drawing: false,
isDraggingText: false,
selectedText: null,
startX: 0,
startY: 0,
size: 5,
text: {
fontSize: 20,
fontStyle: 'normal',
color: '#000000',
items: []
}
};
this.bindEvents();
}
// Centralized event binding
bindEvents() {
this.canvas.addEventListener('mousedown', this.handleMouseDown.bind(this));
this.canvas.addEventListener('mouseup', this.handleMouseUp.bind(this));
this.canvas.addEventListener('mousemove', this.handleMouseMove.bind(this));
// Tool buttons
toolButtons.forEach(button => {
button.addEventListener('click', () => this.changeTool(button.id));
});
// Text options
fontSizeInput.addEventListener('input', (e) => this.state.text.fontSize = parseInt(e.target.value, 10));
fontStyleSelect.addEventListener('change', (e) => this.state.text.fontStyle = e.target.value);
textColorInput.addEventListener('input', (e) => this.state.text.color = e.target.value);
// Size slider
sizeSlider.addEventListener('input', (e) => this.state.size = parseInt(e.target.value, 10));
// Clear button
clearButton.addEventListener('click', () => this.clearCanvas());
}
// Change tool and update UI
changeTool(newTool) {
this.state.tool = newTool;
textOptionsContainer.style.display = (newTool === 'text') ? 'flex' : 'none';
this.ctx.beginPath();
}
// Mouse down handler
handleMouseDown(e) {
const { offsetX: x, offsetY: y } = e;
this.state.startX = x;
this.state.startY = y;
if (this.state.tool === 'text') {
const clickedText = this.findTextAtPosition(x, y);
if (clickedText) {
this.state.selectedText = clickedText;
this.state.isDraggingText = true;
} else {
const text = prompt("Enter your text:");
if (text) {
this.addTextItem(text, x, y);
}
}
} else if (['line', 'circle'].includes(this.state.tool)) {
this.state.drawing = true;
} else {
this.state.drawing = true;
this.ctx.beginPath();
this.ctx.moveTo(x, y);
}
}
// Mouse move handler
handleMouseMove(e) {
if (!this.state.drawing) return;
const { offsetX: x, offsetY: y } = e;
const { tool, size, isDraggingText, selectedText } = this.state;
if (['pencil', 'marker', 'eraser'].includes(tool)) {
this.ctx.lineWidth = size;
this.ctx.strokeStyle = tool === 'eraser' ? '#f9f9f9' : 'black';
this.ctx.lineCap = 'round';
this.ctx.lineTo(x, y);
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo(x, y);
}
if (isDraggingText && selectedText) {
selectedText.x = x;
selectedText.y = y;
this.redrawCanvas();
}
}
// Mouse up handler
handleMouseUp(e) {
const { drawing, tool, startX, startY } = this.state;
const { offsetX: x, offsetY: y } = e;
if (drawing && tool === 'line') {
this.drawLine(startX, startY, x, y);
} else if (drawing && tool === 'circle') {
const radius = Math.hypot(x - startX, y - startY);
this.drawCircle(startX, startY, radius);
}
this.state.drawing = false;
this.state.isDraggingText = false;
this.state.selectedText = null;
this.ctx.beginPath();
}
// Add text item
addTextItem(text, x, y) {
const textObj = {
text,
x,
y,
fontSize: this.state.text.fontSize,
fontStyle: this.state.text.fontStyle,
color: this.state.text.color
};
this.state.text.items.push(textObj);
this.drawTextItem(textObj);
}
// Drawing methods
drawTextItem({ text, x, y, fontSize, fontStyle, color }) {
this.ctx.font = `${fontStyle} ${fontSize}px Arial`;
this.ctx.fillStyle = color;
this.ctx.fillText(text, x, y);
}
drawLine(x1, y1, x2, y2) {
this.ctx.lineWidth = this.state.size;
this.ctx.strokeStyle = 'black';
this.ctx.beginPath();
this.ctx.moveTo(x1, y1);
this.ctx.lineTo(x2, y2);
this.ctx.stroke();
}
drawCircle(x, y, radius) {
this.ctx.lineWidth = this.state.size;
this.ctx.strokeStyle = 'black';
this.ctx.beginPath();
this.ctx.arc(x, y, radius, 0, Math.PI * 2);
this.ctx.stroke();
}
// Find if text is clicked
findTextAtPosition(x, y) {
return this.state.text.items.find(
item => Math.abs(item.x - x) < 20 && Math.abs(item.y - y) < 20
);
}
// Redraw canvas
redrawCanvas() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.state.text.items.forEach(item => this.drawTextItem(item));
}
// Clear canvas
clearCanvas() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.state.text.items = [];
}
}
// Initialize the drawing app
const drawingApp = new DrawingApp(canvas, ctx);