我有一个应用程序,用户可以在其中在画布上绘制笔触。如何保存输入,以便在我清除画布以删除例如已添加的矩形后重新绘制它。我使用了一个数组,但重新绘制所有数组条目确实需要很长时间。
我实现笔画的代码类似于:http: //jsfiddle.net/FgNQk/1/
var points[];
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var width = window.innerWidth;
var height = window.innerHeight;
canvas.height = height;
canvas.width = width;
canvas.addEventListener('mousedown', function(e) {
this.down = true;
this.X = e.pageX ;
this.Y = e.pageY ;
this.color = rgb();
}, 0);
canvas.addEventListener('mouseup', function() {
this.down = false;
}, 0);
canvas.addEventListener('mousemove', function(e) {
if(this.down) {
with(ctx) {
beginPath();
moveTo(this.X, this.Y);
lineTo(e.pageX , e.pageY );
strokeStyle = "rgb(0,0,0)";
ctx.lineWidth=1;
stroke();
// saving via array
if (this.down) {
points.push({x:e.pageX,y:e.pageY});
}
}
this.X = e.pageX ;
this.Y = e.pageY ;
}
}, 0);