[Canvas] Transform

Chenยท2024๋…„ 6์›” 7์ผ
0

Canvas

๋ชฉ๋ก ๋ณด๊ธฐ
7/11
post-thumbnail

save ์™€ restore

save()

์ƒํƒœ ์ €์žฅ

restore()

๊ฐ€์žฅ ์ตœ๊ทผ์— ์ €์žฅ๋œ ์ƒํƒœ ๋ณต์›

์ค‘์ฒฉ ๊ฐ€๋Šฅ!!

  • ์˜ˆ๋ฅผ ๋“ค์–ด, restore() ๋‘ ๋ฒˆ ํ•˜๋ฉด ๋งˆ์ง€๋ง‰ - 1๋กœ ์ €์žฅํ•œ ์ƒํƒœ ๋ณต์›
context.fillRect(100, 100, 200, 200);
context.fillStyle = 'orange'; // ๋ฌผ๊ฐ์ฐ๊ธฐ
context.fillRect(150, 150, 200, 200);

context.save();

context.fillStyle = 'blue';
context.beginPath();
context.arc(300, 300, 50, 0, Math.PI * 2, false);
context.fill();

context.restore();

// ์™œ ์˜ค๋ Œ์ง€์ƒ‰์ด ๋ ๊นŒ?
context.beginPath();
context.arc(300, 300, 20, 0, Math.PI * 2, false);
context.fill();


setTransform() <-- ํ–‰๋ ฌ๋“ฑ์žฅ

resetTransform() ๊ณผ ๊ฐ™์Œ
MDN Docs

  • ๋””ํดํŠธ๋กœ transform ์ „์— ์“ฐ๋Š” ๊ฒƒ
context.setTransform(1, 0, 0, 1, 0, 0);
// context.resetTransform();
  • ๋‚˜์ค‘์— ๋ณต์žกํ•œ transformํ•  ๋•Œ ํ–‰๋ ฌ ํ•„์š”..์ค‘์š”...
  • ๋ณ€ํ™˜(transform)์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค๋Š” ๊ฒƒ์˜ ์˜๋ฏธ = ๋‹จ์œ„ํ–‰๋ ฌ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ!!!!

scale

์บ”๋ฒ„์Šค๋Š” ์–ธ์ œ๋‚˜ ์™ผ์ชฝ์œ„๊ฐ€ ๊ธฐ์ค€. CSS ๊ฐ™์€ ๊ณ ์ˆ˜์ค€์˜ API๊ฐ€ ์•„๋‹˜.

๋”ฐ๋ผ์„œ

  1. ๊ธฐ์ค€์œผ๋กœ ์‚ผ์„ ์ ๊นŒ์ง€ ์ด๋™์‹œํ‚จ ํ›„
  2. ํšŒ์ „ํ•˜๋“  ํฌ๊ธฐ ๋ปฅํŠ€๊ธฐํ•˜๋“  ํ•ด์•ผ ํ•จ

์ฆ‰

๋ชจ๋“  ๊ธฐ์ค€์ ์„ ๋‚ด๊ฐ€ ์žก์•„์ค˜์•ผ ํ•จ

function draw() {
    context.strokeRect(300, 300, 100, 100);

    context.scale(1.01, 1.01);

    requestAnimationFrame(draw);
}

draw();


scale

function draw() {
    // context.clearRect(0, 0, canvas.width, canvas.height);
    context.save();
    context.setTransform(1, 0, 0, 1, 0, 0);
    context.translate(350, 350);
    context.scale(scaleValue, scaleValue);
    scaleValue += 0.1;
    context.strokeRect(-50, -50, 100, 100);
    context.restore();

    requestAnimationFrame(draw);
}


์• ๋‹ˆ๋ฉ”์ด์…˜์ฒ˜๋Ÿผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด?

๋งค loop ๋งˆ๋‹ค ์ง€์›Œ์ฃผ๋ฉด ๋œ๋‹ค!!

function draw() {
    context.clearRect(0, 0, canvas.width,  canvas.height); // ์ถ”๊ฐ€
    context.save();
    context.setTransform(1, 0, 0, 1, 0, 0);
    context.translate(350, 350);
    context.scale(scaleValue, scaleValue);
    scaleValue += 0.1;
    context.strokeRect(-50, -50, 100, 100);
    context.restore();

    requestAnimationFrame(draw);
}

save, restore์˜ ์—ญํ• ์„ ํ™•์ธํ•ด๋ณด์ž

function draw() {
    context.clearRect(0, 0, canvas.width,  canvas.height); 
    // context.save();
    context.setTransform(1, 0, 0, 1, 0, 0);
    context.translate(350, 350);
    context.scale(scaleValue, scaleValue);
    scaleValue += 0.1;
    context.strokeRect(-50, -50, 100, 100);
    // context.restore();

    requestAnimationFrame(draw);
}


๋ฏธ๋‹ˆ ์‚ฌ๊ฐํ˜•์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž

function draw() {
    context.clearRect(0, 0, canvas.width, canvas.height);
    context.save();
    context.setTransform(1, 0, 0, 1, 0, 0);
    context.translate(350, 350);
    context.scale(scaleValue, scaleValue);
    scaleValue += 0.01;
 	context.strokeRect(-50, -50, 100, 100);
    context.restore();

    context.fillRect(10, 10, 30, 30);

    requestAnimationFrame(draw);
}

save, restore์„ ์ฃผ์„์ฒ˜๋ฆฌํ•ด๋ณด์ž

์ฆ‰..save, restore์„ ์ž˜ํ•ด์ค˜์•ผ ์•ˆ ํ—ท๊ฐˆ๋ฆผ


rotate

let rotateValue = 0;

function toRadian(d) {
                return (d * Math.PI) / 180;
}

function draw() {
...
    context.rotate(toRadian(rotateValue));
    rotateValue += 1;
...
}


clearRect ๋ฅผ ์•ˆํ•˜๋ฉด ๊ฐ€๋” ๋ฉ‹์ง„ ํšจ๊ณผ


์ด์ •๋ฆฌ

  1. canvas ๊ธฐ์ค€์ ์€ ๋ฌด์กฐ๊ฑด 0, 0์ด๋‹ค (CSS๊ณผ ๋‹ค๋ฅด๋‹คใ… ใ… )
    ์ฆ‰, ๊ธฐ์ค€์ ์„ ์žก์•„์ค˜์•ผ ํ•œ๋‹ค๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๋‹ค
    ๊ทธ๋ž˜์„œ context.translate(350, 350) ๊ธฐ์ค€์  ์˜ฎ๊ฒจ์คฌ๋‹ค

  2. ๐Ÿ“Œ ๋ฒ„๋ฆ‡์ฒ˜๋Ÿผ!!! ๊ผญ!! resetTransform() ๋กœ ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ฃผ๊ณ  transform ํ•˜๊ธฐ (์˜ˆ์ƒ๊ณผ ๋‹ฌ๋ฆฌ ์›€์ง์ผ ์ผ ์—†์Œ)

  3. Transform context.scale(scaleValue, scaleValue)
    context.rotate(toRadian(rotateValue))

  4. (transform ๋๋‚œ ๋‹ค์Œ์—) ๊ทธ๋ฆฌ๊ธฐ context.strokeRect(-50, -50, 100, 100)

  5. context.restore()

์ „์ฒด ์ฝ”๋“œ

const canvas = document.querySelector('.canvas');
const context = canvas.getContext('2d');
let scaleValue = 1;
let rotateValue = 0;

function toRadian(d) {
    return (d * Math.PI) / 180;
}

function draw() {
    // context.clearRect(0, 0, canvas.width, canvas.height);
    context.save();
    context.setTransform(1, 0, 0, 1, 0, 0);
    // context.resetTransform();
    context.translate(350, 350);
    context.scale(scaleValue, scaleValue);
    context.rotate(toRadian(rotateValue));

    context.strokeRect(-50, -50, 100, 100);
    context.restore();

    scaleValue += 0.05;
    rotateValue += 1;

    context.fillRect(10, 10, 30, 30);

    requestAnimationFrame(draw);
}
profile
ํ˜„์‹ค์ ์ธ ๋ชฝ์ƒ๊ฐ€

0๊ฐœ์˜ ๋Œ“๊ธ€