I have a code that pastes an image over another and increases the size of the image on up arrow key press and vice versa on down arrow key press.
Here is my code:
<!DOCTYPE html>
<html>
<body>
<img id="imgdisplay" width=960 height=540 src="../static/black_background.png" onclick="clickEvent(event);">
<img id="circle" src="../Different Sizes/red_circle_80.png" style="display:none;" onclick="clickEvent(event);">
<p id='X'>0</p>
<p id='Y'>0</p>
<script>
var size = 80;
</script>
<script>
var x = 0;
var y = 0;
function clickEvent(event) {
var thecircle = document.getElementById('circle');
x = event.clientX
y = event.clientY
document.getElementById('X').innerHTML = '' x;
document.getElementById('Y').innerHTML = '' y;
var pastex = x - (circle.width / 2);
var pastey = y - (circle.height / 2);
thecircle.style = `position: absolute; top: ${pastey}px; left: ${pastex}px; display: block; overflow: hidden;`;
}
</script>
<script>
document.onkeydown = function(event) {
var thecircle = document.getElementById('circle');
event = event || window.event;
if (event.keyCode == 38) {
size = 100;
thecircle.src = `../Different Sizes/red_circle_${size}.png`;
clickEvent({'clientX': x, 'clientY' : y})
} else if (event.keyCode == 40) {
size -= 100;
thecircle.src = `../Different Sizes/red_circle_${size}.png`;
clickEvent({'clientX': x, 'clientY' : y})
}
};
</script>
</body>
</html>
The problem is that if I click just on the border of the background image, the circle will overflow and half of the circle will stick out of the background. It sticks out much more when I click on the border and I click the up arrow on the keyboard multiple times to enlarge the circle.
How would I limit that the circle would not stick out of the background and not let the user to enlarge the circle if it's already almost sticking out of the background?
What I mean in images are.
What I currently have and it's not what I want:
What I want (to limit it to not stick out of the black background image):
How would I do this?
Any help appreciated!
CodePudding user response:
Here's the solution I was talking about. The one drawback is this doesn't account for scrolling, but that should be a trivial fix if that is an issue for your case.
var size = 80;
var x = 0;
var y = 0;
function clickEvent(event) {
var thecircle = document.getElementById('circle');
x = event.clientX
y = event.clientY
var box = document.getElementById('imgdisplay');
if (x > box.x box.width - circle.width / 2){
x = box.x box.width - circle.width / 2;
}
if (y > box.y box.height - circle.width / 2){
y = box.y box.height - circle.width / 2;
}
if (x < box.x circle.width/2)
x = box.x circle.width/2;
if (y < box.y circle.width/2)
y = box.y circle.width/2;
document.getElementById('X').innerHTML = '' x;
document.getElementById('Y').innerHTML = '' y;
var pastex = x - (circle.width / 2);
var pastey = y - (circle.height / 2);
thecircle.style = `position: absolute; top: ${pastey}px; left: ${pastex}px; display: block; overflow: hidden;`;
}
#imgdisplay{background:black}
#circle{width:80px;height:80px;background:red;border-radius:40px;}
<img id="imgdisplay" width=500 height=300 src="../static/black_background.png" onclick="clickEvent(event);">
<img id="circle" src="../Different Sizes/red_circle_80.png" style="display:none;" onclick="clickEvent(event);">
<p id='X'>0</p>
<p id='Y'>0</p>
CodePudding user response:
your problem was that the circle had position absolute and not position relative. Take a look at my example, it works.
<!DOCTYPE html>
<html>
<head>
<style>
.container{
z-index: 90;
overflow: hidden;
width:450px;
height: 540px;
background-image: url("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgICAgICAgHBwcHBwoHBwcHBw8ICQgKFREWFhURExMYKDQgGBolJxMfITEhJSkrLjouFx8zODMsNygtLisBCgoKBQUFDgUFDisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIAKgBLAMBIgACEQEDEQH/xAAXAAEBAQEAAAAAAAAAAAAAAAAAAQIH/8QAHxABAQACAQQDAAAAAAAAAAAAAAERITECQVGhYYGR/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AOGigIqAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALMZ3mT4maIAAAoACKgAoCCoANdOO6AgoCAAAACoAAAAACggAAAAAAAAAAAAAKEAEUAAAAAKFBAAAAAAVAAAAVFBAAAUEBQQAAAAAAAAAFAAAAAAAARUBUAFBAFEAAABQBAAAAAAAAAAAAAAAAAFWTN8IAAAAAAAgoCCoAKgAACoAAAAAAAAAAAAAAAAAAAAAoAAAAAAACKgAoAX9AEFAQAAAAVAAAXW/SAAAAAAAAAApLi5hdggAKAAAAAAgoIACgAgAAAAAAAAtxrGeN58oAAACggAAAAAAAKgAAAqLUAABRAAAAXPpAVAAAAAAAAAAVAAAAAAAAAAAAAAABUAAAAAAABbcoAAAqAAAAKCAoIAAKgAAC7Rrp6rJZO/IMgAAAAAAAAAAAAAAAAACoAAAAAAAAAAAAAAAAAAAAAAAC0EVFnPn4vcEFvP2gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKgAAA/9k=");
}
#circle{
display: inline-block;
overflow: hidden !important;
display: none;
max-width: 100%;
max-height:100%;
z-index: 1;
}
</style>
</head>
<body>
<div onclick="clickEvent(event);">
<!-- <img id="imgdisplay" width=960px height=540px src="" onclick="clickEvent(event);"> -->
<img id="circle" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAilBMVEX////u7u7t7e0AAAD39/f5 fn09PT /v7v7 /y8vL7 /u/v7/j4 PPz8/CwsJhYWEqKipaWlrb29s4ODhzc3NNTU0lJSWzs7NCQkKLi4uampp6enpoaGgPDw9YWFjm5uYdHR2ioqKCgoIxMTGpqalQUFAXFxcgICDLy8s Pj63t7eTk5OBgYFGRkaUF5DaAAAQ6klEQVR4nO2dCXOqOhTHQ0xYArjg7rW1Lu21r/X7f70HmnMSEBA0UujczLw3c1P4k59ZzslOCATbksHDKFfGUAYxDoUoAVEco1opZRFzWv8I/xF2nTB iibh1xKKOJwF1Ac5S56j3SWM88tN3nNikk04X 4mk8FgMBrF/1uvJ7vVvL xPCYIs51bUi0kpNRyeZzwyD sJotecVhMVgd/0xQhlUGvPDJoWhA0LQj4HmMOC6LldPhSAqfC2366jYJYUFxLGUiVDYFxCBiFMew6qvC95Lv cvRRCU6Fl/VyzpL6Wq5eP1UEMpNywBeY51hBmIyiLv6SHkTZGJX8rEJEq1lNOBWG8/CSC9RcquoRWpoWvKi0PIdFf6bVSmZhVr4vk/JKqbFUmSQMt OH6CCMtxERl9x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='X'>0</p>
<p id='Y'>0</p>
<script>
var size = 80;
</script>
<script>
var x = 0;
var y = 0;
function clickEvent(event) {
var thecircle = document.getElementById('circle');
x = event.clientX
y = event.clientY
document.getElementById('X').innerHTML = '' x;
document.getElementById('Y').innerHTML = '' y;
var pastex = x - (circle.width / 2);
var pastey = y - (circle.height / 2);
thecircle.style = `position: relative; top: ${pastey}px; left: ${pastex}px; display: block; overflow: hidden;`;
}
</script>
<script>
document.onkeydown = function(event) {
var thecircle = document.getElementById('circle');
event = event || window.event;
if (event.keyCode == 38) {
size = 100;
thecircle.src = `../Different Sizes/red_circle_${size}.png`;
clickEvent({'clientX': x, 'clientY' : y})
} else if (event.keyCode == 40) {
size -= 100;
thecircle.src = `../Different Sizes/red_circle_${size}.png`;
clickEvent({'clientX': x, 'clientY' : y})
}
};
</script>
</body>
</html>