Skip to content

Commit

Permalink
feat: 添加画布操作相关逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wangrongding committed Mar 22, 2024
1 parent 6181ec5 commit e67e328
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
},
"dependencies": {
"fabric": "^5.3.0",
"lodash": "^4.17.21",
"pinia": "^2.1.7",
"vue": "^3.4.21",
"vue-router": "^4.3.0"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.3.3",
"@tsconfig/node20": "^20.1.2",
"@types/fabric": "^5.3.7",
"@types/jsdom": "^21.1.6",
"@types/lodash": "^4.17.0",
"@types/node": "^20.11.25",
"@vitejs/plugin-vue": "^5.0.4",
"@vitejs/plugin-vue-jsx": "^3.1.0",
Expand Down
18 changes: 17 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions src/components/player/CanvasPlayer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<script setup lang="ts">
import { fabric } from 'fabric'
import { onMounted, ref } from 'vue'
import Logo from '~/assets/github.svg'
defineProps<{
msg: string
}>()
let canvas: fabric.Canvas
let ctx: CanvasRenderingContext2D
const container = ref<HTMLElement | null>(null)
function initCanvas() {
canvas = new fabric.Canvas('canvas', {
selection: true,
hoverCursor: 'pointer',
backgroundColor: '#000'
})
resizePlayer()
canvas.on('mouse:down', canvasOnMouseDown)
ctx = canvas.getContext()
drawImage()
}
function drawImage() {
// 绘制 svg
fabric.loadSVGFromURL(Logo, (objects, options) => {
const obj = fabric.util.groupSVGElements(objects, options)
obj.set({
scaleX: 5,
scaleY: 5,
left: canvas.width! / 2 - (obj.width! * 5) / 2,
top: canvas.height! / 2 - (obj.height! * 5) / 2,
angle: 0
})
canvas.add(obj)
})
// 绘制文字
const text = new fabric.Text('开发中...', {
left: 100,
top: 100,
fill: 'white',
fontSize: 30
})
canvas.add(text)
}
function resizePlayer() {
const width = container.value!.clientWidth
const height = container.value!.clientHeight
canvas.setDimensions({
width,
height
})
}
function canvasOnMouseDown() {}
onMounted((): void => {
initCanvas()
})
</script>
<template>
<div class="h-full w-full overflow-hidden" ref="container">
<canvas id="canvas"></canvas>
</div>
</template>
<style lang="scss" scoped></style>
21 changes: 15 additions & 6 deletions src/views/main-page.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<script setup lang="ts"></script>
<script setup lang="ts">
import { onMounted } from 'vue'
import CanvasPlayer from '~/components/player/CanvasPlayer.vue'
onMounted(() => {
// 禁止右键菜单
document.oncontextmenu = () => false
// onselectstart事件禁用网页上选取的内容
document.onselectstart = () => false
})
</script>

<template>
<div class="main flex h-full select-none flex-col gap-1 bg-[#161618] text-white">
<div class="main flex h-full flex-col gap-1 bg-[#161618] text-white">
<!-- 头部 -->
<header class="flex h-[50px] items-center justify-between bg-[#282c34] px-6">
<!-- logo -->
Expand All @@ -20,7 +30,7 @@

<div class="flex flex-1 gap-1">
<!-- 左边栏 -->
<div class="left-box w-[400px] bg-[#282c34] p-8">
<div class="left-box w-[400px] min-w-[300px] bg-[#282c34] p-8">
left-panel
<span>( 开发中... )</span>
</div>
Expand All @@ -29,9 +39,8 @@
<div class="flex flex-1 flex-col gap-1">
<div class="flex flex-1 gap-1">
<!-- 视频画布 -->
<div class="flex-1 bg-[#282c34] p-8">
player
<span>( 开发中... )</span>
<div class="canvas-player-container flex-1 bg-[#282c34] p-8">
<CanvasPlayer msg="hello" />
</div>
<!-- 右边属性设置栏 -->
<div class="right-panel w-[300px] bg-[#282c34] p-8">
Expand Down

0 comments on commit e67e328

Please sign in to comment.