refactoring
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
<svg viewBox="0 0 500 250" ref="canvas">
|
<svg viewBox="0 0 500 250" ref="canvas">
|
||||||
<template v-for="item in node.attrs.lines">
|
<template v-for="item in node.attrs.lines">
|
||||||
<path
|
<path
|
||||||
v-if="item.id !== currentId"
|
v-if="item.id !== id"
|
||||||
:key="item.id"
|
:key="item.id"
|
||||||
:d="item.path"
|
:d="item.path"
|
||||||
:id="`id-${item.id}`"
|
:id="`id-${item.id}`"
|
||||||
@@ -46,31 +46,61 @@ export default {
|
|||||||
return {
|
return {
|
||||||
color: '#000000',
|
color: '#000000',
|
||||||
size: 2,
|
size: 2,
|
||||||
currentId: uuid(),
|
|
||||||
points: [],
|
|
||||||
path: null,
|
|
||||||
drawing: false,
|
|
||||||
svg: null,
|
svg: null,
|
||||||
|
path: null,
|
||||||
|
points: [],
|
||||||
|
drawing: false,
|
||||||
|
id: uuid(),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
clear() {
|
onStartDrawing(event) {
|
||||||
this.updateAttributes({
|
this.drawing = true
|
||||||
lines: [],
|
this.points = []
|
||||||
})
|
this.path = this.svg
|
||||||
|
.append('path')
|
||||||
|
.data([this.points])
|
||||||
|
.attr('id', `id-${this.id}`)
|
||||||
|
.attr('stroke', this.color)
|
||||||
|
.attr('stroke-width', this.size)
|
||||||
|
|
||||||
|
const moveEvent = event.type === 'mousedown'
|
||||||
|
? 'mousemove'
|
||||||
|
: 'touchmove'
|
||||||
|
|
||||||
|
this.svg.on(moveEvent, this.onMove)
|
||||||
|
},
|
||||||
|
|
||||||
|
onMove(event) {
|
||||||
|
event.preventDefault()
|
||||||
|
this.points.push(d3.pointers(event)[0])
|
||||||
|
this.tick()
|
||||||
|
},
|
||||||
|
|
||||||
|
onEndDrawing() {
|
||||||
|
this.svg.on('mousemove', null)
|
||||||
|
this.svg.on('touchmove', null)
|
||||||
|
|
||||||
|
if (!this.drawing) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.drawing = false
|
||||||
|
this.svg.select(`#id-${this.id}`).remove()
|
||||||
|
this.id = uuid()
|
||||||
},
|
},
|
||||||
|
|
||||||
tick() {
|
tick() {
|
||||||
this.path.attr('d', points => {
|
this.path.attr('d', points => {
|
||||||
const path = d3.line()(points)
|
const path = d3.line()(points)
|
||||||
const lines = this.node.attrs.lines.filter(item => item.id !== this.currentId)
|
const lines = this.node.attrs.lines.filter(item => item.id !== this.id)
|
||||||
|
|
||||||
this.updateAttributes({
|
this.updateAttributes({
|
||||||
lines: [
|
lines: [
|
||||||
...lines,
|
...lines,
|
||||||
{
|
{
|
||||||
id: this.currentId,
|
id: this.id,
|
||||||
color: this.color,
|
color: this.color,
|
||||||
size: this.size,
|
size: this.size,
|
||||||
path,
|
path,
|
||||||
@@ -82,40 +112,10 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
listen(event) {
|
clear() {
|
||||||
this.drawing = true
|
this.updateAttributes({
|
||||||
this.points = []
|
lines: [],
|
||||||
this.path = this.svg
|
})
|
||||||
.append('path')
|
|
||||||
.data([this.points])
|
|
||||||
.attr('id', `id-${this.currentId}`)
|
|
||||||
.attr('stroke', this.color)
|
|
||||||
.attr('stroke-width', this.size)
|
|
||||||
|
|
||||||
const moveEvent = event.type === 'mousedown'
|
|
||||||
? 'mousemove'
|
|
||||||
: 'touchmove'
|
|
||||||
|
|
||||||
this.svg.on(moveEvent, this.onmove)
|
|
||||||
},
|
|
||||||
|
|
||||||
ignore() {
|
|
||||||
this.svg.on('mousemove', null)
|
|
||||||
this.svg.on('touchmove', null)
|
|
||||||
|
|
||||||
if (!this.drawing) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
this.drawing = false
|
|
||||||
this.svg.select(`#id-${this.currentId}`).remove()
|
|
||||||
this.currentId = uuid()
|
|
||||||
},
|
|
||||||
|
|
||||||
onmove(event) {
|
|
||||||
event.preventDefault()
|
|
||||||
this.points.push(d3.pointers(event)[0])
|
|
||||||
this.tick()
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -123,12 +123,12 @@ export default {
|
|||||||
this.svg = d3.select(this.$refs.canvas)
|
this.svg = d3.select(this.$refs.canvas)
|
||||||
|
|
||||||
this.svg
|
this.svg
|
||||||
.on('mousedown', this.listen)
|
.on('mousedown', this.onStartDrawing)
|
||||||
.on('mouseup', this.ignore)
|
.on('mouseup', this.onEndDrawing)
|
||||||
.on('mouseleave', this.ignore)
|
.on('mouseleave', this.onEndDrawing)
|
||||||
.on('touchstart', this.listen)
|
.on('touchstart', this.onStartDrawing)
|
||||||
.on('touchend', this.ignore)
|
.on('touchend', this.onEndDrawing)
|
||||||
.on('touchleave', this.ignore)
|
.on('touchleave', this.onEndDrawing)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user