๐Ÿช€ ํ† ์ด ํ”„๋กœ์ ํŠธ/๐Ÿ–ผ๏ธ

[4] ์ด๋ฏธ์ง€ ์ €์žฅํ•˜๋Š” ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

๋˜์ž๐Ÿ‹ 2024. 9. 5. 17:07
728x90

 <button id="save_button">์ €์žฅํ•˜๊ธฐ</button>

 

์ด๋ ‡๊ฒŒ ๋ฒ„ํŠผ ์š”์†Œ๋ฅผ ๋งŒ๋“ค์–ด์ค€ ํ›„, 

JS ๋กœ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ์˜ ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

const onSaveButtonClick = () => {
  const image = canvas.toDataURL('image/jpeg');
  const link = document.createElement('a');
  link.href = image;
  link.download = 'ProfileByDDOZZA';
  link.click();
  alert('save!');
};

 

canva.toDataUrl() 

: ์ด๋ฏธ์ง€ ํ‘œํ˜„์„ ํฌํ•จํ•˜๋Š” ๋ฐ์ดํ„ฐ URL์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํŒŒ์ผ ํ˜•์‹์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด PNG ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด์ง„๋‹ค. ๋งŽ์€ ๊ฒฝ์šฐ์— image/jpeg ํ˜น์€ image/webp ํ˜•์‹๋„ ์ง€์›๋œ๋‹ค. ์›ํ•˜๋Š” ํƒ€์ž…์ด ์žˆ๋‹ค๋ฉด ๊ด„ํ˜ธ ์•ˆ์— ์จ์ฃผ๋ฉด ๋œ๋‹ค. 

<a>.href ์— ์ด๋ฏธ์ง€ ์ƒ์„ฑํ•ด์ค€ ์ด๋ฏธ์ง€ url ์„ ๋„ฃ์–ด์ฃผ๊ณ , 

<a>.download ๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ URL ์„ ์ €์žฅํ•  ์ง€ ๋ฌผ์–ด๋ณธ๋‹ค. ๊ฐ’์„ ์ง€์ •ํ•˜๋ฉด ํŒŒ์ผ ๋ช…์ด ๋œ๋‹ค. 

์ด๋ฏธ์ง€๊ฐ€ ์ €์žฅ๋œ ๋ชจ์–‘

link ์— ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. 

 

์ €์žฅ ์•Œ๋žŒ์„ ๋„ฃ์–ด์คฌ๋Š”๋ฐ, ํ™•์ธ์„ ํด๋ฆญํ•ด์•ผํ•ด์„œ 0.5. ์ดˆ ๋’ค์— ์‚ฌ๋ผ์ง€๊ฒŒ ๋งŒ๋“ค์–ด์•ผ๊ฒ ๋‹ค. 

 

 

์ž˜ ์ €์žฅ๋œ๋‹ค!

 

 

 

 

https://developer.mozilla.org/ko/docs/Web/HTML/Element/a#download_%ED%8A%B9%EC%84%B1%EC%9C%BC%EB%A1%9C_canvas%EB%A5%BC_png%EB%A1%9C_%EC%A0%80%EC%9E%A5%ED%95%98%EA%B8%B0

 

<a> - HTML: Hypertext Markup Language | MDN

HTML <a> ์š”์†Œ(์•ต์ปค ์š”์†Œ)๋Š” href ํŠน์„ฑ์„ ํ†ตํ•ด ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋‚˜ ๊ฐ™์€ ํŽ˜์ด์ง€์˜ ์–ด๋Š ์œ„์น˜, ํŒŒ์ผ, ์ด๋ฉ”์ผ ์ฃผ์†Œ์™€ ๊ทธ ์™ธ ๋‹ค๋ฅธ URL๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜์ดํผ๋งํฌ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. <a> ์•ˆ์˜ ์ฝ˜ํ…์ธ ๋Š” ๋งํฌ ๋ชฉ์ ์ง€

developer.mozilla.org

https://developer.mozilla.org/ko/docs/Web/API/HTMLElement/click

 

HTMLElement.click() - Web API | MDN

HTMLElement.click() ๋ฉ”์†Œ๋Š” ์—˜๋ฆฌ๋จผํŠธ์— ๋งˆ์šฐ์Šค ํด๋ฆญ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•