선택자 및 확대 표시를 위한 헬퍼 함수 생성
스크립트의 주요 기능은 확대 효과를 생성하고 이미지의 필요한 부분을 복사하는 것입니다. 이제 이러한 기능을 정의해 보겠습니다.
- 사각형과 복사 canvas 를 표시하거나 숨기는 함수를 정의합니다.
// main.js
function showSquare() {
square.style.display = "block";
}
function hideSquare() {
square.style.display = "none";
}
function showCanvas() {
copycanvas.style.display = "inline";
}
function hideCanvas() {
copycanvas.style.display = "none";
}
- 확대 효과를 생성합니다.
function createSquare(x, y) {
// Position adjustments to keep the square within canvas boundaries
x = x - 45 < canvas.offsetLeft ? canvas.offsetLeft : x - 45;
y = y - 45 < canvas.offsetTop ? canvas.offsetTop : y - 45;
x = x + 90 < box.right ? x : box.right - 90;
y = y + 90 < box.bottom ? y : box.bottom - 90;
squaredata.left = x; // Update global state
squaredata.top = y;
moveSquare(x, y); // Position the square
copy(); // Invoke the magnifying effect
}
- 사각형의 위치를 지정하고 확대를 호출합니다.
// Function to move the square selector and the magnified display
function moveSquare(x, y) {
square.style.left = x + "px";
square.style.top = y + "px";
showCanvas();
showSquare();
}
- 복사 canvas 에 확대 효과를 생성합니다.
function copy() {
copycontext.drawImage(
canvas, // Specify the source canvas
squaredata.left - box.left, // Position to start copying from
squaredata.top - box.top,
90, // Width and height of the data to copy
90,
0, // Destination position on the copy canvas
0,
copycanvas.width,
copycanvas.height
);
}