javascript - 保存对 html Canvas 路径的引用

标签 javascript html canvas

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

可能有数百条线需要绘制,在这些情况下,性能分析显示绘制大约需要 200 毫秒,因此我希望对此进行一些优化。

我注意到的一件事是,在 Canvas 上绘图时,您基本上是在向路径添加点,然后一旦准备就绪,您就可以填充或描边该路径。尽管 Canvas 上的像素已过时,但如果我能够保留对路径的引用,那么更新就像重新绘制之前构建的路径一样简单。

我的问题是:您究竟如何获得 Path 对象?

填充和描边方法出现在accept a path object ,规范定义了 methods for 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();
}

我想要的是这样的:

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();
}

最佳答案

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

顺便说一句,当实现时,Path 对象在与 context.isPointInPath(myPath) 结合时将在 HitTest 中很有用;总有一天……

在浏览器 catch 之前,您可以通过以下方式创建自己的 Path 对象:

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

演示: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>

关于javascript - 保存对 html Canvas 路径的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21077008/

相关文章:

javascript - Mobile 上的 Safari 阻止 window.open

Javascript 透明 Canvas

javascript - 如何为 Canvas "drawing style"应用程序获取平滑的鼠标事件?

javascript - 在 webworker 中运行 Angular 5 应用程序会导致窗口对象未定义

javascript - 单击父项,忽略其子项

javascript - 在 Angular-Cli WebPack 脚本中,我想重新排序捆绑的脚本

javascript - Drupal 帮助 - 警告 : Unresponsive Script - Everytime Goes into or out of Block Edit Mode

css - 如果在表中找到数据,则更改 <td> 的颜色

html - 右对齐结果

html - 我的链接颜色没有改变?