gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
vue实现多个文字添加,对添加的文字实现指定区域拖动的功能
- User
vue实现多个文字添加,对添加的文字实现指定区域拖动的功能
- Assistant
首先,我们需要使用Vue组件化的方式来实现多个文字的添加和指定区域拖动的功能。
针对这个需求,我们可以创建两个组件:
- 添加文字组件
- 文字区域组件
添加文字组件
添加文字组件包含一个文本框和一个添加按钮,可以让用户输入需要添加的文字。
<template> <div class="add-text"> <input type="text" v-model="inputText" placeholder="输入文本"> <button @click="addText">添加</button> </div> </template> <script> export default { data () { return { inputText: '' } }, methods: { addText () { this.$emit('textAdded', this.inputText) this.inputText = '' } } } </script>文字区域组件
文字区域组件包含一个画布和一个装载文字的容器,绑定鼠标按下和拖动事件。
<template> <div class="text-area" ref="area" @mousedown="startDrag" @mousemove="dragText" @mouseup="stopDrag"> <canvas ref="canvas"></canvas> <div v-for="(text, index) in texts" :key="index" class="text" :style="{top: text.y + 'px', left: text.x + 'px', zIndex: text.z}"> {{text.text}} </div> </div> </template> <script> export default { data () { return { texts: [], isDragging: false, // 是否正在拖动 currentTextIndex: -1, // 当前拖动的文字下标 startX: 0, // 鼠标拖动起始位置横坐标 startY: 0 // 鼠标拖动起始位置纵坐标 } }, mounted () { this.initCanvas() }, methods: { initCanvas () { this.canvas = this.$refs.canvas this.ctx = this.canvas.getContext('2d') // 设置画布宽度和高度 this.canvas.width = this.$refs.area.offsetWidth this.canvas.height = this.$refs.area.offsetHeight // 监听画布宽度和高度的变化,重新设置画布宽度和高度 new ResizeObserver(() => { this.canvas.width = this.$refs.area.offsetWidth this.canvas.height = this.$refs.area.offsetHeight }).observe(this.$refs.area) }, startDrag (event) { this.isDragging = true const { x, y } = this.getMousePosition(event) this.startX = x this.startY = y this.currentTextIndex = this.getTextIndex(event.target) if (this.currentTextIndex >= 0) { this.texts[this.currentTextIndex].z = this.getMaxZIndex() + 1 } }, stopDrag () { this.isDragging = false this.currentTextIndex = -1 }, dragText (event) { if (this.isDragging && this.currentTextIndex >= 0) { const { x, y } = this.getMousePosition(event) this.texts[this.currentTextIndex].x += x - this.startX this.texts[this.currentTextIndex].y += y - this.startY this.startX = x this.startY = y } }, getMousePosition (event) { const rect = this.canvas.getBoundingClientRect() const x = event.clientX - rect.left const y = event.clientY - rect.top return { x, y } }, getTextIndex (target) { const index = this.texts.findIndex(text => text.text === target.innerText) return index }, getMaxZIndex () { const maxZIndex = this.texts.reduce((pre, current) => { return pre > current.z ? pre : current.z }, 0) return maxZIndex }, addText (text) { const x = Math.floor(Math.random() * this.$refs.area.offsetWidth) const y = Math.floor(Math.random() * this.$refs.area.offsetHeight) const z = this.getMaxZIndex() + 1 this.texts.push({ text, x, y, z }) } } } </script>在文字区域组件中,我们需要实现以下几个功能:
- 初始化画布大小,并动态设置画布大小;
- 点击文字时,设置其 zindex 最大,在最上层显示;
- 获取最大 zindex;
- 添加文字。
最后,我们在父组件中使用添加文字组件和文字区域组件,并绑定事件。
<template> <div class="app"> <add-text @textAdded="addText"></add-text> <text-area></text-area> </div> </template> <script> import AddText from './components/AddText' import TextArea from './components/TextArea' export default { components: { AddText, TextArea }, methods: { addText (text) { this.$refs.textArea.addText(text) } } } </script>这样,我们就实现了多个文字添加,并对添加的文字实现指定区域拖动的功能。
- User
还需实现对每次添加的文字单独调整文字大小,文字字体,文字横向或纵向显示的功能
- Assistant
针对这个需求,我们需要对文字区域组件进行修改,添加更多的操作功能。
文字区域组件
在文字区域组件中,我们添加了一个下拉框和两个切换按钮,分别用于调整文字大小、文字字体和文字横向或纵向显示。
<template> <div class="text-area" ref="area" @mousedown="startDrag" @mousemove="dragText" @mouseup="stopDrag"> <canvas ref="canvas"></canvas> <div v-for="(text, index) in texts" :key="index" :class="{text: true, horizontal: text.horizontal, vertical: !text.horizontal}" :style="{top: text.y + 'px', left: text.x + 'px', zIndex: text.z, fontSize: text.size + 'px', fontFamily: text.font}"> {{text.text}} </div> <div class="config"> <select v-model="currentTextSize" @change="changeSize"> <option value="12">12</option> <option value="16">16</option> <option value="20">20</option> <option value="24">24</option> </select> <button :class="{active: currentFont === 'Arial'}" @click="changeFont('Arial')">A</button> <button :class="{active: currentFont === 'SimSun'}" @click="changeFont('SimSun')">字</button> <button :class="{active: currentHorizontal}" @click="toggleHorizontal">横向</button> <button :class="{active: !currentHorizontal}" @click="toggleHorizontal">纵向</button> </div> </div> </template> <script> export default { data () { return { texts: [], isDragging: false, // 是否正在拖动 currentTextIndex: -1, // 当前拖动的文字下标 startX: 0, // 鼠标拖动起始位置横坐标 startY: 0, // 鼠标拖动起始位置纵坐标 currentTextSize: 16, // 当前文字大小 currentFont: 'Arial', // 当前文字字体 currentHorizontal: true // 当前文字是否横向显示 } }, mounted () { this.initCanvas() }, methods: { initCanvas () { this.canvas = this.$refs.canvas this.ctx = this.canvas.getContext('2d') // 设置画布宽度和高度 this.canvas.width = this.$refs.area.offsetWidth this.canvas.height = this.$refs.area.offsetHeight // 监听画布宽度和高度的变化,重新设置画布宽度和高度 new ResizeObserver(() => { this.canvas.width = this.$refs.area.offsetWidth this.canvas.height = this.$refs.area.offsetHeight }).observe(this.$refs.area) }, startDrag (event) { this.isDragging = true const { x, y } = this.getMousePosition(event) this.startX = x this.startY = y this.currentTextIndex = this.getTextIndex(event.target) if (this.currentTextIndex >= 0) { this.texts[this.currentTextIndex].z = this.getMaxZIndex() + 1 } }, stopDrag () { this.isDragging = false this.currentTextIndex = -1 }, dragText (event) { if (this.isDragging && this.currentTextIndex >= 0) { const { x, y } = this.getMousePosition(event) this.texts[this.currentTextIndex].x += x - this.startX this.texts[this.currentTextIndex].y += y - this.startY this.startX = x this.startY = y } }, getMousePosition (event) { const rect = this.canvas.getBoundingClientRect() const x = event.clientX - rect.left const y = event.clientY - rect.top return { x, y } }, getTextIndex (target) { const index = this.texts.findIndex(text => text.text === target.innerText) return index }, getMaxZIndex () { const maxZIndex = this.texts.reduce((pre, current) => { return pre > current.z ? pre : current.z }, 0) return maxZIndex }, addText (text) { const x = Math.floor(Math.random() * this.$refs.area.offsetWidth) const y = Math.floor(Math.random() * this.$refs.area.offsetHeight) const z = this.getMaxZIndex() + 1 const size = this.currentTextSize const font = this.currentFont const horizontal = this.currentHorizontal this.texts.push({ text, x, y, z, size, font, horizontal }) }, changeSize () { this.texts[this.currentTextIndex].size = this.currentTextSize }, changeFont (font) { this.currentFont = font this.texts[this.currentTextIndex].font = font }, toggleHorizontal () { this.currentHorizontal = !this.currentHorizontal this.texts[this.currentTextIndex].horizontal = this.currentHorizontal } } } </script>在文字区域组件中,我们需要实现以下几个功能:
- 每次添加的文字单独调整文字大小;
- 文字字体调整;
- 文字横向或纵向显示调整。
这样,我们就实现了对每次添加的文字单独调整文字大小、文字字体、文字横向或纵向显示的功能。