javascript - HTML5 Canvas : Manipulating Individual Paths

标签 javascript html canvas

在使用 HTML5 Canvas 时,如何将特定路径保存到 javascript 变量/数组,然后对其进行操作?到目前为止,这是我正在做的事情:

                    ctx.beginPath();
                        ctx.moveTo(lastX,lastY);
                        ctx.lineTo(x,y);
                        ctx.lineWidth = s*2;
                        ctx.stroke();
                    ctx.closePath();

现在,我需要的是有时能够将此路径存储在数组中。然后,我需要能够返回并稍后更改数组中所有路径的颜色。 (显然,我也不知道该怎么做。)

最佳答案

您可以将绘制路径所需的所有数据序列化为 javascript 对象

使用 javascript 对象的好处是,如果您需要将路径移动到不同的位置(例如返回服务器),您可以进一步将对象序列化为 JSON。

var path1={
    lineWidth:1, 
    stroke:"blue", 
    points:[{x:10,y:10},{x:100,y:50},{x:30,y:200}]
};

然后您可以使用该对象绘制/重绘该路径

    function draw(path){

        // beginPath
        ctx.beginPath();

        // move to the beginning point of this path
        ctx.moveTo(path.points[0].x,path.points[0].y);

        // draw lines to each point on the path
        for(pt=1;pt<path.points.length;pt++){
            var point=path.points[pt];
            ctx.lineTo(point.x,point.y);
        }

        // set the path styles (color & linewidth)
        ctx.strokeStyle=path.stroke;
        ctx.lineWidth=path.lineWidth;

        // stroke this path
        ctx.stroke();
    }

要更改路径的颜色,您只需更改对象的 stroke 属性并再次调用 draw():

    paths[0].stroke="orange";
    paths[1].stroke="green";
    draw();

这是代码和 fiddle :http://jsfiddle.net/m1erickson/McZrH/

<!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(){

    // get references to canvas and context
    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    // serialize paths to a javascript objects
    var path1={lineWidth:1, stroke:"blue", points:[]};
    var path2={lineWidth:4, stroke:"red", points:[]};

    // save the paths to an array
    var paths=[];
    paths.push(path1);
    paths.push(path2);


    // build test path1
    newPoint(25,25,path1);
    newPoint(100,50,path1);
    newPoint(50,75,path1);
    newPoint(25,25,path1);

    // build test path2
    newPoint(200,100,path2);
    newPoint(250,100,path2);
    newPoint(250,200,path2);
    newPoint(200,200,path2);
    newPoint(200,100,path2);

    // draw the paths defined in the paths array
    draw();

    // add a new point to a path
    function newPoint(x,y,path){
        path.points.push({x:x,y:y});
    }


    function draw(){

        ctx.clearRect(0,0,canvas.width,canvas.height);

        for(p=0;p<paths.length;p++){

            // get a path definition
            var path=paths[p];

            // beginPath
            ctx.beginPath();

            // move to the beginning point of this path
            ctx.moveTo(path.points[0].x,path.points[0].y);

            // draw lines to each point on the path
            for(pt=1;pt<path.points.length;pt++){
                var point=path.points[pt];
                ctx.lineTo(point.x,point.y);
            }

            // set the path styles (color & linewidth)
            ctx.strokeStyle=path.stroke;
            ctx.lineWidth=path.lineWidth;

            // stroke this path
            ctx.stroke();

        }

    }

    // test
    // change the stroke color on each path
    $("#recolor").click(function(){
        paths[0].stroke="orange";
        paths[1].stroke="green";
        draw();
    });

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

</head>

<body>
    <button id="recolor">Recolor</button><br>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

关于javascript - HTML5 Canvas : Manipulating Individual Paths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19471369/

相关文章:

javascript - jQuery 选择器不适用于一个 div

html - 如何更改仅通过使用 CSS 和媒体查询动态生成的图像的 url?

html - 添加背景图像时输入边框更改

javascript - 打印包含动态图像的 div

javascript - 提高 Sprite 绘图性能

javascript - 在 chrome 和 Firefox 中扭曲的 Canvas 边框

php - 激活 PHP 函数的提交按钮

javascript - 更新循环内的变量

javascript - 在 IE 中动态设置滚动到 iframe

javascript - 如何将Greensock集成到pixi js中