Add very basic mouse tracking via events, needs improvements as events are not reliable
This commit is contained in:
commit
1844315490
18
index.html
Normal file
18
index.html
Normal file
@ -0,0 +1,18 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title></title>
|
||||
<link rel="stylesheet" type="text/css" href="main.css" />
|
||||
<script type="text/javascript" src="main.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<canvas id="drawImage"></canvas>
|
||||
<label> Color: <input type="color" id="drawColor" /></label>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
15
main.css
Normal file
15
main.css
Normal file
@ -0,0 +1,15 @@
|
||||
html, body {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
background-color: #333;
|
||||
}
|
||||
|
||||
#drawImage {
|
||||
position: absolute;
|
||||
width: 900px;
|
||||
height: 450px;
|
||||
margin: 0 auto;
|
||||
top: 25%;
|
||||
left: 25%;
|
||||
background-color: white;
|
||||
}
|
35
main.js
Normal file
35
main.js
Normal file
@ -0,0 +1,35 @@
|
||||
window.onload = () => {
|
||||
|
||||
|
||||
console.log('loaded')
|
||||
|
||||
const drawArea = document.querySelector('#drawImage')
|
||||
const colorPicker = document.querySelector('#drawColor')
|
||||
|
||||
let drawRoutine
|
||||
let drawing = false
|
||||
let x, y
|
||||
|
||||
const drawAreaBounding = drawArea.getBoundingClientRect()
|
||||
const xOffset = drawAreaBounding.x
|
||||
const yOffset = drawAreaBounding.y
|
||||
drawArea.width = drawAreaBounding.width
|
||||
drawArea.height = drawAreaBounding.height
|
||||
const drawCtx = drawArea.getContext('2d')
|
||||
|
||||
console.log(drawArea)
|
||||
drawArea.addEventListener('mousedown', () => {drawing = true})
|
||||
drawArea.addEventListener('mousemove', event => {
|
||||
if (drawing) {
|
||||
x = event.clientX - xOffset
|
||||
y = event.clientY - yOffset
|
||||
console.log(x, y, drawArea.width, drawArea.height)
|
||||
drawCtx.beginPath()
|
||||
drawCtx.arc(x, y, 5, 0, Math.PI * 2)
|
||||
drawCtx.fill()
|
||||
}
|
||||
})
|
||||
|
||||
document.addEventListener('mouseup', () => {drawing = false})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user