保存对html画布路径的引用

nic*_*ckf 5 javascript html5 canvas

我正在研究一些绘制到画布的代码.代码的一部分在画布上绘制了一些线条.这些行的位置和颜色不会改变,但它们通常需要重新绘制,因为其他代码可能会影响它(例如:绘制在它的顶部).

可以有几百行绘制,在这些情况下,分析显示我需要大约200毫秒绘制,所以我想要稍微优化一下.

我注意到的一件事是,当绘制到画布时,您基本上是在路径上添加点,然后一旦准备好,您就可以填充或描绘该路径.虽然画布上的像素已经过时,但如果我能够保留对路径的引用,那么更新就像重新描绘先前构建的路径一样简单.

我的问题是:你到底怎么得到一个Path对象?

填充和描边方法似乎接受路径对象,规范定义了Path方法,但我似乎无法在Path任何地方找到实际的类...

所以,回顾一下:

我有这样的事情:

function update() {
  context.beginPath();
  // lots of lines added to the default path...
  context.moveTo(x1, y1); context.lineTo(somewhere, else);
  context.moveTo(x2, y2); context.lineTo(somewhere, else);

  context.stroke();
}
Run Code Online (Sandbox Code Playgroud)

我想要的是这样的:

function update() {
  if (!this.path) {
    this.path = new Path(); // <-- here's the magic
    this.path.moveTo(x1, y2); this.path.lineTo(somewhere, else); // etc
  }
  this.path.stroke();
}
Run Code Online (Sandbox Code Playgroud)

mar*_*rkE 1

画布规范需要一个尚未在浏览器中实现的 Path 对象。

顺便说一句,实现后,Path 对象与 context.isPointInPath(myPath) 结合使用,在命中测试中将非常有用。有一天...

以下是您如何创建自己的 Path 对象,直到浏览器跟上:

  • 创建一个 JS 对象,其中包含绘制路径笔划的画布。
  • 当您想要执行 myPath.lines() 时,请使用 myVisibleContext.drawImage(myPath.context,0,0) 将路径的画布“blit”到绘图画布上。

演示: http: //jsfiddle.net/m1erickson/QLJv8/

代码:

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    #canvas{border:1px solid red;}
</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    function Path(maxWidth,maxHeight,color,linewidth,drawingContext){
        this.width=maxWidth;
        this.height=maxHeight;
        this.drawingCtx=drawingContext;
        this.points=[]
        this.canvas=document.createElement("canvas");
        this.canvas.width=maxWidth;
        this.canvas.height=maxHeight;
        this.ctx=this.canvas.getContext("2d");
        this.ctx.strokeStyle=color;
        this.ctx.lineWidth=linewidth;
        this.lastX;
        this.lastY;
    }
    Path.prototype.moveTo=function(x,y){
        this.lastX=x;
        this.lastY=y;

    }
    Path.prototype.lineTo=function(x,y){
        this.ctx.moveTo(this.lastX,this.lastY);
        this.ctx.lineTo(x,y);
        this.ctx.stroke();
        this.lastX=x;
        this.lastY=y;
    }
    Path.prototype.stroke=function(){
        this.drawingCtx.drawImage(this.canvas,0,0);
    }

    // create a new path object

    var p=new Path(300,300,"blue",2,ctx);

    // set the Path's drawing commands

    p.moveTo(69,91);
    p.lineTo(250,150);
    p.moveTo(69,208);
    p.lineTo(180,54);
    p.lineTo(180,245);
    p.lineTo(69,91);
    p.moveTo(69,208);
    p.lineTo(250,150);

    // draw the Path.canvas to the drawing canvas
    p.stroke();

    // tests...

    $("#stroke").click(function(){
        p.stroke();
    });
    $("#erase").click(function(){
        ctx.clearRect(0,0,canvas.width,canvas.height);
    });

}); // end $(function(){});
</script>

</head>

<body>
    <button id="stroke">Path.stroke</button><br>
    <button id="erase">Erase main canvas</button><br>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)