โŒ› HTML CSS Js SVG ์‹œ๊ณ„ ๐Ÿ•

3421 ๋‹จ์–ด
html, css, svg ์ด๋ฏธ์ง€ ๋ฐ javascript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ์‹œ๊ณ„๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๋ช‡ ๊ฐ€์ง€ ํ•ต์‹ฌ ์‚ฌํ•ญ๋งŒ ์—ผ๋‘์— ๋‘๋ฉด ๊ทธ๋ฆฌ ๋ณต์žกํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ svg circle ์ด๋ฏธ์ง€๋ฅผ Html ํŽ˜์ด์ง€์— ์ง์ ‘ ๋ฐฐ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<div class="clock-container flex">
ย  <svg class="svg-seconds" width="24px" height="24px" viewBox="0 0 24 24"  xmlns="http://www.w3.org/2000/svg"><path d="M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486  10-10S17.514 2 12 2z"/></svg>
ย  <svg class="svg-minutes" width="24px" height="24px" viewBox="0 0 24 24"  xmlns="http://www.w3.org/2000/svg"><path d="M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486  10-10S17.514 2 12 2z"/></svg>
ย  <svg class="svg-hours" width="24px" height="24px" viewBox="0 0 24 24"    xmlns="http://www.w3.org/2000/svg"><path d="M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486  10-10S17.514 2 12 2z"/></svg>
ย  ย  
ย  ย <div class="time-box">
ย  ย  ย  ย  ย  ย  <div class="time">00:00:00</div>
ย  ย </div>
</div>



์ด๊ฒƒ์ด ์‹ค์ œ๋กœ ํ•„์š”ํ•œ ๋ชจ๋“  HTML ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

Css ๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ƒ์ž div์— ์ƒ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•˜๋ฏ€๋กœ Svg ์›์„ ๊ทธ ์•ˆ์— ์ ˆ๋Œ€์ ์œผ๋กœ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

.time-box{
ย  ย  position: relative;
...
}

/* SVGS ย */
svg.svg-seconds{ ย 
ย  ย  position: absolute;
ย  ย  width: 360px;
ย  ย  height: 360px;
ย  ย  fill: var(--second);
ย  ย  stroke: var(--third);
ย  ย  stroke-width: 0.5;
ย  ย  stroke-dasharray: 63;
}

svg.svg-minutes{ ย 
ย  ย  position: absolute;
ย  ย  width: 330px;
ย  ย  height: 330px;
ย  ย  fill: transparent;
ย  ย  stroke: var(--fourth);
ย  ย  stroke-width: 0.6;
ย  ย  stroke-dasharray: 63;
}

svg.svg-hours{ ย 
ย  ย  position: absolute;
ย  ย  width: 270px;
ย  ย  height: 270px;
ย  ย  fill: transparent;
ย  ย  stroke: var(--seventh);
ย  ย  stroke-width: 3;
ย  ย  stroke-dasharray: 63;
}



๋Œ€์‹  javascript ๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ Svg ์›์˜ ์ŠคํŠธ๋กœํฌ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹œ๊ฐ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

// TIME FN
function timeFn (){
ย  ย  let time = new Date();
ย  ย  let hours = addZero(time.getHours());
ย  ย  let minutes = addZero(time.getMinutes());
ย  ย  let seconds = time.getSeconds().toString().padStart(2,0);
ย  ย  // 4 SVG OFFSET
ย  ย  secCircle.style.strokeDashoffset = -(63 - (64 * seconds) / 60);
ย  ย  minCircle.style.strokeDashoffset = -(63 - (64 * minutes) / 60);
ย  ย  hoursCircle.style.strokeDashoffset = -(63 - (64 * hours) / 24)
ย  ย  // FULL TIME
ย  ย  let fullTime = `${hours}:${minutes}:${seconds}`;
ย  ย  // console.log(fullTime)
ย  ย  return fullTime;
}



0์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function addZero (timeValue) {
ย  ย  return Number(timeValue) >= 10 ? timeValue : '0'+ timeValue;
};



๋˜๋Š” padStart() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.

.toString().padStart(2,0);



์ด์ œ ๊ฐ„๊ฒฉ์„ 1์ดˆ(๊ธฐ๋ณธ๊ฐ’)๋กœ ์„ค์ •ํ•˜๊ณ  ๋งค๋ฒˆ time div์˜ ๋‚ด๋ถ€ HTML์„ ๋ณ€๊ฒฝํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

setInterval(()=> time.innerHTML = timeFn());


์—ฌ๊ธฐ์—์„œ ์ฝ”๋“œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹คgithub.

์ด๊ฒƒ์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹คvideo guide

๐Ÿ‘‹

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ