Created
September 30, 2016 10:59
-
-
Save rianby64/46f832626cb4ce7052481a104e805a34 to your computer and use it in GitHub Desktop.
An example about how to draw a region by using mouse gestures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<style> | |
.fullwindow { | |
position: absolute; | |
height: 100%; | |
width: 100%; | |
top: 0px; | |
left: 0px; | |
} | |
.select-region { | |
background-color: red; | |
position: absolute; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="fullwindow"></div> | |
<div class="select-region"></div> | |
<script> | |
'use strict'; | |
var mousePressed = false; | |
var start = { clientX: 0, clientY: 0 }; | |
var body = document.querySelector('body'); | |
var selection = document.querySelector('.select-region'); | |
body.addEventListener('mousedown', e => { | |
mousePressed = true; | |
start.clientX = e.clientX; | |
start.clientY = e.clientY; | |
selection.style.width = '0px'; | |
selection.style.height = '0px'; | |
selection.style.top = start.clientY + 'px'; | |
selection.style.left = start.clientX + 'px'; | |
}); | |
body.addEventListener('mousemove', e => { | |
if (mousePressed) { | |
selection.style.width = Math.abs(e.clientX - start.clientX) + 'px'; | |
selection.style.height = Math.abs(e.clientY - start.clientY) + 'px'; | |
if (e.clientX < start.clientX) { | |
selection.style.left = e.clientX + 'px'; | |
} | |
if (e.clientY < start.clientY) { | |
selection.style.top = e.clientY + 'px'; | |
} | |
} | |
}); | |
body.addEventListener('mouseup', e => { | |
mousePressed = false; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment