-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw.html
84 lines (76 loc) · 2.47 KB
/
draw.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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<title>DIY Lovebox - Dessiner</title>
<style>
body {
background-color: #000000;
color: #fff;
font-family: 'Product Sans', sans-serif;
}
</style>
</head>
<center>
<body>
<canvas id="canvas" width="500" height="500" style="border: 1px solid white;"></canvas>
<br>
<button onclick="save()">Enregistrer</button>
<button id="effacer">Effacer</button>
<br></br>
<label for="color">Couleur :</label>
<input type="color" id="color" onchange="changeColor()">
<br></br>
<label for="size">Taille :</label>
<input type="range" id="size" min="1" max="50" value="5" onchange="changeSize()">
<br></br>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var painting = false;
var color = "#fff";
var size = 5;
canvas.addEventListener("mousedown", startPosition);
canvas.addEventListener("mouseup", finishedPosition);
canvas.addEventListener("mousemove", draw);
function startPosition(event) {
painting = true;
draw(event);
}
function finishedPosition() {
painting = false;
ctx.beginPath();
}
function draw(event) {
if (!painting) return;
ctx.strokeStyle = color;
ctx.lineWidth = size;
ctx.lineCap = "round";
ctx.lineTo(event.clientX - canvas.offsetLeft, event.clientY - canvas.offsetTop);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(event.clientX - canvas.offsetLeft, event.clientY - canvas.offsetTop);
}
function save() {
var image = canvas.toDataURL("image/png");
var link = document.createElement("a");
link.download = "image.png";
link.href = image;
link.click();
}
document.getElementById("effacer").addEventListener("click", function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
function changeColor() {
color = document.getElementById('color').value;
}
function changeSize() {
size = document.getElementById('size').value;
}
</script>
</center>
</body>
</html>