first try:show canvas selected area
<div class="info">
<p class="points-info">No points yet</p>
<input type="text" placeholder="Image URL">
</div>
<div class="output">
<canvas id="canvas" width="500" height="500" style="border: 1px solid black;"></canvas>
<p id="coordinates"></p>
</div>
<style>
canvas {
width: 500px;
height: 500px;
display: inline-block;
border: 3px solid #333;
background-size: cover;
cursor: crosshair;
}
.zoom {
width: 100px;
height: 100px;
border: 3px solid #333;
background-size: fill;
border-radius: 50px;
cursor: crosshair;
}
.magnify {
width: 100px;
height: 100px;
background: black;
background-size: 400%;
border: 2px solid #333;
}
.magnify-dot {
}
</style>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var points = [];
var selectedPointIndex = null;
canvas.addEventListener('mousedown', function(event) {
var x = event.offsetX;
var y = event.offsetY;
// Check if a point is clicked
points.forEach(function(point, index) {
if (Math.abs(x - point.x) < 5 && Math.abs(y - point.y) < 5) {
selectedPointIndex = index;
}
});
// If no point is clicked, create a new point
if (selectedPointIndex === null) {
points.push({x: x, y: y});
draw();
}
});
canvas.addEventListener('mousemove', function(event) {
if (selectedPointIndex === null) return;
points[selectedPointIndex].x = event.offsetX;
points[selectedPointIndex].y = event.offsetY;
draw();
});
canvas.addEventListener('mouseup', function(event) {
selectedPointIndex = null;
printCoordinates();
});
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
points.forEach(function(point, index) {
ctx.arc(point.x, point.y, 5, 0, 2*Math.PI);
if (index === 0) {
ctx.moveTo(point.x, point.y);
} else {
ctx.lineTo(point.x, point.y);
}
});
ctx.closePath();
ctx.stroke();
}
function printCoordinates() {
var coordinatesElement = document.getElementById('coordinates');
var coordinatesString = points.map(function(point) {
return '(' + point.x + ', ' + point.y + ')';
}).join(', ');
coordinatesElement.textContent = coordinatesString;
}
</script>
Comments
Post a Comment