美文网首页我爱编程
用paper.js画矩形

用paper.js画矩形

作者: 露露璐璐 | 来源:发表于2018-05-17 09:59 被阅读0次

    1.创建一个canvas

    <canvas id="myCanvas" resize></canvas>
    

    2.引入paper.js

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.10.2/paper-full.js"></script>
    

    3.js部分

    paper.install(window);
    window.onload = function() {
        var canvas = document.getElementById('myCanvas');
        paper.setup(canvas);
        var shapesLayer = new paper.Layer();
        var path;
        var point, length;
        var tool = new paper.Tool();
        tool.minDistance = 10;
        tool.onMouseDown = function (event) {
            point = event.point;
            length = shapesLayer.children.length;
        };
        tool.onMouseDrag = function (event) {
            var topLeft = new paper.Point(point);
            var rectSize = new paper.Size((event.point.x - point.x), (event.point.y - point.y));
            var rect = new paper.Rectangle(topLeft, rectSize);
            var path = new paper.Path.Rectangle(rect, 0);
            path.strokeColor = 'black';
           /* path.dashArray = [5, 1];*/
            if (shapesLayer.children.length >= (length + 2) && shapesLayer.children.length >= 2) {
                shapesLayer.removeChildren(shapesLayer.children.length - 2, shapesLayer.children.length - 1);
            }
        };
    
    }
    

    相关文章

      网友评论

        本文标题:用paper.js画矩形

        本文链接:https://www.haomeiwen.com/subject/pyovdftx.html