saveData() {
const canvasMap = document.getElementById('canvasMap')
var ctx = canvasMap.getContext('2d')
this.drawingSurfaceImageData = ctx.getImageData(0, 0, 800, 500)
},
restoreData() {
const canvasMap = document.getElementById('canvasMap')
var ctx = canvasMap.getContext('2d')
ctx.putImageData(this.drawingSurfaceImageData, 0, 0)
},
mousedown(e) {
this.flag = true
this.x = e.offsetX // 鼠标落下时的X
this.y = e.offsetY // 鼠标落下时的Y
this.saveData()
},
mouseup() {
this.flag = false
},
mousemove(e) {
if (this.flag) {
this.restoreData()
this.drawRect(e)
}
},
drawRect(e) {
const canvasMap = document.getElementById('canvasMap')
var ctx = canvasMap.getContext('2d')
const x = this.x
const y = this.y
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#00ff00'
ctx.lineWidth = 1
ctx.strokeRect(x, y, e.offsetX - x, e.offsetY - y)
ctx.restore()
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…