-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas_draw.html
64 lines (56 loc) · 1.44 KB
/
canvas_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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5 Canvas</title>
<link rel="icon" href="https://fav.farm/🔥" />
</head>
<body>
<canvas id="draw" width="800" height="800"></canvas>
<script>
const canvas= document.getElementById('draw');
const ctx= canvas.getContext("2d");
canvas.width= window.innerWidth;
canvas.height= window.innerHeight;
ctx.strokeStyle= "#BADA55";
ctx.lineJoin= 'round';
ctx.lineCap= 'round';
ctx.lineWidth= 1;
let allowDraw= false;
let lastX=0;
let lastY=0;
let hue=0;
function draw(e){
if(!allowDraw) return;
ctx.strokeStyle=`hsl(${hue},90%,50%)`;
ctx.beginPath();
ctx.moveTo(lastX,lastY);
ctx.lineTo(e.offsetX,e.offsetY);
ctx.stroke();
lastX=e.offsetX;
lastY=e.offsetY;
hue+=2;
if(ctx.lineWidth<=10) ctx.lineWidth++;
}
canvas.addEventListener('mousedown',(e)=> {
allowDraw=true;
[lastX,lastY]=[e.offsetX,e.offsetY]
});
function eraseCanvas()
{
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width,canvas.height);
}
canvas.addEventListener('mousemove',draw);
canvas.addEventListener('mouseup',()=> {allowDraw=false;
ctx.lineWidth=1;});
canvas.addEventListener('mouseout',()=> {allowDraw=false;
ctx.lineWidth=1;});
</script>
<style>
html, body {
margin: 0;
}
</style>
</body>
</html>