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
|
<!DOCTYPE html>
<html>
<head>
<title>canvas path - quadratic curve example</title>
</head>
<body>
<canvas width=500 height=500></canvas>
<script>
function drawSomeCurves() {
var canvas = document.querySelector("canvas");
var ctx = canvas.getContext("2d");
var x = 150;
var y = 150;
canvas.addEventListener("mousedown", function(e) {
x = e.offsetX;
y = e.offsetY;
});
canvas.addEventListener("mousemove", function(e) {
ctx.beginPath();
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 500, 500);
ctx.strokeStyle = 'red';
ctx.lineWidth = 1;
ctx.fillRect(0, 0, 500, 500);
ctx.moveTo(0, 0);
ctx.quadraticCurveTo(e.offsetX, e.offsetY, x, y);
ctx.stroke();
ctx.moveTo(30, 90);
ctx.lineTo(110, 20);
ctx.lineTo(240, 130);
ctx.lineTo(60, 130);
ctx.lineTo(190, 20);
ctx.lineTo(270, 90);
ctx.closePath();
// Fill path
ctx.fillStyle = 'green';
ctx.fill('evenodd');
});
}
drawSomeCurves();
</script>
</body>
</html>
|