์ํ ์ ์ฅ
๊ฐ์ฅ ์ต๊ทผ์ ์ ์ฅ๋ ์ํ ๋ณต์
์ค์ฒฉ ๊ฐ๋ฅ!!
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();
resetTransform() ๊ณผ ๊ฐ์
MDN Docs
context.setTransform(1, 0, 0, 1, 0, 0);
// context.resetTransform();
์บ๋ฒ์ค๋ ์ธ์ ๋
์ผ์ชฝ์
๊ฐ ๊ธฐ์ค. CSS ๊ฐ์ ๊ณ ์์ค์ API๊ฐ ์๋.๋ฐ๋ผ์
- ๊ธฐ์ค์ผ๋ก ์ผ์ ์ ๊น์ง ์ด๋์ํจ ํ
- ํ์ ํ๋ ํฌ๊ธฐ ๋ปฅํ๊ธฐํ๋ ํด์ผ ํจ
์ฆ
๋ชจ๋ ๊ธฐ์ค์ ์ ๋ด๊ฐ ์ก์์ค์ผ ํจ
function draw() {
context.strokeRect(300, 300, 100, 100);
context.scale(1.01, 1.01);
requestAnimationFrame(draw);
}
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.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);
}
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์ ์ํด์ค์ผ ์ ํท๊ฐ๋ฆผ
let rotateValue = 0;
function toRadian(d) {
return (d * Math.PI) / 180;
}
function draw() {
...
context.rotate(toRadian(rotateValue));
rotateValue += 1;
...
}
canvas
๊ธฐ์ค์ ์ ๋ฌด์กฐ๊ฑด 0, 0์ด๋ค (CSS๊ณผ ๋ค๋ฅด๋คใ ใ )
์ฆ, ๊ธฐ์ค์ ์ ์ก์์ค์ผ ํ๋ค๋ ๋ฒ๊ฑฐ๋ก์์ด ์๋ค
๊ทธ๋์context.translate(350, 350)
๊ธฐ์ค์ ์ฎ๊ฒจ์คฌ๋ค
- ๐ ๋ฒ๋ฆ์ฒ๋ผ!!! ๊ผญ!!
resetTransform()
๋ก ์ด๊ธฐํ ์์ผ์ฃผ๊ณ transform ํ๊ธฐ (์์๊ณผ ๋ฌ๋ฆฌ ์์ง์ผ ์ผ ์์)
- Transform
context.scale(scaleValue, scaleValue)
context.rotate(toRadian(rotateValue))
- (transform ๋๋ ๋ค์์) ๊ทธ๋ฆฌ๊ธฐ
context.strokeRect(-50, -50, 100, 100)
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);
}