-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawing.html
66 lines (56 loc) · 1.82 KB
/
drawing.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mouse Events</title>
<style>
#canvas {
border: 1px black solid;
}
</style>
</head>
<body>
<lable> Color </lable><input type="color" id="colorvalue"></input><button id="color">Submit</button><br>
<lable> Range </lable><input type="range" id="rangevalue"></input><button id="range">Submit</button>
<P>Click the mouse on the canvas</P>
<canvas id="canvas" height="400" width="400"></canvas>
<script>
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var colorbutton = document.getElementById("color")
var rangebutton = document.getElementById("range")
var color = "#000000"
var range = 50
colorbutton.addEventListener('click',function(){
color = document.getElementById("colorvalue").value
console.log(color)
})
rangebutton.addEventListener('click',function(){
range = document.getElementById("rangevalue").value
console.log(range)
})
var mousedown = false
canvas.addEventListener("mousedown", function() {
mousedown = true
});
canvas.addEventListener("mouseup", function() {
mousedown = false
})
canvas.addEventListener("mousemove", function() {
if (!mousedown) {
return; }
// event is a built-in variable, contains the event that triggered this function
// get the x, y location of the event
var x = event.offsetX;
var y = event.offsetY; //offsetX, offsetY, may not work in older browsers
//Draw a filled rectangle centered under the mouse click
//x, y is the top left corner, so shift up and left by 5 to center it under the mouse.
context.fillStyle = color;
context.beginPath();
context.arc(x-5, y-5, range, 0, 2*Math.PI);
context.stroke();
context.fill()
})
</script>
</body>
</html>