티스토리 뷰
어쩌다보니 이런걸 만들어봄.
이 게시글 보고 했음.
<template>
<div>
<div><canvas ref="colorCanvas"></canvas></div>
<div><canvas ref="rainbowCanvas"></canvas></div>
</div>
</template>
이렇게 캔버스 두 개를 사용할 예정임.
<script>
import { onMounted } from 'vue';
import { ref } from '@vue/reactivity';
export default {
name: 'View',
setup() {
const colorCanvas = ref(null);
const rainbowCanvas = ref(null);
let colorCtx = null;
function makeColorCanvas() {
colorCanvas.value.style.width = '300px'
colorCanvas.value.style.height = '300px'
colorCtx = colorCanvas.value.getContext('2d');
colorCtx.canvas.width = 300
colorCtx.canvas.height = 300
let color = 'rgba(0,0,255,1)'
let gradientH = colorCtx.createLinearGradient(0, 0, colorCtx.canvas.width, 0);
gradientH.addColorStop(0, '#FFFFFF');
gradientH.addColorStop(1, color);
colorCtx.fillStyle = gradientH;
colorCtx.fillRect(0, 0, colorCtx.canvas.width, colorCtx.canvas.height);
let gradientV = colorCtx.createLinearGradient(0, 0, 0, 300);
gradientV.addColorStop(0, 'rgba(0,0,0,0)');
gradientV.addColorStop(1, '#000000');
colorCtx.fillStyle = gradientV;
colorCtx.fillRect(0, 0, colorCtx.canvas.width, colorCtx.canvas.height);
}
function makeRainbowCanvas() {
rainbowCanvas.value.style.width = '300px'
rainbowCanvas.value.style.height = '10px'
const rainbowCtx = rainbowCanvas.value.getContext('2d');
rainbowCtx.canvas.width = 300
rainbowCtx.canvas.height = 10
let horizontal = rainbowCtx.createLinearGradient(0, 0, 300, 0);
horizontal.addColorStop(0, "red");
horizontal.addColorStop(0.15, "rgb(255, 255, 0)");
horizontal.addColorStop(0.3, "rgb(0, 255, 0)");
horizontal.addColorStop(0.5, "rgb(0, 255, 255)");
horizontal.addColorStop(0.65, 'rgb(0, 0 ,255)');
horizontal.addColorStop(0.8, 'rgb(255, 0 ,255)');
horizontal.addColorStop(1, "red");
rainbowCtx.fillStyle = horizontal;
rainbowCtx.fillRect(0, 0, rainbowCtx.canvas.width, rainbowCtx.canvas.height);
}
onMounted(()=>{
makeColorCanvas()
makeRainbowCanvas()
})
return {
colorCanvas,
rainbowCanvas
}
}
}
</script>
mdn 참고
반응형
LIST
'공부합시다 > Vue' 카테고리의 다른 글
[Vue.js] 상위/하위 컴포넌트간 데이터 공유 방법 - provide / inject (0) | 2023.04.07 |
---|---|
[Vue3] vue-datepicker 사용기 (0) | 2022.12.20 |
[Vue3] ON/OFF Toggle 만들기 (0) | 2022.12.08 |
[Vue.js 3] Module not found: Error: Can't resolve 'crypto' in (0) | 2022.11.24 |
[Vue.js 2] Error: Vue packages version mismatch (0) | 2022.11.21 |
댓글