画板的功能
- 修改画笔颜色;
- 修改画笔粗细;
- 橡皮擦;
- 重置画板;
- 撤销上一步;
- 保存成图片;
一步步实现
class Board {
constructor(id) {
this.canvas = document.getElementById(id);
this.context = this.canvas.getContext('2d');
this.isDrawing = false;
this.posX =服务器托管网 0;
this.posY = 0;
this.init();
}
init() {
const bindDown = this.handleMouseDown.bind(this);
const bindMove = this.handleMouseMove.bind(this);
this.canvas.addEventListener('mousedown', bindDown);
this.canvas.addEventListener('mousemove', bindMove);
window.ad服务器托管网dEventListener('mouseup', () => {
this.isDrawing = false;
});
}
handleMouseDown(e) {
const rect = this.canvas.getBoundingClientRect();
this.posX = e.clientX - rect.left;
this.posY = e.clientY - rect.top;
this.isDrawing = true;
}
handleMouseMove(e) {
if (this.isDrawing === true) {
const rect = this.canvas.getBoundingClientRect();
this.drawLine(this.context, this.posX, this.posY,
e.clientX - rect.left, e.clientY - rect.top);
this.posX = e.clientX - rect.left;
this.posY = e.clientY - rect.top;
}
}
drawLine(context, x1, y1, x2, y2) {
context.beginPath();
context.strokeStyle = 'black';
context.lineWidth = 1;
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.stroke();
context.closePath();
}
}
new Board('myCanvas');
第二步,可以修改画笔颜色;
document.getElementById('colorPicker').addEventListener('change', e => {
b.changeColor(e.target.value);
})
class Board {
constructor(id, color = '#000') {
this.penColor = color;
}
drawLine(context, x1, y1, x2, y2) {
context.strokeStyle = this.penColor;
}
changeColor(color) {
this.penColor = color;
}
}
第三步,修改画笔粗细;
ctx.lineWidth = number;
第四步,橡皮擦;
context.globalCompositeOperation='destination-out';
参照刮刮乐功能。
第五步,重置画板;
context.clearRect(0,0,width, height);
第六步,撤销上一步;
this.canvas.toDataURL()
将当前canvas保存为base64的图片,存放在数组中。再设置一个索引,撤销/恢复修改索引的值,从数组中取出对应的图片。
第七步,保存为图片;
创建一个a标签,href为toDataURL()生成的图片,模拟点击事件,点击a链接。
完整代码
1
2
3
4
5
class Board {
constructor(id, color = '#000', fontsize = 1) {
this.canvas = document.getElementById(id);
this.context = this.canvas.getContext('2d');
this.isDrawing = false;
this.posX = 0;
this.posY = 0;
this.penColor = color;
this.fontsize = fontsize;
this.isErasing = false;
this.step = 0;
this.histroyList = [];
this.init();
}
init() {
const bindDown = this.handleMouseDown.bind(this);
const bindMove = this.handleMouseMove.bind(this);
this.canvas.addEventListener('mousedown', bindDown);
this.canvas.addEventListener('mousemove', bindMove);
window.addEventListener('mouseup', () => {
this.isDrawing = false;
});
this.canvas.addEventListener('mouseup', () => {
this.step++;
if (this.step 0) {
this.step--;
this.context.clearRect(0, 0, window.myCanvas.width,
window.myCanvas.height);
let pic = new Image();
pic.src = this.histroyList[this.step];
pic.addEventListener('load', () => {
this.context.drawImage(pic, 0, 0);
})
} else {
console.log('不能继续撤销了')
}
}
recover() {
if (this.step {
this.context.drawImage(pic, 0, 0);
})
} else {
console.log('不能继续恢复了')
}
}
saveAsPic() {
const el = document.createElement('a');
el.href = this.canvas.toDataURL();
el.download = 'canvas';
const event = new MouseEvent('click');
el.dispatchEvent(event);
}
}
const b = new Board('myCanvas');
window.colorPicker.addEventListener('change', e => {
b.changeColor(e.target.value);
})
window.fontsizeSelect.addEventListener('change', e => {
b.changeFontSize(window.fontsizeSelect.value);
})
window.eraser.addEventListener('click', () => {
b.switchEraseStatus();
})
window.reset.addEventListener('click', () => {
b.clearBoard();
})
window.revoke.addEventListener('click', () => {
b.revoke();
})
window.recover.addEventListener('click', () => {
b.recover();
})
window.saveAsPic.addEventListener('click', () => {
b.saveAsPic();
})
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: 搭建个人博客-hexo+github详细完整步骤
自己也算是摸爬滚打搭建成功,然后自己再重新安装部署一遍,把完整步骤分享给大家,同时最后有一些连接,如果我的步骤不行,大家可以参考其他人的(这个有点花费时间,大家提前有个心理准备 – _-) 一、第一步:下载安装Git 1、Git下载地址 2、安装步骤 个人选择…