-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathocscript.js
105 lines (92 loc) · 3.71 KB
/
ocscript.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
const selectedItems = [];
function selectItem(item) {
selectedItems.push(item);
updateSelectedItems();
}
function updateSelectedItems() {
var container = document.getElementById('selected-items');
container.forEach(item => {
var img = document.createElement('img');
img.src="pics/dress.jpg";
container.appendChild(img);
});
}
const canvas = document.querySelector("#canvas"),
ctx = canvas.getContext("2d");
let isDrawing = false,
color = "green",
brushSize = 5;
const colorBoxes = Array.from(document.getElementsByClassName("clr")),
favColorInput = document.getElementById("favcolor"),
bgColorInput = document.getElementById("bgcolor"),
brushSizeInput = document.getElementById("brushSize"),
clearButton = document.getElementById("clear"),
saveButton = document.getElementById("save");
// Set up color boxes
colorBoxes.forEach((box) => {
box.addEventListener("click", () => {
colorBoxes.forEach((sibling) => sibling.classList.remove("active"));
color = box.getAttribute("data-clr");
box.classList.add("active");
});
});
// Handle favorite color input
favColorInput.addEventListener("input", () => {
color = favColorInput.value;
});
// Handle background color input
bgColorInput.addEventListener("input", () => {
canvas.style.backgroundColor = bgColorInput.value;
});
// Handle brush size input
brushSizeInput.addEventListener("input", () => {
brushSize = brushSizeInput.value;
});
// Adjust canvas size on load
window.addEventListener("load", () => {
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
});
// Drawing functions
const startDrawing = (e) => {
isDrawing = true;
ctx.beginPath();
ctx.moveTo(e.offsetX || e.touches[0].clientX - canvas.offsetLeft, e.offsetY || e.touches[0].clientY - canvas.offsetTop);
};
const drawing = (e) => {
if (!isDrawing) return;
ctx.strokeStyle = color;
ctx.lineWidth = brushSize;
ctx.lineTo(e.offsetX || e.touches[0].clientX - canvas.offsetLeft, e.offsetY || e.touches[0].clientY - canvas.offsetTop);
ctx.stroke();
};
const stopDrawing = () => {
isDrawing = false;
ctx.closePath();
};
// Event listeners for drawing
canvas.addEventListener("mousedown", startDrawing);
canvas.addEventListener("mousemove", drawing);
canvas.addEventListener("mouseup", stopDrawing);
canvas.addEventListener("mouseout", stopDrawing);
canvas.addEventListener("touchstart", (e) => {
e.preventDefault();
startDrawing(e);
});
canvas.addEventListener("touchmove", (e) => {
e.preventDefault();
drawing(e);
});
canvas.addEventListener("touchend", stopDrawing);
canvas.addEventListener("touchcancel", stopDrawing);
// Clear canvas
clearButton.addEventListener("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
// Save canvas
saveButton.addEventListener("click", () => {
const link = document.createElement('a');
link.download = 'canvas.png';
link.href = canvas.toDataURL('image/png');
link.click();
});