2017-03-07 172 views
0

我無法讓Paper.js繪製一個矩形。我按照教程,但似乎沒有任何工作。你能告訴我下面的代碼有什麼問題嗎?如何在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> 

回答

2

您使用的代碼實際上並沒有正確繪製矩形。相反,它只是定義了矩形對象。

下面就來繪製一個矩形的正確方法: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> 
+0

這就是我需要知道。謝謝! – imsamimsamimsam

+0

更多信息可以在這裏找到:http://paper.js/tutorials/paths/creating-predefined-shapes/ 如果您覺得這篇文章有幫助,請留下評論。 – ethanchewy