class Draw {
constructor () {
this.mouseDown = false;
this.startX = 0;
this.startY = 0;
this.placeholderX = 0;
this.placeholderY = 0;
this.endX = 0;
this.endY = 0;
this.target = null;
this.pathPoint = []
this.createSvgViewBox()
this.drawGridBg()
this.registerEvent()
}
createSvgViewBox () {
const w = width
const h = w * .6
this.s = DOM.svg(w, h);
}
drawGridBg () {
const w = width
const h = w * .6
const mx = w/2 * margin
const my = h/2 * margin;
const path = `M${mx},${my}${gridPath(w - mx * 2, h - my * 2, false)}`;
this.s.appendChild(svg`<path stroke=#ccc d="${path}">`)
}
registerEvent () {
if (this.s) {
this.s.onmousedown = this.startHandler.bind(this)
this.s.onmousemove = this.moveHandler.bind(this)
this.s.onmouseup = this.endHandler.bind(this)
this.s.addEventListener('touchstart', this.startHandler.bind(this), false)
this.s.addEventListener('touchmove', this.moveHandler.bind(this), false)
this.s.addEventListener('touchend', this.endHandler.bind(this), false)
}
}
startHandler (e) {
e.preventDefault()
this.mouseDown = true
this.startX = this.getOffsetX(e)
this.startY = this.getOffsetY(e)
this.pathPoint.push([this.startX, this.startY])
}
moveHandler (e) {
e.preventDefault()
if(this.mouseDown) {
this.placeholderX = this.getOffsetX(e)
this.placeholderY = this.getOffsetY(e)
if (this.startX !== this.endX || this.startY !== this.endY) {
if (!this.target) {
this.target = this.s.appendChild(svg`<path stroke=#666 fill="none" d="${this.getMultiSquarePath()} L${this.placeholderX} ${this.placeholderY}">`)
} else {
d3.select(this.target).attr('d', `${this.getMultiSquarePath()} L${this.placeholderX} ${this.placeholderY}`)
}
}
}
}
endHandler (e) {
e.preventDefault()
if(this.mouseDown) {
this.endX = this.getOffsetX(e)
this.endY = this.getOffsetY(e)
if (!this.target) {
this.target = this.s.appendChild(svg`<path stroke=#666 d="${this.getMultiSquarePath()} L${this.endX} ${this.endY}`)
} else {
d3.select(this.target)
.attr('d', `${this.getMultiSquarePath()} L${this.endX} ${this.endY}`)
.attr('pointer-events', 'visiblePainted')
}
if (this.nearByRadius(this.pathPoint[0][0], this.pathPoint[0][1], this.endX, this.endY)) {
this.mouseDown = false
this.endX = this.pathPoint[0][0]
this.endY = this.pathPoint[0][1]
d3.select(this.target)
.attr('fill', '#CFE2F3')
.attr('stroke', '#000')
.attr('d', `${this.getMultiSquarePath()} L${this.endX} ${this.endY}`)
.attr('stroke-width', '2')
this.pathPoint = []
} else {
this.pathPoint.push([this.endX, this.endY])
}
this.placeholderX = 0
this.placeholderY = 0
this.endX = 0
this.endY = 0
this.target = null
}
}
nearByRadius (x1, y1, x2, y2) {
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) <= 3
}
getMultiSquarePath() {
let path = 'M'
if (this.pathPoint.length > 0) {
path += this.pathPoint[0].join(' ')
this.pathPoint.slice(1).forEach(point => {
path += 'L' + point.join(' ')
})
}
console.log(path)
return path
}
getOffsetX(e) {
if(e.touches) {
if (e.touches[0]) {
const offset = getDomOffset(this.s)
return e.touches[0].pageX - offset.offsetLeft
} else {
return this.placeholderX
}
} else {
return e.offsetX || e.pageX
}
}
getOffsetY(e) {
if(e.touches) {
if (e.touches[0]) {
return e.touches[0].pageY - getDomOffset(this.s).offsetTop
} else {
return this.placeholderY
}
} else {
return e.offsetY || e.pageY
}
}
}