forked from ReproNim/reproschema-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasInput.vue
More file actions
183 lines (168 loc) · 4.14 KB
/
Copy pathCanvasInput.vue
File metadata and controls
183 lines (168 loc) · 4.14 KB
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
<template>
<div class="canvas-container">
<canvas ref="canvas" :width="width" :height="height" @mousedown="startDrawing" @mousemove="draw" @mouseup="stopDrawing" @mouseleave="stopDrawing"></canvas>
<div class="canvas-controls">
<button @click="clearCanvas" class="btn btn-secondary">Clear</button>
<input type="color" v-model="currentColor" title="Choose color">
<input type="range" v-model="lineWidth" min="1" max="20" title="Line width">
</div>
</div>
</template>
<script>
export default {
name: 'CanvasInput',
props: {
value: {
type: String,
default: ''
},
width: {
type: Number,
default: 800
},
height: {
type: Number,
default: 400
},
backgroundImage: {
type: String,
default: ''
}
},
data() {
return {
canvas: null,
ctx: null,
isDrawing: false,
currentColor: '#000000',
lineWidth: 5,
lastX: 0,
lastY: 0,
image: null
}
},
mounted() {
this.canvas = this.$refs.canvas;
this.ctx = this.canvas.getContext('2d');
this.ctx.lineCap = 'round';
this.ctx.lineJoin = 'round';
// Load background image if provided
if (this.backgroundImage) {
this.image = new Image();
this.image.onload = () => {
// Draw the image maintaining aspect ratio
const scale = Math.min(
this.width / this.image.width,
this.height / this.image.height
);
const x = (this.width - this.image.width * scale) / 2;
const y = (this.height - this.image.height * scale) / 2;
this.ctx.drawImage(
this.image,
x, y,
this.image.width * scale,
this.image.height * scale
);
};
this.image.src = this.backgroundImage;
}
// Load existing drawing if value exists
if (this.value) {
const img = new Image();
img.onload = () => {
this.ctx.drawImage(img, 0, 0);
};
img.src = this.value;
}
},
methods: {
startDrawing(event) {
this.isDrawing = true;
const rect = this.canvas.getBoundingClientRect();
this.lastX = event.clientX - rect.left;
this.lastY = event.clientY - rect.top;
},
draw(event) {
if (!this.isDrawing) return;
const rect = this.canvas.getBoundingClientRect();
const currentX = event.clientX - rect.left;
const currentY = event.clientY - rect.top;
this.ctx.beginPath();
this.ctx.strokeStyle = this.currentColor;
this.ctx.lineWidth = this.lineWidth;
this.ctx.moveTo(this.lastX, this.lastY);
this.ctx.lineTo(currentX, currentY);
this.ctx.stroke();
this.lastX = currentX;
this.lastY = currentY;
this.$emit('input', this.canvas.toDataURL());
},
stopDrawing() {
this.isDrawing = false;
},
clearCanvas() {
this.ctx.clearRect(0, 0, this.width, this.height);
// Redraw background image if it exists
if (this.image) {
const scale = Math.min(
this.width / this.image.width,
this.height / this.image.height
);
const x = (this.width - this.image.width * scale) / 2;
const y = (this.height - this.image.height * scale) / 2;
this.ctx.drawImage(
this.image,
x, y,
this.image.width * scale,
this.image.height * scale
);
}
this.$emit('input', this.canvas.toDataURL());
}
}
}
</script>
<style scoped>
.canvas-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
margin: 1rem 0;
}
canvas {
border: 1px solid #ccc;
background: white;
cursor: crosshair;
}
.canvas-controls {
display: flex;
gap: 1rem;
align-items: center;
}
input[type="color"] {
width: 50px;
height: 30px;
padding: 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="range"] {
width: 100px;
}
.btn {
padding: 0.5rem 1rem;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 0.9rem;
}
.btn-secondary {
background-color: #6c757d;
color: white;
}
.btn-secondary:hover {
background-color: #5a6268;
}
</style>