canvas个人总结

今天做了大量的canvas作业,发现很多的步奏都是一样的,我自己就封装了一个画直线形2D图形函数。功能不是很强大。

    function drawModule(Json,strokeStyle,fillStyle) {
        if (fillStyle) {
            context.fillStyle = fillStyle;
        }
        if (strokeStyle) {
            context.strokeStyle =strokeStyle ;
        }
        context.beginPath();
        context.moveTo(Json[0][0], Json[0][1]);
        for (var i = 1; i < Json.length; i++) {
            context.lineTo(Json[i][0], Json[i][1]);
        }
        context.closePath();
        if (strokeStyle) {
            context.stroke();
        }

        if (fillStyle) {
            context.fill();
        }
    }

  

Json:绘制图像的坐标值
strokeStyle:线条颜色
fillStyle:填充颜色
调用方法:
drawModule([[630,860],[630,950],[610,950],[610,860]],"green","green");
原文地址:https://www.cnblogs.com/beta-data/p/4640921.html