Reputation: 23
I cannot get Paper.js to draw a rectangle. I've followed the tutorial but nothing seems to be working. Can you tell me what's wrong with my code below?
<!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>
Upvotes: 2
Views: 4355
Reputation: 572
The code you are using doesn't actually draw out the rectangle correctly. Instead, it just defines the rectangle object.
Here's the correct way to draw a rectangle: 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>
Upvotes: 7