From 9a6049f4c4ca7ee65178346b53c2acaf28366b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Sat, 5 Dec 2020 23:06:06 +0100 Subject: [PATCH] refactoring --- docs/src/demos/Examples/Drawing/Component.vue | 139 +++++++++--------- 1 file changed, 71 insertions(+), 68 deletions(-) diff --git a/docs/src/demos/Examples/Drawing/Component.vue b/docs/src/demos/Examples/Drawing/Component.vue index 5cec9a54..08437e25 100644 --- a/docs/src/demos/Examples/Drawing/Component.vue +++ b/docs/src/demos/Examples/Drawing/Component.vue @@ -4,12 +4,12 @@ clear - @@ -36,6 +36,13 @@ export default { data() { return { currentId: uuid(), + ptdata: [], + path: null, + drawing: false, + svg: null, + line: d3.line() + .x(d => d.x) + .y(d => d.y), } }, @@ -45,69 +52,10 @@ export default { lines: [], }) }, - }, - mounted() { - let ptdata = [] - let path - let drawing = false - - const line = d3.line() - .x(d => d.x) - .y(d => d.y) - - const svg = d3.select(this.$refs.canvas) - - const listen = event => { - drawing = true - ptdata = [] // reset point data - - path = svg.append('path') // start a new line - .data([ptdata]) - .attr('id', `id-${this.currentId}`) - .attr('d', line) - - if (event.type === 'mousedown') { - svg.on('mousemove', onmove) - } else { - svg.on('touchmove', onmove) - } - } - - const ignore = event => { - svg.on('mousemove', null) - svg.on('touchmove', null) - - if (!drawing) { - return - } - - drawing = false - - d3.select(this.$refs.canvas).select(`#id-${this.currentId}`).remove() - this.currentId = uuid() - } - - svg - .on('mousedown', listen) - .on('mouseup', ignore) - .on('mouseleave', ignore) - .on('touchstart', listen) - .on('touchend', ignore) - .on('touchleave', ignore) - - function onmove(event) { - event.preventDefault() - - const point = d3.pointers(event)[0] - - ptdata.push({ x: point[0], y: point[1] }) - tick() - } - - const tick = () => { - path.attr('d', points => { - const d = line(points) + tick() { + this.path.attr('d', points => { + const d = this.line(points) const lines = this.node.attrs.lines.filter(item => item.id !== this.currentId) this.updateAttributes({ @@ -122,7 +70,62 @@ export default { return d }) - } + }, + + listen(event) { + this.drawing = true + this.ptdata = [] + + this.path = this.svg + .append('path') + .data([this.ptdata]) + .attr('id', `id-${this.currentId}`) + .attr('d', this.line) + + 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 + + d3.select(this.$refs.canvas) + .select(`#id-${this.currentId}`) + .remove() + + this.currentId = uuid() + }, + + onmove(event) { + event.preventDefault() + + const point = d3.pointers(event)[0] + + this.ptdata.push({ x: point[0], y: point[1] }) + this.tick() + }, + }, + + mounted() { + this.svg = d3.select(this.$refs.canvas) + + this.svg + .on('mousedown', this.listen) + .on('mouseup', this.ignore) + .on('mouseleave', this.ignore) + .on('touchstart', this.listen) + .on('touchend', this.ignore) + .on('touchleave', this.ignore) }, }