有人知道用paper.js清理画布的最好方法吗?我试过常规的画布清理方法,但它们似乎不适用于paper.js
Html
<canvas id="myCanvas" style="background:url(images/graphpaper.jpg); background-repeat:no-repeat" height="400px" width="400px;"></canvas>
<button class="btn btn-default button" id="clearcanvas" onClick="clearcanvas();" type="button">Clear</button> Javascript/Paperscript
<script type="text/paperscript" canvas="myCanvas">
// Create a Paper.js Path to draw a line into it:
tool.minDistance = 5;
var path;
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
function onMouseDown(event) {
// Create a new path and give it a stroke color:
path = new Path();
path.strokeColor = 'red';
path.strokeWidth= 3;
path.opacity="0.4";
// Add a segment to the path where
// you clicked:
path.add(event.point, event.point);
}
function onMouseDrag(event) {
// Every drag event, add a segment
// to the path at the position of the mouse:
path.add(event.point, event.point);
}
</script>发布于 2013-10-10 18:43:15
常规清除不起作用,因为paper.js维护一个场景图并负责为您绘制它。如果要清除在项目中创建的所有项,则需要删除实际项:
只要你的所有项目都在一个层中,project.activeLayer.removeChildren();就可以工作。还有project.clear()可以删除所有东西,包括层。
发布于 2015-05-08 16:13:41
如果有人在没有Javascript经验的情况下寻找答案,我举了一个简单的例子:
1)正如Jürg Lehni提到的,project.activeLayer.removeChildren();可以用来移除活动层内的所有项目。2)为了反映清理过程,您必须调用paper.view.draw();。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Circles</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="paper-full.js"></script>
<script type="text/paperscript" canvas="canvas">
function onMouseUp(event) {
var circle = new Path.Circle({
center: event.middlePoint,
radius: event.delta.length / 2
});
circle.strokeColor = 'black';
circle.fillColor = 'white';
}
</script>
<script type="text/javascript">
paper.install(window);
window.onload = function () {
paper.setup('canvas');
document.getElementById('reset').onclick = function(){
paper.project.activeLayer.removeChildren();
paper.view.draw();
}
};
</script>
</head>
<body>
<canvas style="background-color: #eeeeed" id="canvas" resize></canvas>
<input id="reset" type="button" value="Reset"/>
</body>
</html>CSS:
html,
body {
margin: 0;
overflow: hidden;
height: 100%;
}
/* Scale canvas with resize attribute to full size */
canvas[resize] {
width: 58%;
height: 100%;
}发布于 2013-09-27 23:52:11
如果你还没有尝试过,c.width = c.width; ctx.clearRect(0,0,c.width,c.height);应该是一个很好的选择。
但是要注意-设置画布宽度将重置画布状态,包括所有应用的变换。
谷歌很快就把我带到了https://groups.google.com/forum/#!topic/paperjs/orL7YwBdZq4,上面指定了另一个(更特定于paper.js的)解决方案:project.activeLayer.removeChildren();
https://stackoverflow.com/questions/19054798
复制相似问题