如何在Paper.js中绘制矩形?

时间:2017-03-07 22:01:54

标签: javascript html5-canvas paperjs

我无法让Paper.js绘制一个矩形。我已经按照教程,但似乎没有任何工作。你能告诉我下面的代码有什么问题吗?

<!DOCTYPE html>
<html>

<head>
<!--load paper.js-->
<script type="text/javascript" src="http://localhost/node_modules/paper/dist/paper-core.js"></script>

<!--drawing script-->
<script type="text/javascript" canvas="canvas">
window.onload = function() {
    var canvas = document.getElementById("canvas");

    paper.setup(canvas);

    var meterStart = new paper.Point(5,5);
    var meterSize = new paper.Size(40, 720);
    var meter = new paper.Rectangle(meterStart, meterSize);
    meter.strokeColor = 'black';
    paper.view.draw();

    }
</script>
</head>

<body>
<canvas id="canvas" height="750px" width="600px"></canvas>
</body>

</html>

1 个答案:

答案 0 :(得分:4)

您使用的代码实际上并没有正确地绘制矩形。相反,它只是定义矩形对象。

这是绘制矩形的正确方法:https://jsbin.com/niwegobanu/edit?html,js,output

<!DOCTYPE html>
<html>

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

</head>

<script type="text/paperscript" canvas="myCanvas">
    var rectangle = new Rectangle(new Point(50, 50), new Point(150, 100));
    var path = new Path.Rectangle(rectangle);
    path.fillColor = '#e9e9ff';
    path.selected = true;
</script>
</head>
<body>
    <canvas id="myCanvas" resize></canvas>
</body>
</html>