์ค๋ ๋ฐฐ์ด ๊ฒ! ์บ๋ฒ์ค์์ ํด๋ฆญํ ํน์ ๊ฐ์ฒด๋ฅผ ๋ฑ ์ง์ด๋ด๋ ๊ฒ!!
clientX
๋ ์บ๋ฒ์ค ์ด๋ฒค๋ ๋ฆฌ์ค๋๋ผ๋ ๋ธ๋ผ์ฐ์ ๊ธฐ์ค,
layerX
๋layer ๊ฐ๋
์ผ๋ก, ์์ ์๋layer
(๋ฐ์ค๋ ๋ญ๋ ๊ฐ์)๋ฅผ ๊ธฐ์ค์ผ๋ก
๊ทผ๋ฐ...!! 0,0 ์ด ์ฐํ์ผ ํ๋๋ฐ ๋ค๋ฅธ ๊ฐ์ด ์ฐํ๋ค? clientX, clientY...?
layerX/layerY๋ Gecko ๊ธฐ๋ฐ ๋ธ๋ผ์ฐ์ (Firefox ๋ฑ)์์ ์ฌ์ฉ๋๋ฉฐ, ๊ฐ์ฅ ๊ฐ๊น์ด ์์น ์ง์ ๋ ์์์ ์๋์ ์ธ ๋ง์ฐ์ค ์์น ๋ํ๋. Chrome๊ณผ Opera์์๋ ์ง์๋์ง๋ง, ๊ณง ์ฌ์ฉ ์ค๋จ(deprecated) ์์ . ์์ผ๋ก ์ฌ์ฉ ์ ํ๋ ๊ฒ ์ข์. StackOverflow
๊ฐ์๊ฐ 4๋ ์ ๊ฐ์๋ผ ๊ทธ๋ด๋งํจ
context.fillRect(200, 200, 100, 100);
function clickHandler(e) {
const x = e.offsetX;
const y = e.offsetY;
if (x > 200 && x < 300 && y > 200 && y < 300) {
console.log('click!');
}
}
canvas.addEventListener('click', clickHandler);
๊ธฐ๋ฅ์ ๋ฐ๋ผ ์ ํ๊ฐ๋ฅ, but ์ฌ๊ธฐ์๋ ๋ฐ๋ก ๊ทธ๋ฆฌ๋๋ก
constructor(x, y) { this.x = x; this.y = y; this.draw(); }
const boxes = [];
class Box {
constructor(x, y) {
this.x = x;
this.y = y;
this.draw();
}
draw() {
context.fillStyle = 'rgba(0, 0, 0, 0.5)';
context.fillRect(this.x, this.y, 100, 100);
}
}
let tempX, tempY;
for (let i = 0; i < 10; i++) {
tempX = Math.random() * canvas.width * 0.8;
tempY = Math.random() * canvas.height * 0.8;
boxes.push(new Box(tempX, tempY));
}
console.log(boxes);
class Box {
constructor(index, x, y) {
this.index = index;
this.x = x;
this.y = y;
this.draw();
}
draw() {
context.fillStyle = 'rgba(0, 0, 0, 0.5)';
context.fillRect(this.x, this.y, 100, 100);
context.fillStyle = 'white';
context.fillText(this.index, this.x + 40, this.y + 55);
}
}
let tempX, tempY;
for (let i = 0; i < 10; i++) {
tempX = Math.random() * canvas.width * 0.8;
tempY = Math.random() * canvas.height * 0.8;
boxes.push(new Box(i, tempX, tempY));
}
const mousePos = { x: 0, y: 0}
class Box {
constructor(index, x, y) {
this.index = index;
this.x = x;
this.width = 100;
this.height = 100;
this.y = y;
this.draw();
}
}
canvas.addEventListener('click', (e) => {
mousePos.x = e.offsetX;
mousePos.y = e.offsetY;
// ๋ฐ์ค 10๊ฐ๋๊น boxes.length ๋์์ผํจ ์ด์ฉ์์์
let box;
for (let i = 0; i < boxes.length; i++) {
box = boxes[i];
if (
mousePos.x > box.x &&
mousePos.x < box.x + box.width &&
mousePos.y > box.y &&
mousePos.y < box.y + box.height
) {
console.log(box.index);
}
}
});
let selectedBox; // ํด๋ฆญ๋ ๋ฐ์ค
let box;
for (let i = 0; i < boxes.length; i++) {
box = boxes[i];
if (
mousePos.x > box.x &&
mousePos.x < box.x + box.width &&
mousePos.y > box.y &&
mousePos.y < box.y + box.height
) {
selectedBox = box; // ๋์ฒด
}
}
console.log(selectedBox.index);
draw()ํจ์๊ฐ requestAniFrame์ ํ๋ฉด์ ๋ฐ๋ณตํด์ ๊ทธ๋ฆฌ๊ณ ์ง์ฐ๊ธฐ๋ฅผ ํ๋ฉด์ => x, y ์ขํ๋ฅผ ๋ฐ๊ฟ์ฃผ๋ ์๋ฆฌ
x++, y++
function render() {
context.clearRect(0, 0, canvas.width, canvas.height);
let box;
for (let i = 0; i < boxes.length; i++) {
box = boxes[i];
box.x += 3;
if (box.x > canvas.width) {
box.x = -box.width;
}
box.draw();
}
requestAnimationFrame(render);
}
render()
class Box {
constructor(index, x, y, speed) {
this.index = index;
this.x = x;
this.speed = speed;
this.width = 100;
this.height = 100;
this.y = y;
this.draw();
}
}
function render() {
context.clearRect(0, 0, canvas.width, canvas.height);
let box;
for (let i = 0; i < boxes.length; i++) {
box = boxes[i];
box.x += box.speed;
if (box.x > canvas.width) {
box.x = -box.width;
}
box.draw();
}
requestAnimationFrame(render);
}
let tempX, tempY, tempSpeed;
for (let i = 0; i < 10; i++) {
tempX = Math.random() * canvas.width * 0.8;
tempY = Math.random() * canvas.height * 0.8;
tempSpeed = Math.random() * 4 + 1; // ์ต์๊ฐ, ์ต๋๊ฐ ์ค์
boxes.push(new Box(i, tempX, tempY, tempSpeed));
}
selectedBox.index ๊ฐ ์๋ ๊ฑธ ํด๋ฆญํ์ ๋, else ์ฒ๋ฆฌ๋ฅผ ์ํด์คฌ๊ธฐ ๋๋ฌธ์ ๋๋ ๋น์ฐํ ์๋ฌ! ์์ธ ์ฒ๋ฆฌ ํด์ฃผ์
๊ธฐ์กด
console.log(selectedBox.index);
๋ณ๊ฒฝ (selectedBox ๊ฐ ์กด์ฌํ ๋์๋ง)
if (selectedBox) {
console.log(selectedBox.index);
}
์ฌ๋ฐฑ์ ํด๋ฆญํด๋ ๋ง์ง๋ง์ผ๋ก ํด๋ฆญํ ๋ฐ์ค index๊ฐ ์ฐํ๋ค.
์ด์ ๋selectedBox = box
๋ผ๊ณ ํ๊ณ clear ์ ์ํด์คฌ๊ธฐ ๋๋ฌธ. ๊ฐ์ ๋น์์ค์ผ์ง
if (selectedBox) {
console.log(selectedBox.index);
selectedBox = null;
}