-
Notifications
You must be signed in to change notification settings - Fork 0
/
painter.html
128 lines (118 loc) · 4.02 KB
/
painter.html
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
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Painter</title>
<link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Ubuntu', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
canvas {
border: 1px solid #ccc;
cursor: crosshair;
}
.controls {
margin: 20px;
}
.controls button {
margin: 5px;
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: #007BFF;
color: white;
cursor: pointer;
font-size: 16px;
}
.controls button:hover {
background-color: #0056b3;
}
.color-picker {
display: flex;
align-items: center;
margin: 20px;
}
.color-picker input[type="color"] {
margin-left: 10px;
width: 40px;
height: 40px;
border: none;
padding: 0;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Paint online</h1>
<div class="controls">
<button onclick="changeColor('black')">Schwarz</button>
<button onclick="changeColor('red')">Rot</button>
<button onclick="changeColor('green')">Grün</button>
<button onclick="changeColor('blue')">Blau</button>
<button onclick="changeColor('yellow')">Gelb</button>
<button onclick="changeColor('purple')">Lila</button>
<button onclick="changeColor('orange')">Orange</button>
<button onclick="changeColor('brown')">Braun</button>
<button onclick="clearCanvas()">Leeren</button>
<button onclick="saveCanvas()">Speichern</button>
</div>
<div class="color-picker">
<label for="colorPicker">Wählen Sie eine Farbe:</label>
<input type="color" id="colorPicker" onchange="changeColor(this.value)">
</div>
<canvas id="painterCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('painterCanvas');
const ctx = canvas.getContext('2d');
let painting = false;
let color = 'black';
function startPosition(e) {
painting = true;
draw(e);
}
function endPosition() {
painting = false;
ctx.beginPath();
}
function draw(e) {
if (!painting) return;
ctx.lineWidth = 5;
ctx.lineCap = 'round';
ctx.strokeStyle = color;
ctx.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
}
function changeColor(newColor) {
color = newColor;
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function saveCanvas() {
const link = document.createElement('a');
link.download = 'zeichnung.png';
link.href = canvas.toDataURL();
link.click();
}
window.addEventListener('beforeunload', function (e) {
// Wenn es ungespeicherte Änderungen gibt, zeige eine Warnung
e.preventDefault();
e.returnValue = '';
});
canvas.addEventListener('mousedown', startPosition);
canvas.addEventListener('mouseup', endPosition);
canvas.addEventListener('mousemove', draw);
</script>
</body>
</html>